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 Control
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-07-15 22:40:50
- ********* 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 updateOutputs(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t ultrasonic_Echo_PIN = 13; // Echo pin for ultrasonic sensor
- const uint8_t ultrasonic_Trigger_PIN = 12; // Trigger pin for ultrasonic sensor
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t pot1_Vout_PIN = 34; // GPIO34 for potentiometer 1
- const uint8_t pot2_Vout_PIN = 35; // GPIO35 for potentiometer 2
- const uint8_t pot3_Vout_PIN = 36; // GPIO36 for potentiometer 3
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- bool ultrasonic_rawData = LOW;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float ultrasonic_phyData = 0.0;
- /****** LIBRARY CLASS INSTANCES *****/
- Ultrasonic ultrasonicSensor(ultrasonic_Trigger_PIN, ultrasonic_Echo_PIN);
- MIDI_CREATE_DEFAULT_INSTANCE();
- void setup(void)
- {
- // Initialize serial for debugging if needed
- Serial.begin(115200);
- // Initialize potentiometer pins
- pinMode(pot1_Vout_PIN, INPUT);
- pinMode(pot2_Vout_PIN, INPUT);
- pinMode(pot3_Vout_PIN, INPUT);
- // Initialize ultrasonic sensor pins
- pinMode(ultrasonic_Trigger_PIN, OUTPUT);
- pinMode(ultrasonic_Echo_PIN, INPUT);
- // Initialize MIDI
- MIDI.begin(MIDI_CHANNEL_OMNI);
- }
- void loop(void)
- {
- updateOutputs(); // Refresh output data
- // Add a small delay to avoid flooding
- delay(50);
- }
- void updateOutputs()
- {
- // Read potentiometers (values from 0 to 4095 for ESP32 ADC)
- int pot1_value = analogRead(pot1_Vout_PIN);
- int pot2_value = analogRead(pot2_Vout_PIN);
- int pot3_value = analogRead(pot3_Vout_PIN);
- // Map potentiometer values to MIDI range (0-127)
- byte midiPot1 = map(pot1_value, 0, 4095, 0, 127);
- byte midiPot2 = map(pot2_value, 0, 4095, 0, 127);
- byte midiPot3 = map(pot3_value, 0, 4095, 0, 127);
- // Send MIDI Control Change messages for potentiometers
- MIDI.sendControlChange(20, midiPot1, 1); // CC 20 for pot1
- MIDI.sendControlChange(21, midiPot2, 1); // CC 21 for pot2
- MIDI.sendControlChange(22, midiPot3, 1); // CC 22 for pot3
- // Read ultrasonic sensor distance in centimeters
- float distance_cm = ultrasonicSensor.read();
- // Check if distance is within 5cm to 40cm
- if (distance_cm >= 5.0 && distance_cm <= 40.0)
- {
- // Map distance to MIDI value (0-127)
- byte midiDistance = map((int)distance_cm, 5, 40, 0, 127);
- // Send MIDI Control Change for ultrasonic sensor
- MIDI.sendControlChange(23, midiDistance, 1); // CC 23 for ultrasonic
- }
- else
- {
- // If out of range, optionally send a value or do nothing
- // Here, we choose to send 0 when out of range
- MIDI.sendControlChange(23, 0, 1);
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment