-
Notifications
You must be signed in to change notification settings - Fork 0
/
midicontroller.ino
66 lines (53 loc) · 2.36 KB
/
midicontroller.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Basic MIDI Controller
* This code transforms a Teensy microcontroller into a MIDI controller with 10 potentiometers.
* Each potentiometer can send MIDI Control Change messages to any MIDI-compatible software or hardware.
* Designed for real-time video/music performance and production.
*
* Author: Santiago Torres - https://github.com/ethx42
*/
const int numPots = 10; // Total number of potentiometers.
int potPin[numPots] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9};
int potCState[numPots] = {0}; // Holds current potentiometer readings.
int potPState[numPots] = {0}; // Holds previous potentiometer readings.
int potVar = 5; // Sensitivity for detecting potentiometer changes.
// MIDI Control Change (CC) numbers for each potentiometer.
int midiCC[numPots] = {1, 7, 10, 11, 71, 74, 77, 80, 83, 86};
const int channel = 5; // MIDI channel for sending messages.
// LED Blink variables
const int ledPin = LED_BUILTIN; // Use Teensy's built-in LED
const long ledOnDuration = 10; // Duration LED stays on (milliseconds)
unsigned long previousBlinkMillis = 0; // Stores last time LED was updated
const long blinkInterval = 2500; // Interval at which to blink (milliseconds)
void setup() {
for (int i = 0; i < numPots; i++) {
pinMode(potPin[i], INPUT);
}
pinMode(ledPin, OUTPUT); // Initialize the LED pin as an output
Serial.begin(9600); // For debugging.
}
void loop() {
unsigned long currentMillis = millis();
// Blink LED for a brief moment every 750ms
if (currentMillis - previousBlinkMillis >= blinkInterval) {
previousBlinkMillis = currentMillis; // Save the last time you blinked the LED
digitalWrite(ledPin, HIGH);
} else if (currentMillis - previousBlinkMillis >= ledOnDuration) {
digitalWrite(ledPin, LOW);
}
for (int i = 0; i < numPots; i++) {
potCState[i] = analogRead(potPin[i]) >> 3; // Convert 0-1023 to 0-127 range.
if (abs(potCState[i] - potPState[i]) > potVar) {
usbMIDI.sendControlChange(midiCC[i], potCState[i], channel); // Send CC message.
// Debugging output: Uncomment to track potentiometer changes.
// Serial.print("Pot ");
// Serial.print(i);
// Serial.print(": ");
// Serial.println(potCState[i]);
potPState[i] = potCState[i]; // Update previous state.
}
}
delay(10); // Maintain MIDI communication stability.
// Clear the USB MIDI read buffer.
while (usbMIDI.read());
}