/********* 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 Sensors"
- Source Code NOT compiled for: ESP32 DevKit V1
- Source Code created on: 2024-06-06 22:20:14
********* 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 *****/
/****** DEFINITION OF LIBRARIES *****/
#include // https://github.com/JRodrigoTech/Ultrasonic-HC-SR04
#include // 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_HC_SR04_Echo_PIN_D17 = 17;
/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t pot1_Potentiometer_Vout_PIN_D4 = 4;
const uint8_t pot2_Potentiometer_Vout_PIN_D13 = 13;
const uint8_t pot3_Potentiometer_Vout_PIN_D14 = 14;
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t ultrasonic_HC_SR04_Trigger_PIN_D16 = 16;
/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool ultrasonic_HC_SR04_Trigger_PIN_D16_rawData = 0;
/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float ultrasonic_HC_SR04_Trigger_PIN_D16_phyData = 0.0;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Ultrasonic ultrasonic(ultrasonic_HC_SR04_Trigger_PIN_D16, ultrasonic_HC_SR04_Echo_PIN_D17); // Create the ultrasonic object
MIDI_CREATE_DEFAULT_INSTANCE(); // Create the MIDI object
void setup(void) {
// put your setup code here, to run once:
pinMode(ultrasonic_HC_SR04_Echo_PIN_D17, INPUT);
pinMode(pot1_Potentiometer_Vout_PIN_D4, INPUT);
pinMode(pot2_Potentiometer_Vout_PIN_D13, INPUT);
pinMode(pot3_Potentiometer_Vout_PIN_D14, INPUT);
pinMode(ultrasonic_HC_SR04_Trigger_PIN_D16, OUTPUT);
Serial.begin(9600); // Initialize serial communication
MIDI.begin(4); // Initialize MIDI and listen to channel 4
}
void loop(void) {
// put your main code here, to run repeatedly:
updateOutputs(); // Refresh output data
// Read potentiometer values and send MIDI messages
int pot1Value = analogRead(pot1_Potentiometer_Vout_PIN_D4);
int pot2Value = analogRead(pot2_Potentiometer_Vout_PIN_D13);
int pot3Value = analogRead(pot3_Potentiometer_Vout_PIN_D14);
MIDI.sendControlChange(1, map(pot1Value, 0, 1023, 0, 127), 1); // Send MIDI CC for pot1
MIDI.sendControlChange(2, map(pot2Value, 0, 1023, 0, 127), 1); // Send MIDI CC for pot2
MIDI.sendControlChange(3, map(pot3Value, 0, 1023, 0, 127), 1); // Send MIDI CC for pot3
// Read ultrasonic sensor and send MIDI messages
float distance = ultrasonic.read(); // Get the distance in cm
if (distance >= 5 && distance <= 40) {
int midiValue = map(distance, 5, 40, 0, 127);
MIDI.sendControlChange(4, midiValue, 1); // Send MIDI CC for ultrasonic sensor
}
delay(100); // Delay for 100ms to avoid flooding the MIDI messages
}
void updateOutputs() {
ultrasonic_HC_SR04_Trigger_PIN_D16_phyData = ultrasonic.read(); // Get the distance in cm
Serial.print("Distance in CM: ");
Serial.println(ultrasonic_HC_SR04_Trigger_PIN_D16_phyData);
digitalWrite(ultrasonic_HC_SR04_Trigger_PIN_D16, ultrasonic_HC_SR04_Trigger_PIN_D16_rawData);
delay(1000); // Delay for 1 second
}
/* END CODE */