Advertisement
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 Controller
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-07-15 23:25:37
- ********* 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_TX2 = TX2;
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t pot1_Vout_PIN_D4 = D4;
- const uint8_t pot2_Vout_PIN_D13 = D13;
- const uint8_t pot3_Vout_PIN_D14 = D14;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t ultrasonic_Trigger_PIN_RX2 = RX2;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- bool ultrasonic_Trigger_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float ultrasonic_phyData = 0.0;
- /***** Instantiate MIDI object for USB MIDI communication *****/
- MIDI_CREATE_DEFAULT_INSTANCE();
- /***** Instantiate Ultrasonic object *****/
- Ultrasonic ultrasonicSensor(ultrasonic_Echo_PIN_TX2, ultrasonic_Trigger_PIN_RX2);
- /***** Variables to store potentiometer values *****/
- int potValue1 = 0;
- int potValue2 = 0;
- int potValue3 = 0;
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(ultrasonic_Echo_PIN_TX2, INPUT);
- pinMode(pot1_Vout_PIN_D4, INPUT);
- pinMode(pot2_Vout_PIN_D13, INPUT);
- pinMode(pot3_Vout_PIN_D14, INPUT);
- pinMode(ultrasonic_Trigger_PIN_RX2, OUTPUT);
- // Initialize MIDI communication
- MIDI.begin();
- // Initialize ultrasonic sensor
- ultrasonicSensor.setTimeout(20000UL); // default timeout
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- updateOutputs();
- }
- void updateOutputs()
- {
- // Read potentiometers
- potValue1 = analogRead(pot1_Vout_PIN_D4);
- potValue2 = analogRead(pot2_Vout_PIN_D13);
- potValue3 = analogRead(pot3_Vout_PIN_D14);
- // Map potentiometer values to MIDI control change range (0-127)
- int midiCC1 = map(potValue1, 0, 4095, 0, 127);
- int midiCC2 = map(potValue2, 0, 4095, 0, 127);
- int midiCC3 = map(potValue3, 0, 4095, 0, 127);
- // Send MIDI control change messages for potentiometers
- MIDI.sendControlChange(1, midiCC1, 1); // CC 1 on channel 1
- MIDI.sendControlChange(2, midiCC2, 1); // CC 2 on channel 1
- MIDI.sendControlChange(3, midiCC3, 1); // CC 3 on channel 1
- // Read ultrasonic sensor distance in centimeters
- ultrasonic_phyData = ultrasonicSensor.read();
- // Check if distance is within 5cm to 40cm
- if (ultrasonic_phyData >= 5 && ultrasonic_phyData <= 40)
- {
- // Map distance to MIDI velocity (0-127)
- int velocity = map((int)ultrasonic_phyData, 5, 40, 0, 127);
- // Send MIDI Note On message (e.g., note 60 middle C)
- MIDI.sendNoteOn(60, velocity, 1);
- // Optional: add a delay or send Note Off after some time
- // For simplicity, send Note Off immediately
- MIDI.sendNoteOff(60, 0, 1);
- }
- // Trigger ultrasonic sensor
- digitalWrite(ultrasonic_Trigger_PIN_RX2, HIGH);
- delayMicroseconds(10);
- digitalWrite(ultrasonic_Trigger_PIN_RX2, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement