I want to do something simple with 3 push buttons. All buttons, when pressed, will print a Serial message as seen here:
void setup() {
Serial.begin(31250); //31250 - standard MIDI baud rate
Serial.println("Press Buttons");
pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
pinMode(BUTTON3,INPUT);
digitalWrite(BUTTON1,HIGH);
digitalWrite(BUTTON2,HIGH);
digitalWrite(BUTTON3,HIGH);
}
boolean recorded_pressed_once = false; //Makes sure the user doesn't do fishy things
boolean RECORD = false;
boolean STOP = true;
boolean LED_MODE = false; //off by default
void loop() {
//BUTTON 1
if( button(BUTTON1) && RECORD == false && STOP == true ){ //if button was pressed for 'RECORD'
//RECORD press
recorded_pressed_once = true;
RECORD = true;
STOP = false;
Serial.println("BEGIN");
while(button(BUTTON1));
}else if( button(BUTTON1) && RECORD == true && STOP == false ){ //else if button was pressed for 'STOP'
//STOP press
STOP = true;
RECORD = false;
Serial.println("STOP");
while(button(BUTTON1));
}
//BUTTON2
if( button(BUTTON2) && RECORD == false && STOP == true ){ //Do not generate unless STOP has been pressed
if(recorded_pressed_once){
Serial.println("GENERATING...");
while(button(BUTTON2));
}else{
//No recording was ever taken. Do nothing
}
}
//BUTTON3
if( button(BUTTON3) ){ //Can turn on/off anytime
if(LED_MODE == true){ //If already on, turn off
Serial.println("LED mode OFF");
LED_MODE = false;
while(button(BUTTON3));
}else if(LED_MODE == false){
Serial.println("LED mode ON");
LED_MODE = true;
while(button(BUTTON3));
}
}
}
char button(char but_num)
{
return (!(digitalRead(but_num)));
}
Currently my output is as follows:
Sometimes when any of the buttons is pressed (it happens with all of them, mostly on button 1) It more than one of the messeges. This poses a problem with button 1 and 3 as they print like this on occasion:
LED mode ON
LED mode OFF
LED mode ON
or
BEGIN
STOP
BEGIN
I'm not sure what is causing this, I am concerned that I am debouncing the buttons incorrectly, which for some reason leads to the inconsistent Serial output shown.