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:
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-07-15 22:06:04
- ********* 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(int potValue, uint8_t midiCC);
- void sendUltrasonicMidiMessage(float distance);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t ultrasonic_Echo_PIN_TX2 = 13; // Example pin, replace with actual
- const uint8_t ultrasonic_Trigger_PIN_RX2 = 12; // Example pin, replace with actual
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t pot1_Vout_PIN_D4 = 34; // Example ADC pin, replace with actual
- const uint8_t pot2_Vout_PIN_D13 = 35; // Example ADC pin, replace with actual
- const uint8_t pot3_Vout_PIN_D14 = 36; // Example ADC pin, replace with actual
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool ultrasonicTriggerRawData = false;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float ultrasonicDistance = 0.0;
- /****** LIBRARIES CLASS INSTANCES *****/
- Ultrasonic ultrasonicSensor(ultrasonic_Trigger_PIN_RX2, ultrasonic_Echo_PIN_TX2);
- MIDI_CREATE_DEFAULT_INSTANCE();
- void setup(void)
- {
- // Initialize pins
- pinMode(ultrasonic_Echo_PIN_TX2, INPUT);
- pinMode(ultrasonic_Trigger_PIN_RX2, OUTPUT);
- pinMode(pot1_Vout_PIN_D4, INPUT);
- pinMode(pot2_Vout_PIN_D13, INPUT);
- pinMode(pot3_Vout_PIN_D14, INPUT);
- // Initialize MIDI
- MIDI.begin(MIDI_CHANNEL_OMNI);
- }
- void loop(void)
- {
- // Read potentiometers (0-4095), map to 0-127 for MIDI CC
- int pot1Value = analogRead(pot1_Vout_PIN_D4);
- int pot2Value = analogRead(pot2_Vout_PIN_D13);
- int pot3Value = analogRead(pot3_Vout_PIN_D14);
- // Send MIDI messages for potentiometers
- sendPotMidiMessage(pot1Value, 1); // CC1
- sendPotMidiMessage(pot2Value, 2); // CC2
- sendPotMidiMessage(pot3Value, 3); // CC3
- // Read ultrasonic sensor distance
- ultrasonicDistance = ultrasonicSensor.read();
- // Check if distance is within 5cm to 40cm
- if (ultrasonicDistance >= 5 && ultrasonicDistance <= 40)
- {
- sendUltrasonicMidiMessage(ultrasonicDistance);
- }
- delay(100); // Small delay for stability
- }
- void sendPotMidiMessage(int potValue, uint8_t midiCC)
- {
- // Map potentiometer value (0-4095) to MIDI CC value (0-127)
- int midiValue = map(potValue, 0, 4095, 0, 127);
- MIDI.sendControlChange(midiCC, midiValue, 1); // Send on channel 1
- }
- void sendUltrasonicMidiMessage(float distance)
- {
- // Map distance (5-40cm) to MIDI CC value (0-127)
- int midiValue = map(distance, 5, 40, 0, 127);
- MIDI.sendControlChange(4, midiValue, 1); // Use CC4 for ultrasonic sensor
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment