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: MIDI Controller
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-07-15 22:31: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 sendPotMidiMessages();
- void sendUltrasonicMidiMessage();
- //***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t ultrasonic_Echo_PIN_TX2 = 13; // Echo pin
- const uint8_t ultrasonic_Trigger_PIN_RX2 = 12; // Trigger pin
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t pot1_Vout_PIN_D4 = 4; // Potentiometer 1
- const uint8_t pot2_Vout_PIN_D13 = 13; // Potentiometer 2
- const uint8_t pot3_Vout_PIN_D14 = 14; // Potentiometer 3
- /***** MIDI Control Change Numbers for potentiometers *****/
- const uint8_t midiCCPot1 = 20;
- const uint8_t midiCCPot2 = 21;
- const uint8_t midiCCPot3 = 22;
- /***** DEFINITION OF ULTRASONIC SENSOR INSTANCE *****/
- Ultrasonic ultrasonic(ultrasonic_Trigger_PIN_RX2, ultrasonic_Echo_PIN_TX2);
- /***** VARIABLES TO STORE POTENTIOMETER VALUES *****/
- int pot1Value = 0;
- int pot2Value = 0;
- int pot3Value = 0;
- /***** VARIABLES TO STORE ULTRASONIC SENSOR DATA *****/
- float ultrasonicDistance = 0.0;
- /***** SYSTEM REQUIREMENTS *****/
- // Each potentiometer send midi messages through USB
- // The ultrasensor send midi message too. The ultrasensor work between 5 cm and 40 cm
- void setup(void)
- {
- // Initialize pins
- 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
- MIDI.begin(MIDI_CHANNEL_OMNI);
- }
- void loop(void)
- {
- // Read potentiometers
- pot1Value = analogRead(pot1_Vout_PIN_D4);
- pot2Value = analogRead(pot2_Vout_PIN_D13);
- pot3Value = analogRead(pot3_Vout_PIN_D14);
- // Send MIDI messages for potentiometers
- sendPotMidiMessages();
- // Read ultrasonic sensor distance in centimeters
- ultrasonicDistance = ultrasonic.read();
- // Send MIDI message if within range 5cm to 40cm
- if (ultrasonicDistance >= 5.0 && ultrasonicDistance <= 40.0)
- {
- sendUltrasonicMidiMessage();
- }
- // Refresh output data (if needed)
- updateOutputs();
- delay(50); // Small delay for stability
- }
- void sendPotMidiMessages()
- {
- // Map potentiometer values (0-1023) to MIDI CC value (0-127)
- uint8_t midiValue1 = map(pot1Value, 0, 1023, 0, 127);
- uint8_t midiValue2 = map(pot2Value, 0, 1023, 0, 127);
- uint8_t midiValue3 = map(pot3Value, 0, 1023, 0, 127);
- // Send MIDI CC messages
- MIDI.sendControlChange(midiCCPot1, midiValue1, 1);
- MIDI.sendControlChange(midiCCPot2, midiValue2, 1);
- MIDI.sendControlChange(midiCCPot3, midiValue3, 1);
- }
- void sendUltrasonicMidiMessage()
- {
- // Map ultrasonic distance (5-40cm) to MIDI CC (0-127)
- uint8_t midiValue = map(ultrasonicDistance, 5, 40, 0, 127);
- // Send MIDI CC message for ultrasonic sensor
- MIDI.sendControlChange(23, midiValue, 1);
- }
- void updateOutputs()
- {
- // Update trigger pin state based on ultrasonic sensor data if needed
- // For example, trigger a pulse when within range
- static bool triggerState = false;
- static unsigned long lastTriggerTime = 0;
- const unsigned long triggerInterval = 100; // ms
- if (millis() - lastTriggerTime > triggerInterval)
- {
- triggerState = !triggerState;
- digitalWrite(ultrasonic_Trigger_PIN_RX2, triggerState);
- lastTriggerTime = millis();
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment