pleasedontcode

Sensor Mapper rev_17

Jul 20th, 2025
373
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: Sensor Mapper
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-07-20 17:58:17
  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 channel);
  35. void sendUltrasonicMidiMessage(float distance, uint8_t channel);
  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 = 34; // ADC1_CH6
  43. const uint8_t pot2_Vout_PIN_D13 = 35; // ADC1_CH7
  44. const uint8_t pot3_Vout_PIN_D14 = 32; // ADC1_CH4
  45.  
  46. /***** DEFINITION OF MIDI CHANNELS *****/
  47. const uint8_t midiChannelPot1 = 1;
  48. const uint8_t midiChannelPot2 = 2;
  49. const uint8_t midiChannelPot3 = 3;
  50. const uint8_t midiChannelUltrasonic = 4;
  51.  
  52. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  53. /***** used to store raw data *****/
  54. int pot1RawValue = 0;
  55. int pot2RawValue = 0;
  56. int pot3RawValue = 0;
  57.  
  58. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  59. /***** used to store data after characteristic curve transformation *****/
  60. int pot1MidiValue = 0;
  61. int pot2MidiValue = 0;
  62. int pot3MidiValue = 0;
  63.  
  64. /***** ULTRASONIC SENSOR INSTANCE *****/
  65. Ultrasonic ultrasonic(ultrasonic_Trigger_PIN_RX2, ultrasonic_Echo_PIN_TX2);
  66.  
  67. /****** MIDI INSTANCE *****/
  68. MIDI_CREATE_DEFAULT_INSTANCE();
  69.  
  70. void setup(void)
  71. {
  72.   // Initialize serial communication for debugging if needed
  73.   Serial.begin(115200);
  74.  
  75.   // Initialize pins
  76.   pinMode(ultrasonic_Echo_PIN_TX2, INPUT);
  77.   pinMode(ultrasonic_Trigger_PIN_RX2, OUTPUT);
  78.   pinMode(pot1_Vout_PIN_D4, INPUT);
  79.   pinMode(pot2_Vout_PIN_D13, INPUT);
  80.   pinMode(pot3_Vout_PIN_D14, INPUT);
  81.  
  82.   // Initialize MIDI
  83.   MIDI.begin(MIDI_CHANNEL_OMNI);
  84. }
  85.  
  86. void loop(void)
  87. {
  88.   // Read potentiometers
  89.   pot1RawValue = analogRead(pot1_Vout_PIN_D4);
  90.   pot2RawValue = analogRead(pot2_Vout_PIN_D13);
  91.   pot3RawValue = analogRead(pot3_Vout_PIN_D14);
  92.  
  93.   // Map potentiometer values (0-4095) to MIDI range (0-127)
  94.   pot1MidiValue = map(pot1RawValue, 0, 4095, 0, 127);
  95.   pot2MidiValue = map(pot2RawValue, 0, 4095, 0, 127);
  96.   pot3MidiValue = map(pot3RawValue, 0, 4095, 0, 127);
  97.  
  98.   // Send MIDI messages for potentiometers
  99.   sendPotMidiMessage(pot1MidiValue, midiChannelPot1);
  100.   sendPotMidiMessage(pot2MidiValue, midiChannelPot2);
  101.   sendPotMidiMessage(pot3MidiValue, midiChannelPot3);
  102.  
  103.   // Read ultrasonic sensor distance in centimeters
  104.   float distance = ultrasonic.read();
  105.  
  106.   // Check if distance is within 5cm to 40cm
  107.   if (distance >= 5.0 && distance <= 40.0)
  108.   {
  109.     // Send MIDI message based on ultrasonic distance
  110.     sendUltrasonicMidiMessage(distance, midiChannelUltrasonic);
  111.   }
  112.  
  113.   delay(100); // Small delay for stability
  114. }
  115.  
  116. // Function to send MIDI message based on potentiometer value
  117. void sendPotMidiMessage(int potValue, uint8_t channel)
  118. {
  119.   MIDI.sendControlChange(channel, 1, potValue); // Control Change #1 (Modulation Wheel)
  120. }
  121.  
  122. // Function to send MIDI message based on ultrasonic distance
  123. void sendUltrasonicMidiMessage(float distance, uint8_t channel)
  124. {
  125.   int midiValue = map((int)distance, 5, 40, 0, 127);
  126.   MIDI.sendControlChange(channel, 2, midiValue); // Control Change #2 (Breath Controller)
  127. }
  128.  
  129. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment