pleasedontcode

Sensor MIDI rev_13

Jul 15th, 2025
174
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 MIDI
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-07-15 22:35:35
  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 sendPotMidiMessage(uint8_t potValue, uint8_t controlNumber);
  35. void sendUltrasonicMidiMessage(float distance);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t ultrasonic_Echo_PIN = 13; // Echo pin
  39. const uint8_t ultrasonic_Trigger_PIN = 12; // Trigger pin
  40.  
  41. /***** DEFINITION OF ANALOG INPUT PINS *****/
  42. const uint8_t pot1_Vout_PIN = 34; // D34, analog input
  43. const uint8_t pot2_Vout_PIN = 35; // D35, analog input
  44. const uint8_t pot3_Vout_PIN = 36; // D36, analog input
  45.  
  46. /***** MIDI Control Change Numbers for potentiometers *****/
  47. const uint8_t pot1_CC = 20;
  48. const uint8_t pot2_CC = 21;
  49. const uint8_t pot3_CC = 22;
  50.  
  51. /***** MIDI Control Number for Ultrasonic sensor *****/
  52. const uint8_t ultrasonic_CC = 23;
  53.  
  54. /***** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  55. Ultrasonic ultrasonic(ultrasonic_Trigger_PIN, ultrasonic_Echo_PIN);
  56. MIDI_CREATE_DEFAULT_INSTANCE();
  57.  
  58. void setup(void)
  59. {
  60.   // Initialize pins
  61.   pinMode(ultrasonic_Echo_PIN, INPUT);
  62.   pinMode(ultrasonic_Trigger_PIN, OUTPUT);
  63.   pinMode(pot1_Vout_PIN, INPUT);
  64.   pinMode(pot2_Vout_PIN, INPUT);
  65.   pinMode(pot3_Vout_PIN, INPUT);
  66.  
  67.   // Initialize MIDI
  68.   MIDI.begin(MIDI_CHANNEL_OMNI);
  69. }
  70.  
  71. void loop(void)
  72. {
  73.   // Read potentiometers (0-4095), map to 0-127
  74.   uint8_t pot1_value = map(analogRead(pot1_Vout_PIN), 0, 4095, 0, 127);
  75.   uint8_t pot2_value = map(analogRead(pot2_Vout_PIN), 0, 4095, 0, 127);
  76.   uint8_t pot3_value = map(analogRead(pot3_Vout_PIN), 0, 4095, 0, 127);
  77.  
  78.   // Send MIDI messages for potentiometers
  79.   sendPotMidiMessage(pot1_value, pot1_CC);
  80.   sendPotMidiMessage(pot2_value, pot2_CC);
  81.   sendPotMidiMessage(pot3_value, pot3_CC);
  82.  
  83.   // Read ultrasonic distance in centimeters
  84.   float distance = ultrasonic.read(); // in cm
  85.  
  86.   // Check if distance is within 5cm to 40cm
  87.   if (distance >= 5.0 && distance <= 40.0)
  88.   {
  89.     sendUltrasonicMidiMessage(distance);
  90.   }
  91.  
  92.   delay(100); // Small delay to avoid flooding MIDI messages
  93. }
  94.  
  95. void sendPotMidiMessage(uint8_t potValue, uint8_t controlNumber)
  96. {
  97.   // Send Control Change message
  98.   MIDI.sendControlChange(controlNumber, potValue, 1);
  99. }
  100.  
  101. void sendUltrasonicMidiMessage(float distance)
  102. {
  103.   // Map distance (5-40cm) to MIDI value (0-127)
  104.   uint8_t midiValue = map(distance * 100, 500, 4000, 0, 127);
  105.   MIDI.sendControlChange(ultrasonic_CC, midiValue, 1);
  106. }
  107.  
  108. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment