Aussie_CrocHunter
Well-known member
Hi there people who know way more than me! *wave*
I'm building a midi controller (isn't everyone?). I am using footswitches with built-in LED rings. They are two-color rings (red and green).
The footswitches are latching switches (push on, then push off again).
I have successfully written a sketch where each button sends a different midi CC and then the LEDs read the midi state from the software. when the switch is taken LOW, the controller sends value of 127 (fully on) and then the software will send a message back to the LED saying that it should change to green - the same happens when sending a value of "0" - the software will acknowledge and send back a "0" value which the teensy will read and turn the green LEDs off and the red LEDs on.
This is also fairly simple. But, what I'd like, is for the CC value that the button sends to be based on a midi read or at least, the state of the LEDs. That would mean that the it doesn't matter if the switch is made low or high, it will simply do the opposite of the last message it received. (if the leds are green, changing the state of the footswitch would send a midi CC value of "0", and if the leds are red, then changing the switch would send "127")
I haven't studied programming, but through my googling I assume I'm going to have to store a variable. But unfortunately I have no idea what that means or how to write it!
any help would be greatly appreciated (and also any refining of my current code - if i won't have to completely re-write it for this sort of function that is)
I'm building a midi controller (isn't everyone?). I am using footswitches with built-in LED rings. They are two-color rings (red and green).
The footswitches are latching switches (push on, then push off again).
I have successfully written a sketch where each button sends a different midi CC and then the LEDs read the midi state from the software. when the switch is taken LOW, the controller sends value of 127 (fully on) and then the software will send a message back to the LED saying that it should change to green - the same happens when sending a value of "0" - the software will acknowledge and send back a "0" value which the teensy will read and turn the green LEDs off and the red LEDs on.
This is also fairly simple. But, what I'd like, is for the CC value that the button sends to be based on a midi read or at least, the state of the LEDs. That would mean that the it doesn't matter if the switch is made low or high, it will simply do the opposite of the last message it received. (if the leds are green, changing the state of the footswitch would send a midi CC value of "0", and if the leds are red, then changing the switch would send "127")
I haven't studied programming, but through my googling I assume I'm going to have to store a variable. But unfortunately I have no idea what that means or how to write it!
any help would be greatly appreciated (and also any refining of my current code - if i won't have to completely re-write it for this sort of function that is)
Code:
#include <Bounce2.h>
//BUTTONS SETUP
const int NUM_OF_BUTTONS = 6;
const int DEBOUNCE_TIME = 5;
Bounce buttons[NUM_OF_BUTTONS];
int MIDI_CC_NUMS[NUM_OF_BUTTONS];
int MIDI_CC_VALS[NUM_OF_BUTTONS];
int MIDI_CHAN = 1;
//Change LED red to green when receiving MIDI CC
int redLED1 = 6;
int greenLED1 = 7;
int redLED2 = 8;
int greenLED2 = 9;
int redLED3 = 10;
int greenLED3 = 16;
int redLED4 = 17;
int greenLED4 = 18;
int redLED5 = 19;
int greenLED5 = 20;
int singleLED = 21;
void OnControlChange(byte channel, byte controller, byte value) {
if (controller == 99 && value == 127) {
digitalWrite(greenLED1, LOW);
digitalWrite(redLED1, HIGH); //receiving 127 turns green on and red off
}
if (controller == 99 && value == 0) {
digitalWrite(greenLED1, HIGH);
digitalWrite(redLED1, LOW); //receiving 127 turns green off and red on
}
if (controller == 100 && value == 127) {
digitalWrite(greenLED5, LOW);
digitalWrite(redLED5, HIGH); //receiving 127 turns green on and red off
}
if (controller == 100 && value == 0) {
digitalWrite(greenLED5, HIGH);
digitalWrite(redLED5, LOW); //receiving 127 turns green off and red on
}
if (controller == 101 && value == 127) {
digitalWrite(greenLED2, LOW);
digitalWrite(redLED2, HIGH); //receiving 127 turns green on and red off
}
if (controller == 101 && value == 0) {
digitalWrite(greenLED2, HIGH);
digitalWrite(redLED2, LOW); //receiving 127 turns green off and red on
}
if (controller == 102 && value == 127) {
digitalWrite(greenLED3, LOW);
digitalWrite(redLED3, HIGH); //receiving 127 turns green on and red off
}
if (controller == 102 && value == 0) {
digitalWrite(greenLED3, HIGH);
digitalWrite(redLED3, LOW); //receiving 127 turns green off and red on
}
if (controller == 103 && value == 127) {
digitalWrite(greenLED4, LOW);
digitalWrite(redLED4, HIGH); //receiving 127 turns green on and red off
}
if (controller == 103 && value == 0) {
digitalWrite(greenLED4, HIGH);
digitalWrite(redLED4, LOW); //receiving 127 turns green off and red on
}
}
void setup() {
for (int i= 0; i < NUM_OF_BUTTONS; i++)
{
Bounce bounce = Bounce();
bounce.attach(i);
bounce.interval(DEBOUNCE_TIME);
buttons[i] = bounce;
pinMode (i, INPUT_PULLUP);
MIDI_CC_NUMS[i] = 99 + i;
MIDI_CC_VALS[i] = 127;
}
pinMode(redLED1, OUTPUT);
pinMode(greenLED1, OUTPUT);
pinMode(redLED2, OUTPUT);
pinMode(greenLED2, OUTPUT);
pinMode(redLED3, OUTPUT);
pinMode(greenLED3, OUTPUT);
pinMode(redLED4, OUTPUT);
pinMode(greenLED4, OUTPUT);
pinMode(redLED5, OUTPUT);
pinMode(greenLED5, OUTPUT);
digitalWrite(redLED1, HIGH);
digitalWrite(greenLED1, HIGH);
digitalWrite(redLED2, HIGH);
digitalWrite(greenLED2, HIGH);
digitalWrite(redLED3, HIGH);
digitalWrite(greenLED3, HIGH);
digitalWrite(redLED4, HIGH);
digitalWrite(greenLED4, HIGH);
digitalWrite(redLED5, HIGH);
digitalWrite(greenLED5, HIGH);
usbMIDI.setHandleControlChange(OnControlChange);
}
void loop() {
for (int i = 0; i < NUM_OF_BUTTONS + 1; i++) {
buttons[i].update();
}
for (int i = 0; i < NUM_OF_BUTTONS; i++)
{
if (buttons[i].fallingEdge())
{
usbMIDI.sendControlChange (MIDI_CC_NUMS[i], MIDI_CC_VALS[i], MIDI_CHAN);
}
else if (buttons[i].risingEdge())
{
usbMIDI.sendControlChange (MIDI_CC_NUMS[i], 0, MIDI_CHAN);
}
while (usbMIDI.read());
}
}