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 Mapper
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-07-20 17:58:17
- ********* 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 channel);
- void sendUltrasonicMidiMessage(float distance, uint8_t channel);
- /***** 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 = 34; // ADC1_CH6
- const uint8_t pot2_Vout_PIN_D13 = 35; // ADC1_CH7
- const uint8_t pot3_Vout_PIN_D14 = 32; // ADC1_CH4
- /***** DEFINITION OF MIDI CHANNELS *****/
- const uint8_t midiChannelPot1 = 1;
- const uint8_t midiChannelPot2 = 2;
- const uint8_t midiChannelPot3 = 3;
- const uint8_t midiChannelUltrasonic = 4;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- int pot1RawValue = 0;
- int pot2RawValue = 0;
- int pot3RawValue = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- int pot1MidiValue = 0;
- int pot2MidiValue = 0;
- int pot3MidiValue = 0;
- /***** ULTRASONIC SENSOR INSTANCE *****/
- Ultrasonic ultrasonic(ultrasonic_Trigger_PIN_RX2, ultrasonic_Echo_PIN_TX2);
- /****** MIDI INSTANCE *****/
- MIDI_CREATE_DEFAULT_INSTANCE();
- void setup(void)
- {
- // Initialize serial communication for debugging if needed
- Serial.begin(115200);
- // 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
- pot1RawValue = analogRead(pot1_Vout_PIN_D4);
- pot2RawValue = analogRead(pot2_Vout_PIN_D13);
- pot3RawValue = analogRead(pot3_Vout_PIN_D14);
- // Map potentiometer values (0-4095) to MIDI range (0-127)
- pot1MidiValue = map(pot1RawValue, 0, 4095, 0, 127);
- pot2MidiValue = map(pot2RawValue, 0, 4095, 0, 127);
- pot3MidiValue = map(pot3RawValue, 0, 4095, 0, 127);
- // Send MIDI messages for potentiometers
- sendPotMidiMessage(pot1MidiValue, midiChannelPot1);
- sendPotMidiMessage(pot2MidiValue, midiChannelPot2);
- sendPotMidiMessage(pot3MidiValue, midiChannelPot3);
- // Read ultrasonic sensor distance in centimeters
- float distance = ultrasonic.read();
- // Check if distance is within 5cm to 40cm
- if (distance >= 5.0 && distance <= 40.0)
- {
- // Send MIDI message based on ultrasonic distance
- sendUltrasonicMidiMessage(distance, midiChannelUltrasonic);
- }
- delay(100); // Small delay for stability
- }
- // Function to send MIDI message based on potentiometer value
- void sendPotMidiMessage(int potValue, uint8_t channel)
- {
- MIDI.sendControlChange(channel, 1, potValue); // Control Change #1 (Modulation Wheel)
- }
- // Function to send MIDI message based on ultrasonic distance
- void sendUltrasonicMidiMessage(float distance, uint8_t channel)
- {
- int midiValue = map((int)distance, 5, 40, 0, 127);
- MIDI.sendControlChange(channel, 2, midiValue); // Control Change #2 (Breath Controller)
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment