I am trying to make a game countdown timer with Arduino Uno and the LCD included in the Arduino starter kit. I created a function called turn for the sequence the program should follow on each turn. What I want to happen is when you push the button, a the function turn starts. Then, if you push the button again, have the function starts over.
Here is my code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int switchPin = 6;
int switchState = 0;
int prevSwitchState = 0;
int reply;
const int greenPin = 7;
const int yellowPin = 8;
const int redPin = 13;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(switchPin, INPUT);
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(redPin, OUTPUT);
lcd.print("Press button to");
lcd.setCursor(0, 1);
lcd.print("Begin Turn");
}
void turn() {
digitalWrite(greenPin, HIGH);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("45");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("44");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("43");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("42");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("41");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("40");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("39");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("38");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("37");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("36");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("35");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("34");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("33");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("32");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("31");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("30");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("29");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("28");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("27");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("26");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("25");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("24");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("23");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("22");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("21");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("20");
delay(1000);
lcd.clear();
digitalWrite(greenPin, LOW);
digitalWrite(yellowPin, HIGH);
lcd.setCursor(6, 0);
lcd.print("19");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("18");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("17");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("16");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("15");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("14");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("13");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("12");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("11");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("10");
delay(1000);
lcd.clear();
digitalWrite(yellowPin, LOW);
digitalWrite(redPin, HIGH);
lcd.setCursor(6, 0);
lcd.print("09");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("08");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("07");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("06");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("05");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("04");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("03");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("02");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("01");
delay(1000);
lcd.clear();
lcd.setCursor(6, 0);
lcd.print("00");
delay(1000);
lcd.clear();
digitalWrite(redPin, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
switchState = digitalRead(switchPin);
if(switchState != prevSwitchState){
if(switchState == LOW){
turn();
lcd.clear();
}
}
lcd.setCursor(0, 0);
lcd.print("Press button to");
lcd.setCursor(0, 1);
lcd.print("Begin Turn");
prevSwitchState = switchState;
}