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 22:35:35
- ********* 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 controlNumber);
- void sendUltrasonicMidiMessage(float distance);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t ultrasonic_Echo_PIN = 13; // Echo pin
- const uint8_t ultrasonic_Trigger_PIN = 12; // Trigger pin
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t pot1_Vout_PIN = 34; // D34, analog input
- const uint8_t pot2_Vout_PIN = 35; // D35, analog input
- const uint8_t pot3_Vout_PIN = 36; // D36, analog input
- /***** MIDI Control Change Numbers for potentiometers *****/
- const uint8_t pot1_CC = 20;
- const uint8_t pot2_CC = 21;
- const uint8_t pot3_CC = 22;
- /***** MIDI Control Number for Ultrasonic sensor *****/
- const uint8_t ultrasonic_CC = 23;
- /***** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- Ultrasonic ultrasonic(ultrasonic_Trigger_PIN, ultrasonic_Echo_PIN);
- MIDI_CREATE_DEFAULT_INSTANCE();
- void setup(void)
- {
- // Initialize pins
- pinMode(ultrasonic_Echo_PIN, INPUT);
- pinMode(ultrasonic_Trigger_PIN, OUTPUT);
- pinMode(pot1_Vout_PIN, INPUT);
- pinMode(pot2_Vout_PIN, INPUT);
- pinMode(pot3_Vout_PIN, INPUT);
- // Initialize MIDI
- MIDI.begin(MIDI_CHANNEL_OMNI);
- }
- void loop(void)
- {
- // Read potentiometers (0-4095), map to 0-127
- uint8_t pot1_value = map(analogRead(pot1_Vout_PIN), 0, 4095, 0, 127);
- uint8_t pot2_value = map(analogRead(pot2_Vout_PIN), 0, 4095, 0, 127);
- uint8_t pot3_value = map(analogRead(pot3_Vout_PIN), 0, 4095, 0, 127);
- // Send MIDI messages for potentiometers
- sendPotMidiMessage(pot1_value, pot1_CC);
- sendPotMidiMessage(pot2_value, pot2_CC);
- sendPotMidiMessage(pot3_value, pot3_CC);
- // Read ultrasonic distance in centimeters
- float distance = ultrasonic.read(); // in cm
- // Check if distance is within 5cm to 40cm
- if (distance >= 5.0 && distance <= 40.0)
- {
- sendUltrasonicMidiMessage(distance);
- }
- delay(100); // Small delay to avoid flooding MIDI messages
- }
- void sendPotMidiMessage(uint8_t potValue, uint8_t controlNumber)
- {
- // Send Control Change message
- MIDI.sendControlChange(controlNumber, potValue, 1);
- }
- void sendUltrasonicMidiMessage(float distance)
- {
- // Map distance (5-40cm) to MIDI value (0-127)
- uint8_t midiValue = map(distance * 100, 500, 4000, 0, 127);
- MIDI.sendControlChange(ultrasonic_CC, midiValue, 1);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment