I am having trouble with the way the states switch. When i push the button connected to pin 3, The variable "button" becomes 1 but instead of going back to 0 when the button is not pressed it remains at 1 with 3 sec delays every interval in the serial monitor. Any ideas on how to control this variable successfully?
int button;
int state=0;
int nextState=0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(4,5,6,7,8,9);
void setup() {
lcd.begin(16,2);
lcd.clear();
pinMode(3,INPUT);
Serial.begin(9600);
}
void loop() {
switch(state)
{
case 0:
lcd.setCursor(0,0);
lcd.print("Hello, please");
lcd.setCursor(0,1);
lcd.print("deposit a coin");
break;
case 1:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("payment made");
lcd.setCursor(0,1);
lcd.print("$0.25");
delay(3000);
break;
case 2:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("payment made");
lcd.setCursor(0,1);
lcd.print("$0.25");
delay(3000);
break;
case 3:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("payment made");
lcd.setCursor(0,1);
lcd.print("$0.50");
delay(3000);
break;
default:
lcd.setCursor(0,0);
lcd.print("Hello, please");
lcd.setCursor(0,1);
lcd.print("deposit a coin");
break;
}
switch(state)
{
case 0:
if (digitalRead(3)==HIGH)
{nextState=1;}
else
{nextState=0;}
break;
case 1:
if(digitalRead(3)==HIGH)
{nextState=2;}
else
{nextState=1;}
break;
case 2:
if(digitalRead(3)==HIGH)
{nextState=2;}
else
{nextState=3;}
break;
case 3:
nextState=0;
break;
default:
nextState=0;
break;
}
state=nextState;
Serial.println(state);
}