CCCPaste

"Ultrasonic MIDI" rev_01

/********* 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: "Ultrasonic MIDI"
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-06-06 21:58:54

********* 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/ErickSimoes/Ultrasonic
#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);  // Initialize Ultrasonic object
MIDI_CREATE_DEFAULT_INSTANCE();  // Initialize MIDI object

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

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

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

  // Read distance from Ultrasonic sensor
  int distance = ultrasonic.read();
  Serial.print("Distance in CM: ");
  Serial.println(distance);

  // Send MIDI message if distance is between 5 cm and 40 cm
  if (distance >= 5 && distance <= 40) {
    MIDI.sendControlChange(1, map(distance, 5, 40, 0, 127), 1);  // Send MIDI Control Change 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 Control Change message for pot1
  MIDI.sendControlChange(3, map(pot2Value, 0, 1023, 0, 127), 1);  // Send MIDI Control Change message for pot2
  MIDI.sendControlChange(4, map(pot3Value, 0, 1023, 0, 127), 1);  // Send MIDI Control Change message for pot3

  // Check for incoming MIDI messages
  if (MIDI.read()) {
    // Handle MIDI messages here
    if (MIDI.getType() == midi::NoteOn) {
      Serial.print("Note On, pitch: ");
      Serial.print(MIDI.getData1());
      Serial.print(", velocity: ");
      Serial.println(MIDI.getData2());
    } else if (MIDI.getType() == midi::NoteOff) {
      Serial.print("Note Off, pitch: ");
      Serial.print(MIDI.getData1());
      Serial.print(", velocity: ");
      Serial.println(MIDI.getData2());
    }
  }

  delay(100);  // Wait for 100 ms before next reading
}

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

/* END CODE */