// AIIT, 27.09.2023 Julian Weber 5AHET
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h> //including the required libraries
#define startButton A0 //defining the start button
#define Button1 0
#define Button2 6
#define Button3 2
#define Button4 3
#define Button5 4
#define Button6 5 //defining the button pins, which when pushed whack the mole
#define LED1 13
#define LED2 12
#define LED3 11
#define LED4 10
#define LED5 9
#define LED6 8 //defining the LED Pins (moles)
bool buttonstart = 0; //needed to read the start button
bool cButtonstate1 = 0;
bool cButtonstate2 = 0;
bool cButtonstate3 = 0;
bool cButtonstate4 = 0;
bool cButtonstate5 = 0;
bool cButtonstate6 = 0; //currentbuttonstates
bool pButtonstate1 = 0;
bool pButtonstate2 = 0;
bool pButtonstate3 = 0;
bool pButtonstate4 = 0;
bool pButtonstate5 = 0;
bool pButtonstate6 = 0; //previousbuttonstates
bool LED_1 = 0;
bool LED_2 = 0;
bool LED_3 = 0;
bool LED_4 = 0;
bool LED_5 = 0;
bool LED_6 = 0; //needed to read the LEDs
bool timereset = 0; //resets the millis every time the state goes to start
unsigned long time = 0; //stores the time, which is going by
unsigned long ptime = 0; //stores the previous time
unsigned long gtime = 0; //stores the time, which is going by until the button is pushed
unsigned long gametime = 60000; //defining the gametime (60sec)
unsigned long moleinterval = 2000; //defining the time you have to whack the mole (2sec)
unsigned long moletime = 0; //stores the time, when the random LED went HIGH
unsigned long score = 0; //stores the score
unsigned long mole = 0; //later needed for the random function
LiquidCrystal_I2C lcd(0x27, 16, 2); //defining the LCD Display
enum state
{
menu,
start,
end,
};
state game = menu; //state machine for the menu and start case
void setup()
{
lcd.init();
lcd.backlight(); //initialize LCD Display
pinMode(startButton, INPUT_PULLUP);
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Button4, INPUT_PULLUP);
pinMode(Button5, INPUT_PULLUP);
pinMode(Button6, INPUT_PULLUP); //defining all Buttons as Pullups for less cables
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT); //defining LEDs as outputs
}
void loop()
{
time = millis(); //storing the current time in time
buttonstart = !digitalRead(startButton);
cButtonstate1 = !digitalRead(Button1);
cButtonstate2 = !digitalRead(Button2);
cButtonstate3 = !digitalRead(Button3);
cButtonstate4 = !digitalRead(Button4);
cButtonstate5 = !digitalRead(Button5);
cButtonstate6 = !digitalRead(Button6); //reading all Buttons (! because they are pullups)
LED_1 = digitalRead(LED1);
LED_2 = digitalRead(LED2);
LED_3 = digitalRead(LED3);
LED_4 = digitalRead(LED4);
LED_5 = digitalRead(LED5);
LED_6 = digitalRead(LED6); //reading all the LEDs
switch (game)
{
case menu:
{
lcd.setCursor(0,0);
lcd.print("Playtime:"); //printing playtime on the Display
lcd.print(gametime/1000); //printing the gametime in sec on the Display
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("press start");
if(buttonstart)
{
gtime = time; //sets gtime to the current time
game = start; //if the start Button is pushed the state goes to start
lcd.clear(); //clearing the Display
score = 0; //setting score to zero every restart
}
}
break; //case menu
case start:
{
time = time-gtime; //sets time to the time going by after the button was pushed
lcd.setCursor(0,0);
lcd.print("Playtime:");
lcd.print((gametime-time)/1000); //printing gametime on the Display
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("score:");
lcd.print(score); //printing the score on the Display
if(!LED_1 && !LED_2 && !LED_3 && !LED_4 && !LED_5 && !LED_6) //if no LED is shining
{
mole = random(8,14); //pick a random LED Pin
digitalWrite(mole,HIGH); //turn the random LED on
moletime = time;
}
if(LED_1&&cButtonstate1&&!pButtonstate1)
{
score++;
digitalWrite(LED1, LOW);
moletime = 0;
}
if(LED_2&&cButtonstate2&&!pButtonstate2)
{
score++;
digitalWrite(LED2, LOW);
moletime = 0;
}
if(LED_3&&cButtonstate3&&!pButtonstate3)
{
score++;
digitalWrite(LED3, LOW);
moletime = 0;
}
if(LED_4&&cButtonstate4&&!pButtonstate4)
{
score++;
digitalWrite(LED4, LOW);
moletime = 0;
}
if(LED_5&&cButtonstate5&&!pButtonstate5)
{
score++;
digitalWrite(LED5, LOW);
moletime = 0;
}
if(LED_6&&cButtonstate6&&!pButtonstate6) //if the LED is shining and the button is pressed
{
score++;
digitalWrite(LED6, LOW); //the score is increasing by one and the LED is turning off
moletime = 0; //the moletime is set to zero
}
if(time-moletime > moleinterval)
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW); //if the moletime is expired, then set all LEDs to low
}
pButtonstate1 = cButtonstate1;
pButtonstate2 = cButtonstate2;
pButtonstate3 = cButtonstate3;
pButtonstate4 = cButtonstate4;
pButtonstate5 = cButtonstate5;
pButtonstate6 = cButtonstate6; //setting all previous button states to the current button state
if(time >= gametime)
{
lcd.clear();
game = end; //if gametime is reached, then clear the Display and go to the end sate
}
}
break;
case end:
{
lcd.setCursor(0,0);
lcd.print("Playtime:0");
lcd.setCursor(0,1);
lcd.print("score:");
lcd.print(score);
if(buttonstart)
{
game = menu; //if the startbutton is pushed, go back in the menu state
}
}
break;
}
}