|
| 1 | +/* |
| 2 | +This is an example of a Simple MIDI Controller using an ESP32 with a native USB support stack (S2, S3, |
| 3 | +etc.). |
| 4 | +
|
| 5 | +For a hookup guide and more information on reading the ADC, please see: |
| 6 | +https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/ (Note: This sketch uses GPIO05) |
| 7 | +
|
| 8 | +For best results, it is recommended to add an extra offset resistor between VCC and the potentiometer. |
| 9 | +(For a standard 10kOhm potentiometer, 3kOhm - 4kOhm will do.) |
| 10 | +
|
| 11 | +View this sketch in action on YouTube: https://youtu.be/Y9TLXs_3w1M |
| 12 | +*/ |
| 13 | +#if ARDUINO_USB_MODE |
| 14 | +#warning This sketch should be used when USB is in OTG mode |
| 15 | +void setup() {} |
| 16 | +void loop() {} |
| 17 | +#else |
| 18 | + |
| 19 | +#include <math.h> |
| 20 | + |
| 21 | +#include "USB.h" |
| 22 | +#include "USBMIDI.h" |
| 23 | +USBMIDI MIDI; |
| 24 | + |
| 25 | +#define MIDI_NOTE_C4 64 |
| 26 | + |
| 27 | +#define MIDI_CC_CUTOFF 74 |
| 28 | + |
| 29 | +///// ADC & Controller Input Handling ///// |
| 30 | + |
| 31 | +#define CONTROLLER_PIN 5 |
| 32 | + |
| 33 | +// ESP32 ADC needs a ton of smoothing |
| 34 | +#define SMOOTHING_VALUE 1000 |
| 35 | +static double controllerInputValue = 0; |
| 36 | + |
| 37 | +void updateControllerInputValue() { |
| 38 | + controllerInputValue = |
| 39 | + (controllerInputValue * (SMOOTHING_VALUE - 1) + analogRead(CONTROLLER_PIN)) / SMOOTHING_VALUE; |
| 40 | +} |
| 41 | + |
| 42 | +void primeControllerInputValue() { |
| 43 | + for (int i = 0; i < SMOOTHING_VALUE; i++) { |
| 44 | + updateControllerInputValue(); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +uint16_t readControllerValue() { |
| 49 | + // Lower ADC input amplitude to get a stable value |
| 50 | + return round(controllerInputValue / 12); |
| 51 | +} |
| 52 | + |
| 53 | +///// Button Handling ///// |
| 54 | + |
| 55 | +#define BUTTON_PIN 0 |
| 56 | + |
| 57 | +// Simple button state transition function with debounce |
| 58 | +// (See also: https://tinyurl.com/simple-debounce) |
| 59 | +#define PRESSED 0xff00 |
| 60 | +#define RELEASED 0xfe1f |
| 61 | +uint16_t getButtonEvent() { |
| 62 | + static uint16_t state = 0; |
| 63 | + state = (state << 1) | digitalRead(BUTTON_PIN) | 0xfe00; |
| 64 | + return state; |
| 65 | +} |
| 66 | + |
| 67 | +///// Arduino Hooks ///// |
| 68 | + |
| 69 | +void setup() { |
| 70 | + Serial.begin(115200); |
| 71 | + MIDI.begin(); |
| 72 | + USB.begin(); |
| 73 | + |
| 74 | + primeControllerInputValue(); |
| 75 | +} |
| 76 | + |
| 77 | +void loop() { |
| 78 | + uint16_t newControllerValue = readControllerValue(); |
| 79 | + static uint16_t lastControllerValue = 0; |
| 80 | + |
| 81 | + // Auto-calibrate the controller range |
| 82 | + static uint16_t maxControllerValue = 0; |
| 83 | + static uint16_t minControllerValue = 0xFFFF; |
| 84 | + |
| 85 | + if (newControllerValue < minControllerValue) { |
| 86 | + minControllerValue = newControllerValue; |
| 87 | + } |
| 88 | + if (newControllerValue > maxControllerValue) { |
| 89 | + maxControllerValue = newControllerValue; |
| 90 | + } |
| 91 | + |
| 92 | + // Send update if the controller value has changed |
| 93 | + if (lastControllerValue != newControllerValue) { |
| 94 | + lastControllerValue = newControllerValue; |
| 95 | + |
| 96 | + // Can't map if the range is zero |
| 97 | + if (minControllerValue != maxControllerValue) { |
| 98 | + MIDI.controlChange(MIDI_CC_CUTOFF, |
| 99 | + map(newControllerValue, minControllerValue, maxControllerValue, 0, 127)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + updateControllerInputValue(); |
| 104 | + |
| 105 | + // Hook Button0 to a MIDI note so that we can observe |
| 106 | + // the CC effect without the need for a MIDI keyboard. |
| 107 | + switch (getButtonEvent()) { |
| 108 | + case PRESSED: |
| 109 | + MIDI.noteOn(MIDI_NOTE_C4, 64); |
| 110 | + break; |
| 111 | + case RELEASED: |
| 112 | + MIDI.noteOff(MIDI_NOTE_C4, 0); |
| 113 | + break; |
| 114 | + default: |
| 115 | + break; |
| 116 | + } |
| 117 | +} |
| 118 | +#endif /* ARDUINO_USB_MODE */ |
0 commit comments