pleasedontcode

3POTlaserMIDI rev_10

Jul 15th, 2025
185
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:
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-07-15 22:06:04
  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 sendPotMidiMessage(int potValue, uint8_t midiCC);
  35. void sendUltrasonicMidiMessage(float distance);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t ultrasonic_Echo_PIN_TX2 = 13; // Example pin, replace with actual
  39. const uint8_t ultrasonic_Trigger_PIN_RX2 = 12; // Example pin, replace with actual
  40.  
  41. /***** DEFINITION OF ANALOG INPUT PINS *****/
  42. const uint8_t pot1_Vout_PIN_D4 = 34; // Example ADC pin, replace with actual
  43. const uint8_t pot2_Vout_PIN_D13 = 35; // Example ADC pin, replace with actual
  44. const uint8_t pot3_Vout_PIN_D14 = 36; // Example ADC pin, replace with actual
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. bool ultrasonicTriggerRawData = false;
  48.  
  49. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  50. float ultrasonicDistance = 0.0;
  51.  
  52. /****** LIBRARIES CLASS INSTANCES *****/
  53. Ultrasonic ultrasonicSensor(ultrasonic_Trigger_PIN_RX2, ultrasonic_Echo_PIN_TX2);
  54. MIDI_CREATE_DEFAULT_INSTANCE();
  55.  
  56. void setup(void)
  57. {
  58.   // Initialize pins
  59.   pinMode(ultrasonic_Echo_PIN_TX2, INPUT);
  60.   pinMode(ultrasonic_Trigger_PIN_RX2, OUTPUT);
  61.   pinMode(pot1_Vout_PIN_D4, INPUT);
  62.   pinMode(pot2_Vout_PIN_D13, INPUT);
  63.   pinMode(pot3_Vout_PIN_D14, INPUT);
  64.  
  65.   // Initialize MIDI
  66.   MIDI.begin(MIDI_CHANNEL_OMNI);
  67. }
  68.  
  69. void loop(void)
  70. {
  71.   // Read potentiometers (0-4095), map to 0-127 for MIDI CC
  72.   int pot1Value = analogRead(pot1_Vout_PIN_D4);
  73.   int pot2Value = analogRead(pot2_Vout_PIN_D13);
  74.   int pot3Value = analogRead(pot3_Vout_PIN_D14);
  75.  
  76.   // Send MIDI messages for potentiometers
  77.   sendPotMidiMessage(pot1Value, 1); // CC1
  78.   sendPotMidiMessage(pot2Value, 2); // CC2
  79.   sendPotMidiMessage(pot3Value, 3); // CC3
  80.  
  81.   // Read ultrasonic sensor distance
  82.   ultrasonicDistance = ultrasonicSensor.read();
  83.  
  84.   // Check if distance is within 5cm to 40cm
  85.   if (ultrasonicDistance >= 5 && ultrasonicDistance <= 40)
  86.   {
  87.     sendUltrasonicMidiMessage(ultrasonicDistance);
  88.   }
  89.  
  90.   delay(100); // Small delay for stability
  91. }
  92.  
  93. void sendPotMidiMessage(int potValue, uint8_t midiCC)
  94. {
  95.   // Map potentiometer value (0-4095) to MIDI CC value (0-127)
  96.   int midiValue = map(potValue, 0, 4095, 0, 127);
  97.   MIDI.sendControlChange(midiCC, midiValue, 1); // Send on channel 1
  98. }
  99.  
  100. void sendUltrasonicMidiMessage(float distance)
  101. {
  102.   // Map distance (5-40cm) to MIDI CC value (0-127)
  103.   int midiValue = map(distance, 5, 40, 0, 127);
  104.   MIDI.sendControlChange(4, midiValue, 1); // Use CC4 for ultrasonic sensor
  105. }
  106.  
  107. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment