Advertisement
pleasedontcode

Sensor Controller rev_16

Jul 15th, 2025
367
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 23:25:37
  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_TX2 = TX2;
  38.  
  39. /***** DEFINITION OF ANALOG INPUT PINS *****/
  40. const uint8_t pot1_Vout_PIN_D4 = D4;
  41. const uint8_t pot2_Vout_PIN_D13 = D13;
  42. const uint8_t pot3_Vout_PIN_D14 = D14;
  43.  
  44. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  45. const uint8_t ultrasonic_Trigger_PIN_RX2 = RX2;
  46.  
  47. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  48. /***** used to store raw data *****/
  49. bool ultrasonic_Trigger_rawData = 0;
  50.  
  51. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  52. /***** used to store data after characteristic curve transformation *****/
  53. float ultrasonic_phyData = 0.0;
  54.  
  55. /***** Instantiate MIDI object for USB MIDI communication *****/
  56. MIDI_CREATE_DEFAULT_INSTANCE();
  57.  
  58. /***** Instantiate Ultrasonic object *****/
  59. Ultrasonic ultrasonicSensor(ultrasonic_Echo_PIN_TX2, ultrasonic_Trigger_PIN_RX2);
  60.  
  61. /***** Variables to store potentiometer values *****/
  62. int potValue1 = 0;
  63. int potValue2 = 0;
  64. int potValue3 = 0;
  65.  
  66. void setup(void)
  67. {
  68.     // put your setup code here, to run once:
  69.     pinMode(ultrasonic_Echo_PIN_TX2, INPUT);
  70.     pinMode(pot1_Vout_PIN_D4, INPUT);
  71.     pinMode(pot2_Vout_PIN_D13, INPUT);
  72.     pinMode(pot3_Vout_PIN_D14, INPUT);
  73.     pinMode(ultrasonic_Trigger_PIN_RX2, OUTPUT);
  74.  
  75.     // Initialize MIDI communication
  76.     MIDI.begin();
  77.  
  78.     // Initialize ultrasonic sensor
  79.     ultrasonicSensor.setTimeout(20000UL); // default timeout
  80. }
  81.  
  82. void loop(void)
  83. {
  84.     // put your main code here, to run repeatedly:
  85.     updateOutputs();
  86. }
  87.  
  88. void updateOutputs()
  89. {
  90.     // Read potentiometers
  91.     potValue1 = analogRead(pot1_Vout_PIN_D4);
  92.     potValue2 = analogRead(pot2_Vout_PIN_D13);
  93.     potValue3 = analogRead(pot3_Vout_PIN_D14);
  94.  
  95.     // Map potentiometer values to MIDI control change range (0-127)
  96.     int midiCC1 = map(potValue1, 0, 4095, 0, 127);
  97.     int midiCC2 = map(potValue2, 0, 4095, 0, 127);
  98.     int midiCC3 = map(potValue3, 0, 4095, 0, 127);
  99.  
  100.     // Send MIDI control change messages for potentiometers
  101.     MIDI.sendControlChange(1, midiCC1, 1); // CC 1 on channel 1
  102.     MIDI.sendControlChange(2, midiCC2, 1); // CC 2 on channel 1
  103.     MIDI.sendControlChange(3, midiCC3, 1); // CC 3 on channel 1
  104.  
  105.     // Read ultrasonic sensor distance in centimeters
  106.     ultrasonic_phyData = ultrasonicSensor.read();
  107.  
  108.     // Check if distance is within 5cm to 40cm
  109.     if (ultrasonic_phyData >= 5 && ultrasonic_phyData <= 40)
  110.     {
  111.         // Map distance to MIDI velocity (0-127)
  112.         int velocity = map((int)ultrasonic_phyData, 5, 40, 0, 127);
  113.         // Send MIDI Note On message (e.g., note 60 middle C)
  114.         MIDI.sendNoteOn(60, velocity, 1);
  115.         // Optional: add a delay or send Note Off after some time
  116.         // For simplicity, send Note Off immediately
  117.         MIDI.sendNoteOff(60, 0, 1);
  118.     }
  119.  
  120.     // Trigger ultrasonic sensor
  121.     digitalWrite(ultrasonic_Trigger_PIN_RX2, HIGH);
  122.     delayMicroseconds(10);
  123.     digitalWrite(ultrasonic_Trigger_PIN_RX2, LOW);
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement