Advertisement
pleasedontcode

"MIDI Sensors" rev_02

Jun 6th, 2024
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "MIDI Sensors"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-06 22:04:15
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Each potentiometer send midi messages through USB */
  21.     /* The ultrasensor send midi message too. The */
  22.     /* ultrasensor work between 5 cm and 40 cm */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <EasyUltrasonic.h> //https://github.com/SpulberGeorge/EasyUltrasonic
  27. #include <MIDI.h> //https://github.com/FortySevenEffects/arduino_midi_library
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33. void BlinkLed(byte num);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t ultrasonic_HC_SR04_Echo_PIN_D17 = 17;
  37.  
  38. /***** DEFINITION OF ANALOG INPUT PINS *****/
  39. const uint8_t pot1_Potentiometer_Vout_PIN_D4 = 4;
  40. const uint8_t pot2_Potentiometer_Vout_PIN_D13 = 13;
  41. const uint8_t pot3_Potentiometer_Vout_PIN_D14 = 14;
  42.  
  43. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  44. const uint8_t ultrasonic_HC_SR04_Trigger_PIN_D16 = 16;
  45. const uint8_t LED_PIN = 13; // Define the LED pin
  46.  
  47. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  48. /***** used to store raw data *****/
  49. bool ultrasonic_HC_SR04_Trigger_PIN_D16_rawData = 0;
  50.  
  51. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  52. /***** used to store data after characteristic curve transformation *****/
  53. float ultrasonic_HC_SR04_Trigger_PIN_D16_phyData = 0.0;
  54.  
  55. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  56. EasyUltrasonic ultrasonic; // Create the ultrasonic object
  57. MIDI_CREATE_DEFAULT_INSTANCE(); // Create the default MIDI instance
  58.  
  59. void setup(void)
  60. {
  61.     // put your setup code here, to run once:
  62.     Serial.begin(9600); // Initialize serial communication
  63.  
  64.     pinMode(ultrasonic_HC_SR04_Echo_PIN_D17, INPUT);
  65.     pinMode(pot1_Potentiometer_Vout_PIN_D4, INPUT);
  66.     pinMode(pot2_Potentiometer_Vout_PIN_D13, INPUT);
  67.     pinMode(pot3_Potentiometer_Vout_PIN_D14, INPUT);
  68.  
  69.     pinMode(ultrasonic_HC_SR04_Trigger_PIN_D16, OUTPUT);
  70.     pinMode(LED_PIN, OUTPUT); // Initialize LED pin as output
  71.  
  72.     // Attach the ultrasonic sensor to the specified pins
  73.     ultrasonic.attach(ultrasonic_HC_SR04_Trigger_PIN_D16, ultrasonic_HC_SR04_Echo_PIN_D17);
  74.  
  75.     // Initialize MIDI communication
  76.     MIDI.begin();
  77. }
  78.  
  79. void loop(void)
  80. {
  81.     // put your main code here, to run repeatedly:
  82.     updateOutputs(); // Refresh output data
  83.  
  84.     // Read the distance in centimeters
  85.     float distanceCM = ultrasonic.getDistanceCM();
  86.  
  87.     // Print the distance value to Serial Monitor
  88.     Serial.print("Distance in CM: ");
  89.     Serial.println(distanceCM);
  90.  
  91.     // Send MIDI message based on the distance
  92.     if (distanceCM >= 5 && distanceCM <= 40) {
  93.         MIDI.sendControlChange(1, map(distanceCM, 5, 40, 0, 127), 1);
  94.     }
  95.  
  96.     // Read potentiometer values and send MIDI messages
  97.     int pot1Value = analogRead(pot1_Potentiometer_Vout_PIN_D4);
  98.     int pot2Value = analogRead(pot2_Potentiometer_Vout_PIN_D13);
  99.     int pot3Value = analogRead(pot3_Potentiometer_Vout_PIN_D14);
  100.  
  101.     MIDI.sendControlChange(2, map(pot1Value, 0, 4095, 0, 127), 1);
  102.     MIDI.sendControlChange(3, map(pot2Value, 0, 4095, 0, 127), 1);
  103.     MIDI.sendControlChange(4, map(pot3Value, 0, 4095, 0, 127), 1);
  104.  
  105.     // Check for MIDI messages
  106.     if (MIDI.read() && MIDI.getType() == midi::ProgramChange) {
  107.         BlinkLed(MIDI.getData1());
  108.     }
  109.  
  110.     delay(1000); // Delay for 1 second
  111. }
  112.  
  113. void updateOutputs()
  114. {
  115.     digitalWrite(ultrasonic_HC_SR04_Trigger_PIN_D16, ultrasonic_HC_SR04_Trigger_PIN_D16_rawData);
  116. }
  117.  
  118. void BlinkLed(byte num) {
  119.     for (byte i = 0; i < num; i++) {
  120.         digitalWrite(LED_PIN, HIGH);
  121.         delay(50);
  122.         digitalWrite(LED_PIN, LOW);
  123.         delay(50);
  124.     }
  125. }
  126.  
  127. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement