Advertisement
pleasedontcode

"Ultrasonic MIDI" rev_01

Jun 6th, 2024
331
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: "Ultrasonic MIDI"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-06 21:58:54
  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 <Ultrasonic.h>  //https://github.com/ErickSimoes/Ultrasonic
  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.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t ultrasonic_HC_SR04_Echo_PIN_D17 = 17;
  36.  
  37. /***** DEFINITION OF ANALOG INPUT PINS *****/
  38. const uint8_t pot1_Potentiometer_Vout_PIN_D4 = 4;
  39. const uint8_t pot2_Potentiometer_Vout_PIN_D13 = 13;
  40. const uint8_t pot3_Potentiometer_Vout_PIN_D14 = 14;
  41.  
  42. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  43. const uint8_t ultrasonic_HC_SR04_Trigger_PIN_D16 = 16;
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. /***** used to store raw data *****/
  47. bool ultrasonic_HC_SR04_Trigger_PIN_D16_rawData = 0;
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. /***** used to store data after characteristic curve transformation *****/
  51. float ultrasonic_HC_SR04_Trigger_PIN_D16_phyData = 0.0;
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  54. Ultrasonic ultrasonic(ultrasonic_HC_SR04_Trigger_PIN_D16, ultrasonic_HC_SR04_Echo_PIN_D17);  // Initialize Ultrasonic object
  55. MIDI_CREATE_DEFAULT_INSTANCE();  // Initialize MIDI object
  56.  
  57. void setup(void)
  58. {
  59.   // put your setup code here, to run once:
  60.   Serial.begin(9600);  // Initialize serial communication
  61.  
  62.   pinMode(ultrasonic_HC_SR04_Echo_PIN_D17, INPUT);
  63.   pinMode(pot1_Potentiometer_Vout_PIN_D4, INPUT);
  64.   pinMode(pot2_Potentiometer_Vout_PIN_D13, INPUT);
  65.   pinMode(pot3_Potentiometer_Vout_PIN_D14, INPUT);
  66.  
  67.   pinMode(ultrasonic_HC_SR04_Trigger_PIN_D16, OUTPUT);
  68.  
  69.   MIDI.begin();  // Initialize MIDI communication
  70. }
  71.  
  72. void loop(void)
  73. {
  74.   // put your main code here, to run repeatedly:
  75.   updateOutputs();  // Refresh output data
  76.  
  77.   // Read distance from Ultrasonic sensor
  78.   int distance = ultrasonic.read();
  79.   Serial.print("Distance in CM: ");
  80.   Serial.println(distance);
  81.  
  82.   // Send MIDI message if distance is between 5 cm and 40 cm
  83.   if (distance >= 5 && distance <= 40) {
  84.     MIDI.sendControlChange(1, map(distance, 5, 40, 0, 127), 1);  // Send MIDI Control Change message
  85.   }
  86.  
  87.   // Read potentiometer values and send MIDI messages
  88.   int pot1Value = analogRead(pot1_Potentiometer_Vout_PIN_D4);
  89.   int pot2Value = analogRead(pot2_Potentiometer_Vout_PIN_D13);
  90.   int pot3Value = analogRead(pot3_Potentiometer_Vout_PIN_D14);
  91.  
  92.   MIDI.sendControlChange(2, map(pot1Value, 0, 1023, 0, 127), 1);  // Send MIDI Control Change message for pot1
  93.   MIDI.sendControlChange(3, map(pot2Value, 0, 1023, 0, 127), 1);  // Send MIDI Control Change message for pot2
  94.   MIDI.sendControlChange(4, map(pot3Value, 0, 1023, 0, 127), 1);  // Send MIDI Control Change message for pot3
  95.  
  96.   // Check for incoming MIDI messages
  97.   if (MIDI.read()) {
  98.     // Handle MIDI messages here
  99.     if (MIDI.getType() == midi::NoteOn) {
  100.       Serial.print("Note On, pitch: ");
  101.       Serial.print(MIDI.getData1());
  102.       Serial.print(", velocity: ");
  103.       Serial.println(MIDI.getData2());
  104.     } else if (MIDI.getType() == midi::NoteOff) {
  105.       Serial.print("Note Off, pitch: ");
  106.       Serial.print(MIDI.getData1());
  107.       Serial.print(", velocity: ");
  108.       Serial.println(MIDI.getData2());
  109.     }
  110.   }
  111.  
  112.   delay(100);  // Wait for 100 ms before next reading
  113. }
  114.  
  115. void updateOutputs()
  116. {
  117.   digitalWrite(ultrasonic_HC_SR04_Trigger_PIN_D16, ultrasonic_HC_SR04_Trigger_PIN_D16_rawData);
  118. }
  119.  
  120. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement