#include <Bounce.h>
//**********CONSTANTS**********
const int BOUNCE_TIME = 15; // 10ms is usually ok for buttons, bump way up if testing with a wire!
const int LEDPIN = 13; // Upper BANK indicator LED
const int DIGITAL_PINS = 5; // size of the PINS array
const int PINS[DIGITAL_PINS] = {0,1,2,3,4}; // set pin numbers of swtiches
const int channel = 1;
const byte CC[][DIGITAL_PINS] = { // D1 values
{0,0,0,0,0}, // Send D1 = 0 for the whole bank!
{65,66,67,68,69},
{70,71,72,73,74},
{75,76,77,78,79},
{80,81,82,83,84},
{85,86,87,88,89}
}; // this is the banks by switches matrix of CC values for the bank system
const int ONvalue[][DIGITAL_PINS] = { // D2 values when switch is toggled ON
{22,23,24,25,26},
{70,71,72,73,74},
{75,76,77,78,79},
{80,81,82,83,84},
{85,86,87,88,89},
{90,91,92,93,94},
};
const int OFFvalue[][DIGITAL_PINS] = { // D2 values when switch is toggled OFF
{22,23,24,25,26}, // if these are the same as the same row in ONvalue then there is no toggle but the same message!
{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}
};// arbitrary 'off' value for D2 of CC message
const int resetThreshold = 1500; /// mS hold to change bank
//**********VARIABLES/LIBRARY OBJECTS**********
int bank = 0; // this is the bank variable to be toggled
boolean CCstate[DIGITAL_PINS]; // array of current CC on/off toggle state
boolean timerOn; // timer toggle memory tracks whether a new fallingEdge was heard since the last rising; used in bank switching
Bounce btn[] = {
Bounce(PINS[0], BOUNCE_TIME),
Bounce(PINS[1], BOUNCE_TIME),
Bounce(PINS[2], BOUNCE_TIME),
Bounce(PINS[3], BOUNCE_TIME),
Bounce(PINS[4], BOUNCE_TIME),
};
elapsedMillis toggleReset; // mS clock counter to reset on contact/risgingEdge()
//**********SETUP**********
void setup() {
//Serial.begin(9600);
for (int i = 0;i<DIGITAL_PINS;i++){ // set up pins as input and with pullup resistors
pinMode(PINS[i], INPUT_PULLUP);
}
pinMode(LEDPIN, OUTPUT);
}
//**********LOOP**********
void loop() {
for (int i = 0;i<DIGITAL_PINS;i++){ // loop through each object in the BOUNCE arrray for the note switches
btn[i].update();
if (btn[i].fallingEdge()) {
timerOn = true ;
toggleReset = 0;
}
if (btn[i].risingEdge()) {
if(toggleReset<resetThreshold){
if (CCstate[i]){
usbMIDI.sendControlChange(CC[bank][i], ONvalue[bank][i], channel);
}else{
usbMIDI.sendControlChange(CC[bank][i], OFFvalue[bank][i], channel);
}
CCstate[i] = !CCstate[i] ; //set state memory
timerOn = false ; //turn off timer (resets each cycle)
}
}
if ([COLOR="#FF0000"]digitalRead(PINS[i])==LOW[/COLOR] && toggleReset>resetThreshold){
toggleReset = 0;
timerOn = false ;
bank = i;
[COLOR="#FF0000"]flash(i+1); [/COLOR]// flash for bank selection 1-5 (for index 0-4)
//digitalWrite(LEDPIN, bank); // turn off for now...
}
}
}
[COLOR="#FF0000"]void flash(int flashCount){
int i;
for (int i = 0;i<flashCount;i++){
digitalWrite(LEDPIN, HIGH);
delay(100);
digitalWrite(LEDPIN, LOW);
delay(100);
}
}[/COLOR]