pleasedontcode

MIDI Controller rev_12

Jul 15th, 2025
171
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 Controller
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-07-15 22:31:09
  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. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Ultrasonic.h> // https://github.com/ErickSimoes/Ultrasonic
  29. #include <MIDI.h>       // https://github.com/FortySevenEffects/arduino_midi_library
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void sendPotMidiMessages();
  35. void sendUltrasonicMidiMessage();
  36.  
  37. //***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t ultrasonic_Echo_PIN_TX2 = 13; // Echo pin
  39. const uint8_t ultrasonic_Trigger_PIN_RX2 = 12; // Trigger pin
  40.  
  41. /***** DEFINITION OF ANALOG INPUT PINS *****/
  42. const uint8_t pot1_Vout_PIN_D4 = 4;   // Potentiometer 1
  43. const uint8_t pot2_Vout_PIN_D13 = 13; // Potentiometer 2
  44. const uint8_t pot3_Vout_PIN_D14 = 14; // Potentiometer 3
  45.  
  46. /***** MIDI Control Change Numbers for potentiometers *****/
  47. const uint8_t midiCCPot1 = 20;
  48. const uint8_t midiCCPot2 = 21;
  49. const uint8_t midiCCPot3 = 22;
  50.  
  51. /***** DEFINITION OF ULTRASONIC SENSOR INSTANCE *****/
  52. Ultrasonic ultrasonic(ultrasonic_Trigger_PIN_RX2, ultrasonic_Echo_PIN_TX2);
  53.  
  54. /***** VARIABLES TO STORE POTENTIOMETER VALUES *****/
  55. int pot1Value = 0;
  56. int pot2Value = 0;
  57. int pot3Value = 0;
  58.  
  59. /***** VARIABLES TO STORE ULTRASONIC SENSOR DATA *****/
  60. float ultrasonicDistance = 0.0;
  61.  
  62. /***** SYSTEM REQUIREMENTS *****/
  63. // Each potentiometer send midi messages through USB
  64. // The ultrasensor send midi message too. The ultrasensor work between 5 cm and 40 cm
  65.  
  66. void setup(void)
  67. {
  68.   // Initialize pins
  69.   pinMode(ultrasonic_Echo_PIN_TX2, INPUT);
  70.   pinMode(pot1_Vout_PIN_D4, INPUT);
  71.   pinMode(pot2_Vout_PIN_D13, INPUT);
  72.   pinMode(pot3_Vout_PIN_D14, INPUT);
  73.   pinMode(ultrasonic_Trigger_PIN_RX2, OUTPUT);
  74.  
  75.   // Initialize MIDI
  76.   MIDI.begin(MIDI_CHANNEL_OMNI);
  77. }
  78.  
  79. void loop(void)
  80. {
  81.   // Read potentiometers
  82.   pot1Value = analogRead(pot1_Vout_PIN_D4);
  83.   pot2Value = analogRead(pot2_Vout_PIN_D13);
  84.   pot3Value = analogRead(pot3_Vout_PIN_D14);
  85.  
  86.   // Send MIDI messages for potentiometers
  87.   sendPotMidiMessages();
  88.  
  89.   // Read ultrasonic sensor distance in centimeters
  90.   ultrasonicDistance = ultrasonic.read();
  91.  
  92.   // Send MIDI message if within range 5cm to 40cm
  93.   if (ultrasonicDistance >= 5.0 && ultrasonicDistance <= 40.0)
  94.   {
  95.     sendUltrasonicMidiMessage();
  96.   }
  97.  
  98.   // Refresh output data (if needed)
  99.   updateOutputs();
  100.  
  101.   delay(50); // Small delay for stability
  102. }
  103.  
  104. void sendPotMidiMessages()
  105. {
  106.   // Map potentiometer values (0-1023) to MIDI CC value (0-127)
  107.   uint8_t midiValue1 = map(pot1Value, 0, 1023, 0, 127);
  108.   uint8_t midiValue2 = map(pot2Value, 0, 1023, 0, 127);
  109.   uint8_t midiValue3 = map(pot3Value, 0, 1023, 0, 127);
  110.  
  111.   // Send MIDI CC messages
  112.   MIDI.sendControlChange(midiCCPot1, midiValue1, 1);
  113.   MIDI.sendControlChange(midiCCPot2, midiValue2, 1);
  114.   MIDI.sendControlChange(midiCCPot3, midiValue3, 1);
  115. }
  116.  
  117. void sendUltrasonicMidiMessage()
  118. {
  119.   // Map ultrasonic distance (5-40cm) to MIDI CC (0-127)
  120.   uint8_t midiValue = map(ultrasonicDistance, 5, 40, 0, 127);
  121.   // Send MIDI CC message for ultrasonic sensor
  122.   MIDI.sendControlChange(23, midiValue, 1);
  123. }
  124.  
  125. void updateOutputs()
  126. {
  127.   // Update trigger pin state based on ultrasonic sensor data if needed
  128.   // For example, trigger a pulse when within range
  129.   static bool triggerState = false;
  130.   static unsigned long lastTriggerTime = 0;
  131.   const unsigned long triggerInterval = 100; // ms
  132.  
  133.   if (millis() - lastTriggerTime > triggerInterval)
  134.   {
  135.     triggerState = !triggerState;
  136.     digitalWrite(ultrasonic_Trigger_PIN_RX2, triggerState);
  137.     lastTriggerTime = millis();
  138.   }
  139. }
  140.  
  141. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment