#include <LiquidCrystal_I2C.h>
#include <elapsedMillis.h>
elapsedMillis lcdUpdate(50);
elapsedMillis Time;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int Player1 = 2;
int zone = 0;
int stopwatch = 0;
int Player1PushCounter = 0;
int Player1State = 0;
int Player1lastState = 0;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(Player1, INPUT_PULLUP);
Player1State = digitalRead(Player1);
Player1State = LOW;
}
void loop() {
// start screen
if (zone == 1) {
lcd.setCursor(0, 0);
lcd.print("Press To Start");
if (Player1State == HIGH) {
zone = 2;
stopwatch = 0;
}
}
// game starts
else if (zone == 2) {
Player1State = LOW;
if (Player1State == Player1lastState) {
if (Player1State == HIGH) {
Player1PushCounter++;
lcd.setCursor(0, 0);
lcd.print("Player 1: ");
lcd.setCursor(9, 0);
lcd.println(Player1PushCounter);
lcdUpdate;
}
}
if (Player1PushCounter >= 10) {
zone = 3;
stopwatch = stopwatch;
}
}
// winning screen
else if (zone == 3) {
Player1State == Player1lastState;
if (Player1PushCounter >= 10) {
lcd.setCursor(0, 1);
lcd.println(" ");
lcd.setCursor(0, 0);
lcd.println("P1 Wins!");
//stopwatch shows
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(stopwatch);
lcd.print("ms");
Player1PushCounter = 0;
stopwatch = 0;
}
lcdUpdate;
zone = 1;
}
Player1lastState = Player1State;
Player1State = digitalRead(Player1);
lcdUpdate;
}