Aussie_CrocHunter
Well-known member
Hi,
I have 6 switches on my usb midi device. Five switches using the code I've put at the bottom here, and a sixth momentary switch (pin 5) with a single LED (pin 21).
What I'm aiming to do is have the momentary switch (pin 5) send midi cc on and off (not toggle) with each press, but ALSO have a total of three banks of switches for the 5 latching switches. To do so, I was thinking that the momentary switch could be held down for 21.5-2 seconds which would change all the CC values to the second bank with an acknowledgement long flash by the single led (pin 21), and then so on to the third bank and back to the 1st bank (cycling through the banks infinitely).
One issue i perceive is how to make the switch on pin 5 send midi CC responsively without delay, whilst also having it not send midi if a long-press is detected. I don't know if those two things can coexist.
I have 6 switches on my usb midi device. Five switches using the code I've put at the bottom here, and a sixth momentary switch (pin 5) with a single LED (pin 21).
What I'm aiming to do is have the momentary switch (pin 5) send midi cc on and off (not toggle) with each press, but ALSO have a total of three banks of switches for the 5 latching switches. To do so, I was thinking that the momentary switch could be held down for 21.5-2 seconds which would change all the CC values to the second bank with an acknowledgement long flash by the single led (pin 21), and then so on to the third bank and back to the 1st bank (cycling through the banks infinitely).
One issue i perceive is how to make the switch on pin 5 send midi CC responsively without delay, whilst also having it not send midi if a long-press is detected. I don't know if those two things can coexist.
Code:
//************LIBRARIES USED**************
// 'include the Bounce library for 'de-bouncing' switches -- removing electrical chatter as contacts settle'
#include <Bounce.h>
//'usbMIDI.h library is added automatically when code is compiled as a MIDI device'
// ******CONSTANT VALUES********
// customize code behaviour here!
const int channel = 1; // MIDI channel
const int D_PINS = 5; // number of Digital PINS
const int ON_Value = 127; // note-one velocity sent from buttons (should be 65 to 127)
// 'define the pins and notes for digital events'
const int DIGITAL_PINS[D_PINS] = {0,1,2,3,4};
const int RED_LED_PINS[D_PINS] = {6,19,8,10,17};
const int GREEN_LED_PINS[D_PINS] = {7,20,9,16,18};
const int MIDI_CC_NUMS[D_PINS] = {60,61,62,63,64};
const int BOUNCE_TIME = 7; // 5 ms is usually sufficient
const boolean toggled = true;
//******VARIABLES***********
// a data array to remember the current state of each switch
boolean state[D_PINS];
//************INITIALIZE LIBRARY OBJECTS**************
// initialize the bounce objects
Bounce digital[] = {
Bounce(DIGITAL_PINS[0],BOUNCE_TIME),
Bounce(DIGITAL_PINS[1], BOUNCE_TIME),
Bounce(DIGITAL_PINS[2], BOUNCE_TIME),
Bounce(DIGITAL_PINS[3], BOUNCE_TIME),
Bounce(DIGITAL_PINS[4], BOUNCE_TIME)
};
//************SETUP**************
void setup() {
//'set a handle for returning control change messages'
usbMIDI.setHandleControlChange(OnControlChange);
//'loop to configure input pins and internal pullup resisters for digital section'
for (int i=0;i<D_PINS;i++){
pinMode(DIGITAL_PINS[i], INPUT_PULLUP);
pinMode(RED_LED_PINS[i], OUTPUT);
pinMode(GREEN_LED_PINS[i], OUTPUT);
}
}
//************LOOP**************
void loop() {
getDigitalData();
while (usbMIDI.read()) {
//' controllers must call .read() to keep the queue clear even if they are not responding to MIDI'
}
}
//************DIGITAL SECTION**************
void getDigitalData(){
for (int i=0;i<D_PINS;i++){
digital[i].update();
if (digital[i].fallingEdge() || digital[i].risingEdge()) {
if (state[i]) {
usbMIDI.sendControlChange(MIDI_CC_NUMS[i], 0, channel);
}else{
usbMIDI.sendControlChange(MIDI_CC_NUMS[i], ON_Value, channel);
}
state[i] = !state[i] ;
digitalWrite(RED_LED_PINS[i], state[i]);
digitalWrite(GREEN_LED_PINS[i], !state[i]);
}
}
}
void OnControlChange(byte channel, byte controller, byte value) {
// add channel check?
for (int i = 0; i < D_PINS ; i++){
if (MIDI_CC_NUMS[i] == controller) {
if (value >= 64) {
digitalWrite(GREEN_LED_PINS[i], LOW);
digitalWrite(RED_LED_PINS[i], HIGH); //'receiving >64 turns green on and red off'
state[i] = true;
}else{
digitalWrite(GREEN_LED_PINS[i], HIGH);
digitalWrite(RED_LED_PINS[i], LOW); //'receiving <64 turns red on and green off'
state[i] = false;
} // 'if not the controller for i then skip this loop'
}
}
}