Hi all. I've tried searching the forum and Google on this question, but I haven't quite found the answer I'm looking for.
For starters what I want to do is have two push buttons hooked up to two inputs and when I press one button, I want it to loop a function indefinitely until the other button is pressed at which point I want another separate function to loop indefinitely.
I know it has something to do with reading whether the button state has changed and if it has, perform "x", else if the other button has changed states then perform "y" but I can't quite figure out how to get to work right. It only performs each function while the buttons are held down. Here is the code.
EDIT: I have the inputs hooked up to pull-up resistors, hence I'm checking to see if the input goes LOW.
const int button_1 = 52;
const int button_2 = 53;
bool button_1_counter = false;
bool button_2_counter = false;
void setup()
{
pinMode (button_1, INPUT);
pinMode (button_2, INPUT);
pinMode (51, OUTPUT);
pinMode (50, OUTPUT);
Serial.begin (9600);
}
void loop()
{
int button_1_state = digitalRead(button_1);
int button_2_state = digitalRead(button_2);
if (button_1_state == LOW)
{
delay (10);
button_1_counter = true;
button_2_counter = false;
if (button_1_counter == true)
{
Serial.println ("Button 1");
delay (500);
}
}
else if (button_2_state == LOW)
{
delay (10);
button_1_counter = false;
button_2_counter = true;
if (button_2_counter == true)
{
Serial.println ("Button 2");
delay (500);
}
}
}