So I'm trying to create a MIDI controller for a guitar pedal I have.
On the physical pedal (a Line6 M9) each button serves 2 functions.
Button1 is Record/Overdub
Button2 is Play/Stop
If nothing is happening, hitting button one will go into record mode and create a loop. If you then press button 1 again, it goes into PLAY mode. If you press button 2 instead, it goes into PLAY mode. If you press button 2 while in PLAY mode it goes STOPs. If while PLAYing you press button1, it goes back into OVERDUB.
That's sort of the basic operating principles.
Now, in order to manage those interdependent states, I've created this mess of a code, and it doesn't work. I'm guessing that I create impossible states, and the code can't get out of it.
After wrestling with debouncing stuff, I went with the Button library, and so far that's been working fine. Just don't know how to keep track of what's going on, that would be error proof.
Also, a smaller detail. Is there a benefit (other than tidiness) for nesting IF loops? Like in my code, the first 3 bits contain "((button1.uniquePress())". Would it be better to have an IF loop that starts with that, then uses ELSES to go into the next criteria?
Here's my code.
#include <Button.h>
#define LED 13
Button button1 = Button(8,PULLDOWN);
Button button2 = Button(9,PULLDOWN);
Button button3 = Button(10,PULLDOWN);
int state1 = 0; // state of notrecording(0),record(1),overdub(2)
int state2 = 0; // state of stop(0),play(1)
void setup() {
pinMode(13,OUTPUT);
Serial.begin(31250);
}
void loop(){
// ======= RECORD NEW LOOP
if ((button1.uniquePress()) && (state1 == 0) && (state2 == 0)) {
state1 = 1;
Serial.print(0xb0,BYTE);
Serial.print(50,BYTE);
Serial.print(127,BYTE);
digitalWrite(LED,HIGH);
delay(5);
digitalWrite(LED,LOW);
}
// ======= OVERDUB (FROM RECORD STATE)
if ((button1.uniquePress()) && (state1 == 1) && (state2 == 0)) {
state1 = 2;
state2 = 1;
Serial.print(0xb0,BYTE);
Serial.print(50,BYTE);
Serial.print(0,BYTE);
digitalWrite(LED,HIGH);
delay(5);
digitalWrite(LED,LOW);
}
// ======= OVERDUB (TO PLAY STATE)
if ((button1.uniquePress()) && (state1 == 2) && (state2 == 1)) {
state1 = 0;
Serial.print(0xb0,BYTE);
Serial.print(50,BYTE);
Serial.print(0,BYTE);
digitalWrite(LED,HIGH);
delay(5);
digitalWrite(LED,LOW);
}
// ======= OVERDUB (FROM PLAY STATE)
if ((button1.isPressed()) && (state1 == 0) && (state2 == 1)) {
state1 = 2;
Serial.print(0xb0,BYTE);
Serial.print(50,BYTE);
Serial.print(0,BYTE);
digitalWrite(LED,HIGH);
delay(5);
digitalWrite(LED,LOW);
}
// ======= PLAY
if ((button2.uniquePress()) && (state2 == 0)) {
state2 = 1;
Serial.print(0xb0,BYTE);
Serial.print(28,BYTE);
Serial.print(127,BYTE);
digitalWrite(LED,HIGH);
delay(5);
digitalWrite(LED,LOW);
}
// ======= STOP
if ((button2.uniquePress()) && (state2 == 1)) {
state2 = 0;
Serial.print(0xb0,BYTE);
Serial.print(28,BYTE);
Serial.print(0,BYTE);
digitalWrite(LED,HIGH);
delay(5);
digitalWrite(LED,LOW);
}
// ======= PLAY ONCE
if(button3.uniquePress()){
Serial.print(0xb0,BYTE);
Serial.print(80,BYTE);
Serial.print(127,BYTE);
digitalWrite(LED,HIGH);
delay(5);
digitalWrite(LED,LOW);
}
}