pleasedontcode

Sensor MIDI rev_09

Jul 15th, 2025
160
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 21:30:09
  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 channel);
  35. void sendUltrasonicMidiMessage(float distance, uint8_t channel);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t ultrasonic_Echo_PIN = GPIO_NUM_13; // Example GPIO pin for Echo
  39. const uint8_t ultrasonic_Trigger_PIN = GPIO_NUM_12; // Example GPIO pin for Trigger
  40.  
  41. /***** DEFINITION OF ANALOG INPUT PINS *****/
  42. const uint8_t pot1_Vout_PIN = GPIO_NUM_34; // Example ADC pin for potentiometer 1
  43. const uint8_t pot2_Vout_PIN = GPIO_NUM_35; // Example ADC pin for potentiometer 2
  44. const uint8_t pot3_Vout_PIN = GPIO_NUM_32; // Example ADC pin for potentiometer 3
  45.  
  46. /***** DEFINITION OF MIDI CHANNELS *****/
  47. const uint8_t MIDI_CHANNEL_POT1 = 1;
  48. const uint8_t MIDI_CHANNEL_POT2 = 2;
  49. const uint8_t MIDI_CHANNEL_POT3 = 3;
  50. const uint8_t MIDI_CHANNEL_ULTRASONIC = 4;
  51.  
  52. /***** DEFINITION OF ULTRASONIC SENSOR INSTANCE *****/
  53. Ultrasonic ultrasonic(ultrasonic_Trigger_PIN, ultrasonic_Echo_PIN);
  54.  
  55. /***** VARIABLES *****/
  56. // Raw potentiometer values
  57. uint16_t pot1_value = 0;
  58. uint16_t pot2_value = 0;
  59. uint16_t pot3_value = 0;
  60.  
  61. // MIDI message value range
  62. const uint8_t MIDI_MIN = 0;
  63. const uint8_t MIDI_MAX = 127;
  64.  
  65. /****** MIDI INSTANCE *****/
  66. MIDI_CREATE_DEFAULT_INSTANCE();
  67.  
  68. /****** SETUP FUNCTION *****/
  69. void setup(void)
  70. {
  71.   // Initialize serial for debugging
  72.   Serial.begin(115200);
  73.  
  74.   // Initialize potentiometer pins as ADC
  75.   // No need for pinMode for ADC pins on ESP32
  76.  
  77.   // Initialize ultrasonic sensor pins
  78.   pinMode(ultrasonic_Echo_PIN, INPUT);
  79.   pinMode(ultrasonic_Trigger_PIN, OUTPUT);
  80.  
  81.   // Initialize MIDI
  82.   MIDI.begin(MIDI_CHANNEL_OMNI);
  83. }
  84.  
  85. /****** LOOP FUNCTION *****/
  86. void loop(void)
  87. {
  88.   // Read potentiometers
  89.   pot1_value = analogRead(pot1_Vout_PIN);
  90.   pot2_value = analogRead(pot2_Vout_PIN);
  91.   pot3_value = analogRead(pot3_Vout_PIN);
  92.  
  93.   // Map potentiometer values to MIDI range
  94.   uint8_t midiPot1 = map(pot1_value, 0, 4095, MIDI_MIN, MIDI_MAX);
  95.   uint8_t midiPot2 = map(pot2_value, 0, 4095, MIDI_MIN, MIDI_MAX);
  96.   uint8_t midiPot3 = map(pot3_value, 0, 4095, MIDI_MIN, MIDI_MAX);
  97.  
  98.   // Send MIDI messages for potentiometers
  99.   sendPotMidiMessage(midiPot1, MIDI_CHANNEL_POT1);
  100.   sendPotMidiMessage(midiPot2, MIDI_CHANNEL_POT2);
  101.   sendPotMidiMessage(midiPot3, MIDI_CHANNEL_POT3);
  102.  
  103.   // Read ultrasonic distance in centimeters
  104.   float distance = ultrasonic.read();
  105.  
  106.   // Check if distance is between 5 cm and 40 cm
  107.   if (distance >= 5.0 && distance <= 40.0)
  108.   {
  109.     // Send MIDI message for ultrasonic sensor
  110.     sendUltrasonicMidiMessage(distance, MIDI_CHANNEL_ULTRASONIC);
  111.   }
  112.  
  113.   // Optional: Add a small delay to avoid flooding MIDI
  114.   delay(50);
  115. }
  116.  
  117. /****** FUNCTION TO SEND POTENTIOMETER MIDI MESSAGE *****/
  118. void sendPotMidiMessage(uint8_t potValue, uint8_t channel)
  119. {
  120.   // Send Control Change message with controller number 1 (modulation wheel) as example
  121.   // You can choose other controller numbers as needed
  122.   MIDI.sendControlChange(channel, 1, potValue);
  123. }
  124.  
  125. /****** FUNCTION TO SEND ULTRASONIC MIDI MESSAGE *****/
  126. void sendUltrasonicMidiMessage(float distance, uint8_t channel)
  127. {
  128.   // Map distance to MIDI value (0-127)
  129.   uint8_t midiValue = map(distance * 10, 50, 400, MIDI_MIN, MIDI_MAX); // Convert to mm for mapping
  130.  
  131.   // Send Control Change message with controller number 2 as example
  132.   MIDI.sendControlChange(channel, 2, midiValue);
  133. }
  134.  
  135. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment