Advertisement
pleasedontcode

"MIDI Sensors" rev_03

Jun 6th, 2024
316
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:09:11
  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/JRodrigoTech/Ultrasonic-HC-SR04
  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);  // Create the ultrasonic object
  55. MIDI_CREATE_DEFAULT_INSTANCE();  // Create the 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(pot1_Potentiometer_Vout_PIN_D4, INPUT);
  63.   pinMode(pot2_Potentiometer_Vout_PIN_D13, INPUT);
  64.   pinMode(pot3_Potentiometer_Vout_PIN_D14, INPUT);
  65.  
  66.   pinMode(ultrasonic_HC_SR04_Trigger_PIN_D16, OUTPUT);
  67.  
  68.   // Initialize MIDI communication
  69.   MIDI.begin();
  70. }
  71.  
  72. void loop(void)
  73. {
  74.   // put your main code here, to run repeatedly:
  75.   updateOutputs();  // Refresh output data
  76.  
  77.   // Read the distance in centimeters
  78.   float distanceCM = ultrasonic.read(CM);
  79.  
  80.   // Print the distance value in Serial Monitor
  81.   Serial.print("Distance in CM: ");
  82.   Serial.println(distanceCM);
  83.  
  84.   // Send MIDI message based on the distance
  85.   if (distanceCM >= 5 && distanceCM <= 40) {
  86.     MIDI.sendControlChange(1, map(distanceCM, 5, 40, 0, 127), 1);  // Send MIDI CC message
  87.   }
  88.  
  89.   // Read potentiometer values and send MIDI messages
  90.   int pot1Value = analogRead(pot1_Potentiometer_Vout_PIN_D4);
  91.   int pot2Value = analogRead(pot2_Potentiometer_Vout_PIN_D13);
  92.   int pot3Value = analogRead(pot3_Potentiometer_Vout_PIN_D14);
  93.  
  94.   MIDI.sendControlChange(2, map(pot1Value, 0, 1023, 0, 127), 1);  // Send MIDI CC message for pot1
  95.   MIDI.sendControlChange(3, map(pot2Value, 0, 1023, 0, 127), 1);  // Send MIDI CC message for pot2
  96.   MIDI.sendControlChange(4, map(pot3Value, 0, 1023, 0, 127), 1);  // Send MIDI CC message for pot3
  97.  
  98.   delay(100);  // Delay for 100 milliseconds
  99. }
  100.  
  101. void updateOutputs()
  102. {
  103.   digitalWrite(ultrasonic_HC_SR04_Trigger_PIN_D16, ultrasonic_HC_SR04_Trigger_PIN_D16_rawData);
  104. }
  105.  
  106. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement