Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Sensor MIDI
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-07-15 21:30:09
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Each potentiometer send midi messages through USB */
- /* The ultrasensor send midi message too. The */
- /* ultrasensor work between 5 cm and 40 cm */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Ultrasonic.h> // https://github.com/ErickSimoes/Ultrasonic
- #include <MIDI.h> // https://github.com/FortySevenEffects/arduino_midi_library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void sendPotMidiMessage(uint8_t potValue, uint8_t channel);
- void sendUltrasonicMidiMessage(float distance, uint8_t channel);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t ultrasonic_Echo_PIN = GPIO_NUM_13; // Example GPIO pin for Echo
- const uint8_t ultrasonic_Trigger_PIN = GPIO_NUM_12; // Example GPIO pin for Trigger
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t pot1_Vout_PIN = GPIO_NUM_34; // Example ADC pin for potentiometer 1
- const uint8_t pot2_Vout_PIN = GPIO_NUM_35; // Example ADC pin for potentiometer 2
- const uint8_t pot3_Vout_PIN = GPIO_NUM_32; // Example ADC pin for potentiometer 3
- /***** DEFINITION OF MIDI CHANNELS *****/
- const uint8_t MIDI_CHANNEL_POT1 = 1;
- const uint8_t MIDI_CHANNEL_POT2 = 2;
- const uint8_t MIDI_CHANNEL_POT3 = 3;
- const uint8_t MIDI_CHANNEL_ULTRASONIC = 4;
- /***** DEFINITION OF ULTRASONIC SENSOR INSTANCE *****/
- Ultrasonic ultrasonic(ultrasonic_Trigger_PIN, ultrasonic_Echo_PIN);
- /***** VARIABLES *****/
- // Raw potentiometer values
- uint16_t pot1_value = 0;
- uint16_t pot2_value = 0;
- uint16_t pot3_value = 0;
- // MIDI message value range
- const uint8_t MIDI_MIN = 0;
- const uint8_t MIDI_MAX = 127;
- /****** MIDI INSTANCE *****/
- MIDI_CREATE_DEFAULT_INSTANCE();
- /****** SETUP FUNCTION *****/
- void setup(void)
- {
- // Initialize serial for debugging
- Serial.begin(115200);
- // Initialize potentiometer pins as ADC
- // No need for pinMode for ADC pins on ESP32
- // Initialize ultrasonic sensor pins
- pinMode(ultrasonic_Echo_PIN, INPUT);
- pinMode(ultrasonic_Trigger_PIN, OUTPUT);
- // Initialize MIDI
- MIDI.begin(MIDI_CHANNEL_OMNI);
- }
- /****** LOOP FUNCTION *****/
- void loop(void)
- {
- // Read potentiometers
- pot1_value = analogRead(pot1_Vout_PIN);
- pot2_value = analogRead(pot2_Vout_PIN);
- pot3_value = analogRead(pot3_Vout_PIN);
- // Map potentiometer values to MIDI range
- uint8_t midiPot1 = map(pot1_value, 0, 4095, MIDI_MIN, MIDI_MAX);
- uint8_t midiPot2 = map(pot2_value, 0, 4095, MIDI_MIN, MIDI_MAX);
- uint8_t midiPot3 = map(pot3_value, 0, 4095, MIDI_MIN, MIDI_MAX);
- // Send MIDI messages for potentiometers
- sendPotMidiMessage(midiPot1, MIDI_CHANNEL_POT1);
- sendPotMidiMessage(midiPot2, MIDI_CHANNEL_POT2);
- sendPotMidiMessage(midiPot3, MIDI_CHANNEL_POT3);
- // Read ultrasonic distance in centimeters
- float distance = ultrasonic.read();
- // Check if distance is between 5 cm and 40 cm
- if (distance >= 5.0 && distance <= 40.0)
- {
- // Send MIDI message for ultrasonic sensor
- sendUltrasonicMidiMessage(distance, MIDI_CHANNEL_ULTRASONIC);
- }
- // Optional: Add a small delay to avoid flooding MIDI
- delay(50);
- }
- /****** FUNCTION TO SEND POTENTIOMETER MIDI MESSAGE *****/
- void sendPotMidiMessage(uint8_t potValue, uint8_t channel)
- {
- // Send Control Change message with controller number 1 (modulation wheel) as example
- // You can choose other controller numbers as needed
- MIDI.sendControlChange(channel, 1, potValue);
- }
- /****** FUNCTION TO SEND ULTRASONIC MIDI MESSAGE *****/
- void sendUltrasonicMidiMessage(float distance, uint8_t channel)
- {
- // Map distance to MIDI value (0-127)
- uint8_t midiValue = map(distance * 10, 50, 400, MIDI_MIN, MIDI_MAX); // Convert to mm for mapping
- // Send Control Change message with controller number 2 as example
- MIDI.sendControlChange(channel, 2, midiValue);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment