pleasedontcode

Sensor Controller rev_15

Jul 15th, 2025
166
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 Controller
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-07-15 22:48:16
  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();
  35. void readPotentiometers();
  36. void readUltrasonicSensor();
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t ultrasonic_Echo_PIN_TX2 = 13; // Echo pin
  40. const uint8_t ultrasonic_Trigger_PIN_RX2 = 12; // Trigger pin
  41.  
  42. /***** DEFINITION OF ANALOG INPUT PINS *****/
  43. const uint8_t pot1_Vout_PIN_D4 = 34; // GPIO34 for ESP32 analog input
  44. const uint8_t pot2_Vout_PIN_D13 = 35; // GPIO35 for ESP32 analog input
  45. const uint8_t pot3_Vout_PIN_D14 = 32; // GPIO32 for ESP32 analog input
  46.  
  47. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  48. /***** used to store raw data *****/
  49. bool ultrasonicTriggerRawData = false;
  50.  
  51. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  52. /***** used to store data after characteristic curve transformation *****/
  53. float ultrasonicPhyData = 0.0;
  54.  
  55. /***** SYSTEM REQUIREMENTS IMPLEMENTATION VARIABLES *****/
  56. // Potentiometers send MIDI messages via USB
  57. // Ultrasonic sensor sends MIDI message when between 5cm and 40cm
  58.  
  59. /****** LIBRARY CLASS INSTANCES *****/
  60. Ultrasonic ultrasonic(ultrasonic_Trigger_PIN_RX2, ultrasonic_Echo_PIN_TX2);
  61. MIDI_CREATE_DEFAULT_INSTANCE();
  62.  
  63. void setup(void)
  64. {
  65.   // put your setup code here, to run once:
  66.   pinMode(ultrasonic_Echo_PIN_TX2, INPUT);
  67.   pinMode(ultrasonic_Trigger_PIN_RX2, OUTPUT);
  68.   pinMode(pot1_Vout_PIN_D4, INPUT);
  69.   pinMode(pot2_Vout_PIN_D13, INPUT);
  70.   pinMode(pot3_Vout_PIN_D14, INPUT);
  71.  
  72.   // Initialize MIDI
  73.   MIDI.begin();
  74.  
  75.   // Initialize serial for debugging if needed
  76.   Serial.begin(115200);
  77. }
  78.  
  79. void loop(void)
  80. {
  81.   // Read potentiometers and send MIDI messages
  82.   readPotentiometers();
  83.  
  84.   // Read ultrasonic sensor and send MIDI message if in range
  85.   readUltrasonicSensor();
  86.  
  87.   // Update outputs (trigger pin)
  88.   updateOutputs();
  89.  
  90.   delay(10); // Small delay for stability
  91. }
  92.  
  93. void updateOutputs()
  94. {
  95.   digitalWrite(ultrasonic_Trigger_PIN_RX2, ultrasonicTriggerRawData);
  96. }
  97.  
  98. void readPotentiometers()
  99. {
  100.   int pot1Value = analogRead(pot1_Vout_PIN_D4);
  101.   int pot2Value = analogRead(pot2_Vout_PIN_D13);
  102.   int pot3Value = analogRead(pot3_Vout_PIN_D14);
  103.  
  104.   // Map potentiometer values (0-4095) to MIDI control change values (0-127)
  105.   byte midiValue1 = map(pot1Value, 0, 4095, 0, 127);
  106.   byte midiValue2 = map(pot2Value, 0, 4095, 0, 127);
  107.   byte midiValue3 = map(pot3Value, 0, 4095, 0, 127);
  108.  
  109.   // Send MIDI Control Change messages for each potentiometer
  110.   // Control change number can be assigned as needed, here using 1, 2, 3
  111.   MIDI.sendControlChange(1, midiValue1, 1);
  112.   MIDI.sendControlChange(2, midiValue2, 1);
  113.   MIDI.sendControlChange(3, midiValue3, 1);
  114. }
  115.  
  116. void readUltrasonicSensor()
  117. {
  118.   // Trigger ultrasonic sensor
  119.   digitalWrite(ultrasonic_Trigger_PIN_RX2, HIGH);
  120.   delayMicroseconds(10);
  121.   digitalWrite(ultrasonic_Trigger_PIN_RX2, LOW);
  122.  
  123.   // Read distance in centimeters
  124.   float distance = ultrasonic.read();
  125.  
  126.   // Check if distance is within 5cm to 40cm
  127.   if (distance >= 5 && distance <= 40)
  128.   {
  129.     // Send MIDI message, for example, Control Change 4
  130.     // Map distance to MIDI value (0-127)
  131.     byte midiValue = map(distance, 5, 40, 0, 127);
  132.     MIDI.sendControlChange(4, midiValue, 1);
  133.   }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment