I'm making an Arduino based MIDI controller that is going to control an effect pedal.
There are 4 buttons I'm going to be controlling that vary what they do depending on the state of the button.
For example, button1 is either record or overdub. button2 is play or stop. And they are codependent. If button2 is playing and I press button1 it goes into overdub instead of record.
Ok, that's the basic code requirement.
Before my horrible state changing logic code, I'm also having a hard time managing debounce for multiple (4) switches. I stuck some debounce code into each 'if' of the loop. Is that the best place to put debounce delay?
In the code below its debounce1 (and current 40ms). "debounce" is for the 4way button code below it all that isn't in use yet.
I've separated each possible function into its own 'if' loop, so I can troubleshoot and eventually add LED functionality too, without having to sift through 'elses' (I'm still new to coding).
When I start the code up, it almost works once, but then I think I break the state logic and nothing works anymore.
SWITCH1, SWITCH2, and SWITCH3 are the only buttons I'm using at the moment, and I'm using 2 variables for state changing.
state1 is for notrecording(0),recording(1), or overdubbing(2), and state2 is for not playing(0), and playing(1).
What am I doing wrong here?
// ======= SWITCHES
#define SWITCH1 8 // record/overdub
#define SWITCH2 9 // play/stop
#define SWITCH3 10 // play once
// ======= STATE variables
int val1 = 0; // record/overdub button press
int val2 = 0; // play/stop button press
int val3 = 0; // play once button press
int state1 = 0; // state of notrecording(0),record(1),overdub(2)
int state2 = 0; // state of stop(0),play(1)
// ======= TIMING variables
int debounce1 = 40; // ms debounce for record/overdub/play/stop switches
//=================================================
void setup()
{
// ======= BUTTONS
pinMode(SWITCH1, INPUT); // record/overdub
pinMode(SWITCH2, INPUT); // play/stop
pinMode(SWITCH3, INPUT); // play once
// ======= MIDI
Serial.begin(31250);
}
//=================================================
void loop()
{
// Get Switch Values
val1 = digitalRead(SWITCH1); // record/overdub button press
val2 = digitalRead(SWITCH2); // play/stop button press
val3 = digitalRead(SWITCH3); // play once button press
// ======= RECORD NEW LOOP
if ((val1 == HIGH) && (state1 == 0) && (state2 == 0)) {
state1 = 1;
Serial.print(0xb0,BYTE);
Serial.print(50,BYTE);
Serial.print(127,BYTE);
delay(debounce1);
}
// ======= OVERDUB (FROM RECORD STATE)
if ((val1 == HIGH) && (state1 == 1) && (state2 == 0)) {
state1 = 2;
state2 = 1;
Serial.print(0xb0,BYTE);
Serial.print(50,BYTE);
Serial.print(0,BYTE);
delay(debounce1);
}
// ======= OVERDUB (TO PLAY STATE)
if ((val1 == HIGH) && (state1 == 2) && (state2 == 1)) {
state1 = 0;
Serial.print(0xb0,BYTE);
Serial.print(50,BYTE);
Serial.print(0,BYTE);
delay(debounce1);
}
// ======= OVERDUB (FROM PLAY STATE)
if ((val1 == HIGH) && (state1 == 0) && (state2 == 1)) {
state1 = 2;
Serial.print(0xb0,BYTE);
Serial.print(50,BYTE);
Serial.print(0,BYTE);
delay(debounce1);
}
// ======= PLAY
if ((val2 == HIGH) && (state2 == 0)) {
state2 = 1;
Serial.print(0xb0,BYTE);
Serial.print(28,BYTE);
Serial.print(127,BYTE);
delay(debounce1);
}
// ======= STOP
if ((val2 == HIGH) && (state2 == 1)) {
state2 = 0;
Serial.print(0xb0,BYTE);
Serial.print(28,BYTE);
Serial.print(0,BYTE);
delay(debounce1);
}
// ======= PLAY ONCE
if (val3 == HIGH) {
Serial.print(0xb0,BYTE);
Serial.print(80,BYTE);
Serial.print(127,BYTE);
delay(debounce1);
}