Advertisement
pleasedontcode

"MIDI Sensors" rev_08

Jun 6th, 2024
344
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:20:14
  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.   // put your setup code here, to run once:
  59.  
  60.   pinMode(ultrasonic_HC_SR04_Echo_PIN_D17, INPUT);
  61.   pinMode(pot1_Potentiometer_Vout_PIN_D4, INPUT);
  62.   pinMode(pot2_Potentiometer_Vout_PIN_D13, INPUT);
  63.   pinMode(pot3_Potentiometer_Vout_PIN_D14, INPUT);
  64.  
  65.   pinMode(ultrasonic_HC_SR04_Trigger_PIN_D16, OUTPUT);
  66.  
  67.   Serial.begin(9600);  // Initialize serial communication
  68.  
  69.   MIDI.begin(4);  // Initialize MIDI and listen to channel 4
  70. }
  71.  
  72. void loop(void) {
  73.   // put your main code here, to run repeatedly:
  74.  
  75.   updateOutputs();  // Refresh output data
  76.  
  77.   // Read potentiometer values and send MIDI messages
  78.   int pot1Value = analogRead(pot1_Potentiometer_Vout_PIN_D4);
  79.   int pot2Value = analogRead(pot2_Potentiometer_Vout_PIN_D13);
  80.   int pot3Value = analogRead(pot3_Potentiometer_Vout_PIN_D14);
  81.  
  82.   MIDI.sendControlChange(1, map(pot1Value, 0, 1023, 0, 127), 1);  // Send MIDI CC for pot1
  83.   MIDI.sendControlChange(2, map(pot2Value, 0, 1023, 0, 127), 1);  // Send MIDI CC for pot2
  84.   MIDI.sendControlChange(3, map(pot3Value, 0, 1023, 0, 127), 1);  // Send MIDI CC for pot3
  85.  
  86.   // Read ultrasonic sensor and send MIDI messages
  87.   float distance = ultrasonic.read();  // Get the distance in cm
  88.   if (distance >= 5 && distance <= 40) {
  89.     int midiValue = map(distance, 5, 40, 0, 127);
  90.     MIDI.sendControlChange(4, midiValue, 1);  // Send MIDI CC for ultrasonic sensor
  91.   }
  92.  
  93.   delay(100);  // Delay for 100ms to avoid flooding the MIDI messages
  94. }
  95.  
  96. void updateOutputs() {
  97.   ultrasonic_HC_SR04_Trigger_PIN_D16_phyData = ultrasonic.read();  // Get the distance in cm
  98.   Serial.print("Distance in CM: ");
  99.   Serial.println(ultrasonic_HC_SR04_Trigger_PIN_D16_phyData);
  100.  
  101.   digitalWrite(ultrasonic_HC_SR04_Trigger_PIN_D16, ultrasonic_HC_SR04_Trigger_PIN_D16_rawData);
  102.   delay(1000);  // Delay for 1 second
  103. }
  104.  
  105. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement