pleasedontcode

Sensor Control rev_14

Jul 15th, 2025
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Sensor Control
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-07-15 22:40:50
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Each potentiometer send midi messages through USB */
  21.     /* The ultrasensor send midi message too. The */
  22.     /* ultrasensor work between 5 cm and 40 cm */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Ultrasonic.h>  //https://github.com/ErickSimoes/Ultrasonic
  29. #include <MIDI.h>        //https://github.com/FortySevenEffects/arduino_midi_library
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t ultrasonic_Echo_PIN = 13; // Echo pin for ultrasonic sensor
  38. const uint8_t ultrasonic_Trigger_PIN = 12; // Trigger pin for ultrasonic sensor
  39.  
  40. /***** DEFINITION OF ANALOG INPUT PINS *****/
  41. const uint8_t pot1_Vout_PIN = 34; // GPIO34 for potentiometer 1
  42. const uint8_t pot2_Vout_PIN = 35; // GPIO35 for potentiometer 2
  43. const uint8_t pot3_Vout_PIN = 36; // GPIO36 for potentiometer 3
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. /***** used to store raw data *****/
  47. bool ultrasonic_rawData = LOW;
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. /***** used to store data after characteristic curve transformation *****/
  51. float ultrasonic_phyData = 0.0;
  52.  
  53. /****** LIBRARY CLASS INSTANCES *****/
  54. Ultrasonic ultrasonicSensor(ultrasonic_Trigger_PIN, ultrasonic_Echo_PIN);
  55. MIDI_CREATE_DEFAULT_INSTANCE();
  56.  
  57. void setup(void)
  58. {
  59.   // Initialize serial for debugging if needed
  60.   Serial.begin(115200);
  61.  
  62.   // Initialize potentiometer pins
  63.   pinMode(pot1_Vout_PIN, INPUT);
  64.   pinMode(pot2_Vout_PIN, INPUT);
  65.   pinMode(pot3_Vout_PIN, INPUT);
  66.  
  67.   // Initialize ultrasonic sensor pins
  68.   pinMode(ultrasonic_Trigger_PIN, OUTPUT);
  69.   pinMode(ultrasonic_Echo_PIN, INPUT);
  70.  
  71.   // Initialize MIDI
  72.   MIDI.begin(MIDI_CHANNEL_OMNI);
  73. }
  74.  
  75. void loop(void)
  76. {
  77.   updateOutputs(); // Refresh output data
  78.  
  79.   // Add a small delay to avoid flooding
  80.   delay(50);
  81. }
  82.  
  83. void updateOutputs()
  84. {
  85.   // Read potentiometers (values from 0 to 4095 for ESP32 ADC)
  86.   int pot1_value = analogRead(pot1_Vout_PIN);
  87.   int pot2_value = analogRead(pot2_Vout_PIN);
  88.   int pot3_value = analogRead(pot3_Vout_PIN);
  89.  
  90.   // Map potentiometer values to MIDI range (0-127)
  91.   byte midiPot1 = map(pot1_value, 0, 4095, 0, 127);
  92.   byte midiPot2 = map(pot2_value, 0, 4095, 0, 127);
  93.   byte midiPot3 = map(pot3_value, 0, 4095, 0, 127);
  94.  
  95.   // Send MIDI Control Change messages for potentiometers
  96.   MIDI.sendControlChange(20, midiPot1, 1); // CC 20 for pot1
  97.   MIDI.sendControlChange(21, midiPot2, 1); // CC 21 for pot2
  98.   MIDI.sendControlChange(22, midiPot3, 1); // CC 22 for pot3
  99.  
  100.   // Read ultrasonic sensor distance in centimeters
  101.   float distance_cm = ultrasonicSensor.read();
  102.  
  103.   // Check if distance is within 5cm to 40cm
  104.   if (distance_cm >= 5.0 && distance_cm <= 40.0)
  105.   {
  106.     // Map distance to MIDI value (0-127)
  107.     byte midiDistance = map((int)distance_cm, 5, 40, 0, 127);
  108.     // Send MIDI Control Change for ultrasonic sensor
  109.     MIDI.sendControlChange(23, midiDistance, 1); // CC 23 for ultrasonic
  110.   }
  111.   else
  112.   {
  113.     // If out of range, optionally send a value or do nothing
  114.     // Here, we choose to send 0 when out of range
  115.     MIDI.sendControlChange(23, 0, 1);
  116.   }
  117. }
  118.  
  119. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment