Skip to content

Instantly share code, notes, and snippets.

@mzierer
Created August 1, 2017 21:19
Show Gist options
  • Save mzierer/c33c4af0cab044be53f2a3cdc4c5d57d to your computer and use it in GitHub Desktop.
Save mzierer/c33c4af0cab044be53f2a3cdc4c5d57d to your computer and use it in GitHub Desktop.
Some code for a custom MIDI controller I built
//This code is to be used with a MIDI controller I recently built.
//Pictures and info about the project: http://imgur.com/a/ngEN2
//It's a Teensy LC arduino style microcontroller working inside an old guitar distortion effect foot pedal.
//The controller features four main potentiometers to send midi CC values in two different banks that can be changed by an spdt switch
//There's also one button to send a midi note and another potentiometer on the right side to send midi CC signals
//The code and project are heavily inspired by this awesome video tutorial series: https://youtu.be/JONIZdLZAVM (in German)
//The LED pulse effect I found somewhere on the internet
//The MIDI channel to be used for all the signals
const int chn = 2;
//The pin for the Teensy's internal LED
int pin_led_internal = 13;
//The digital pins for the LED, the button and the switch
int pin_led = 9;
int pin_button = 2;
int pin_switch = 12;
int ledState = LOW;
// The period for the blinking LED
unsigned long ledPeriode = 3000;
const long interval_1 = 1000;
const long interval_2 = 500;
int sw;
int btn = HIGH;
int btnOld = HIGH;
int i = 0;
//The analog pin for the side poti
int sidePotiPin = A0;
//the MIDI controller number for the side poti
int sidePotiCont = 20;
//the MIDI controller number for the button
int btnCont = 29;
int sidePotiRead = 0;
//The four analog pins used for the four main potis
int bankPotiPin[] = {A1, A2, A3, A4};
//The MIDI controller number for the potis in bank 0 and 1
int bank0Cont[] = {21, 22, 23, 24};
int bank1Cont[] = {25, 26, 27, 28};
int bankPotiRead[] = {0, 0, 0, 0};
int bankPotiMidi[] = {0, 0, 0, 0};
int bankPotiOld[] = {0, 0, 0, 0};
int sidePotiMidi = 0;
int sidePotiOld = 0;
int smoothValue[] = {0, 0, 0, 0};
void setup() {
//Serial connection with the standard baud rate for USB MIDI
Serial.begin(31250);
//setting the pin modes for the digital input and output pins
pinMode(pin_led_internal, OUTPUT);
pinMode(pin_led, OUTPUT);
pinMode(pin_button, INPUT_PULLUP);
pinMode(pin_switch, INPUT_PULLUP);
}
void loop() {
//Reading the values for the four main potis (values from 0 to 1023)
for ( i = 0; i < 4; i++ ) {
bankPotiRead[i] = analogRead(bankPotiPin[i]);
//Smoothing the trembling for the four values
smoothValue[i] = 0.87 * smoothValue[i] + 0.13 * bankPotiRead[i];
//Mapping the smoothed values to MIDI scale (0 to 127)
bankPotiMidi[i] = map(smoothValue[i], 0, 1023, 0, 127);
//Reading and mapping the values for the side poti
sidePotiRead = analogRead(sidePotiPin);
sidePotiMidi = map(sidePotiRead, 0, 1023, 0, 127);
//Reading the state of the switch
sw = digitalRead(pin_switch);
//Reading the state of the button
btn = digitalRead(pin_button);
//Sending a MIDI note (C4) on button click
if (btn == LOW && btnOld == HIGH) {
// usbMIDI.sendControlChange(btnCont, 127, chn);
usbMIDI.sendNoteOn(60, 99, chn); // 60 = C4
btnOld = btn;
}
//Sending a 0 velocity MIDI note on button unclick
if (btn == HIGH && btnOld == LOW) {
// usbMIDI.sendControlChange(btnCont, 0, chn);
usbMIDI.sendNoteOff(60, 0, chn); // 60 = C4
btnOld = btn;
}
digitalWrite(pin_led_internal, HIGH);
//Sending a MIDI CC value for the side poti
if (sidePotiMidi != sidePotiOld) {
// Serial.println(sidePotiMidi);
// Serial.println(sidePotiCont);
usbMIDI.sendControlChange(sidePotiCont, sidePotiMidi, chn);
}
sidePotiOld = sidePotiMidi;
//the "2 bank" logic: if the switch value exists, use bank 0
if (sw){
//Serial.println("Bank state: ");
//Serial.println(sw);
if (bankPotiMidi[i] != bankPotiOld[i]) {
//use these to send test values on the arduino serial monitor
// Serial.print(bankPotiRead[i]);
// Serial.print("\t");
// Serial.print(smoothValue[i]);
// Serial.print("\t");
// Serial.println(bankPotiMidi[i]);
// Serial.print("\t");
// Serial.println(bank1Cont[i]);
usbMIDI.sendControlChange(bank1Cont[i], bankPotiMidi[i], chn);
}
bankPotiOld[i] = bankPotiMidi[i];
//in this bank state: constant LED light with about half power (120 of 255)
analogWrite(pin_led, 120);
//if the switch value doesn't exist, use the other bank
}else {
// Serial.println("Bank state: ");
//Serial.println(sw);
if (bankPotiMidi[i] != bankPotiOld[i]) {
// Serial.print(bankPotiRead[i]);
// Serial.print("\t");
// Serial.print(smoothValue[i]);
// Serial.print("\t");
// Serial.println(bankPotiMidi[i]);
// Serial.print("\t");
// Serial.println(bank0Cont[i]);
usbMIDI.sendControlChange(bank0Cont[i], bankPotiMidi[i], chn);
}
bankPotiOld[i] = bankPotiMidi[i];
//In this bank we use the LED pulse effect
unsigned long now = millis();
int val = 128 + 127 * cos(2 * PI / ledPeriode * now);
analogWrite(pin_led, val);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment