CCCPaste

"MIDI Sensors" rev_02

/********* 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:04:15

********* 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/SpulberGeorge/EasyUltrasonic
#include  //https://github.com/FortySevenEffects/arduino_midi_library

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void BlinkLed(byte num);

/***** 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;
const uint8_t LED_PIN = 13; // Define the LED pin

/***** 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*****/
EasyUltrasonic ultrasonic; // Create the ultrasonic object
MIDI_CREATE_DEFAULT_INSTANCE(); // Create the default MIDI instance

void setup(void)
{
    // put your setup code here, to run once:
    Serial.begin(9600); // Initialize serial communication

    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);
    pinMode(LED_PIN, OUTPUT); // Initialize LED pin as output

    // Attach the ultrasonic sensor to the specified pins
    ultrasonic.attach(ultrasonic_HC_SR04_Trigger_PIN_D16, ultrasonic_HC_SR04_Echo_PIN_D17);

    // Initialize MIDI communication
    MIDI.begin();
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    updateOutputs(); // Refresh output data

    // Read the distance in centimeters
    float distanceCM = ultrasonic.getDistanceCM();

    // Print the distance value to Serial Monitor
    Serial.print("Distance in CM: ");
    Serial.println(distanceCM);

    // Send MIDI message based on the distance
    if (distanceCM >= 5 && distanceCM <= 40) {
        MIDI.sendControlChange(1, map(distanceCM, 5, 40, 0, 127), 1);
    }

    // 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(2, map(pot1Value, 0, 4095, 0, 127), 1);
    MIDI.sendControlChange(3, map(pot2Value, 0, 4095, 0, 127), 1);
    MIDI.sendControlChange(4, map(pot3Value, 0, 4095, 0, 127), 1);

    // Check for MIDI messages
    if (MIDI.read() && MIDI.getType() == midi::ProgramChange) {
        BlinkLed(MIDI.getData1());
    }

    delay(1000); // Delay for 1 second
}

void updateOutputs()
{
    digitalWrite(ultrasonic_HC_SR04_Trigger_PIN_D16, ultrasonic_HC_SR04_Trigger_PIN_D16_rawData);
}

void BlinkLed(byte num) {
    for (byte i = 0; i < num; i++) {
        digitalWrite(LED_PIN, HIGH);
        delay(50);
        digitalWrite(LED_PIN, LOW);
        delay(50);
    }
}

/* END CODE */