/********* 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:09:11

********* 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 <Ultrasonic.h>  //https://github.com/JRodrigoTech/Ultrasonic-HC-SR04
#include <MIDI.h>  //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:
  Serial.begin(9600);  // Initialize serial communication

  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);

  // 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.read(CM);

  // Print the distance value in 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);  // Send MIDI CC message
  }

  // 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, 1023, 0, 127), 1);  // Send MIDI CC message for pot1
  MIDI.sendControlChange(3, map(pot2Value, 0, 1023, 0, 127), 1);  // Send MIDI CC message for pot2
  MIDI.sendControlChange(4, map(pot3Value, 0, 1023, 0, 127), 1);  // Send MIDI CC message for pot3

  delay(100);  // Delay for 100 milliseconds
}

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

/* END CODE */