somebody please help, i want to make 3 switchable condition that can be switched by pressing the button. Something like this :
if push button pressed
{
go to next condition; }
I will have 3 conditions. if the current state running is state 2, when the button is pressed, the current state is state 3 and if the button pressed again, then the active state will return to state 1.
StateChangeDetection example shows you how to notice a button press. A variable to contain the state. And a switch statement to conditions based on the state.
mctosima:
somebody please help, i want to make 3 switchable condition that can be switched by pressing the button. Something like this :
if push button pressed
{
go to next condition; }
I will have 3 conditions. if the current state running is state 2, when the button is pressed, the current state is state 3 and if the button pressed again, then the active state will return to state 1.
how to code it guys ?
you are almost there!!!
void loop()
{
int pressed = digitalRead(buttonPin);
if (pressed == LOW)
{
if (pressed != lastPressed)
{
state++;
if (state > 2 ) state = 0;// from the top, please
printed = false;
}
}
lastPressed = pressed;
if (state == 0)
{
// do state = zero stuff
}
else if (state == 1)
{
// do state = one stuff
}
else if (state == 2)
{
// do state = two stuff
}
}