// AIIT, 27.09.2023 Julian Weber 5AHET
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h> //including the required libraries
#include "pitches.h"
#define startButton A0 //defining the start button
// declaring variables
const int tonePin = 10;
unsigned long previousMillis = 0;
unsigned long speakerMillis = 0;
unsigned long currentMillis = 0;
long interval = 20;
#define buzzerPin 1
#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 Button7 23
#define Button8 25
#define Button9 27
#define Button10 29
#define Button11 31
#define Button12 33 //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)
#define LED7 43
#define LED8 45
#define LED9 47
#define LED10 49
#define LED11 51
#define LED12 53 //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 cButtonstate7 = 0;
bool cButtonstate8 = 0;
bool cButtonstate9 = 0;
bool cButtonstate10 = 0;
bool cButtonstate11= 0;
bool cButtonstate12 = 0; //currentbuttonstates
bool pButtonstate1 = 0;
bool pButtonstate2 = 0;
bool pButtonstate3 = 0;
bool pButtonstate4 = 0;
bool pButtonstate5 = 0;
bool pButtonstate6 = 0; //previousbuttonstates
bool pButtonstate7 = 0;
bool pButtonstate8 = 0;
bool pButtonstate9 = 0;
bool pButtonstate10 = 0;
bool pButtonstate11 = 0;
bool pButtonstate12 = 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 LED_7 = 0;
bool LED_8 = 0;
bool LED_9 = 0;
bool LED_10 = 0;
bool LED_11 = 0;
bool LED_12 = 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 = 30000; //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
int moleVals[12] = {13, 12, 11, 10, 9, 8, 43, 45, 47, 49, 51, 53};
LiquidCrystal_I2C lcd(0x27, 16, 2); //defining the LCD Display
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4,
NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4};
int melodyInterval[] = {
1,1,1,1,1,1,1,1,1,1};
const int max = 8;
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(Button7, INPUT_PULLUP);
pinMode(Button8, INPUT_PULLUP);
pinMode(Button9, INPUT_PULLUP);
pinMode(Button10, INPUT_PULLUP);
pinMode(Button11, INPUT_PULLUP);
pinMode(Button12, 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
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);
pinMode(LED10, OUTPUT);
pinMode(LED11, OUTPUT);
pinMode(LED12, OUTPUT); //defining LEDs as outputs
}
/**
Lights the given LED and plays a suitable tone
*/
void beep1(){
int i = 0;
while (i<max) {
unsigned long currentMillis = millis();
if(currentMillis - speakerMillis > melodyInterval[i]) {
speakerMillis = currentMillis;
tone(buzzerPin,melody[i],i);
i++;
}
}
}
void loop()
{
time = millis(); //storing the current time in time
currentMillis = millis();
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)
cButtonstate7 = !digitalRead(Button7);
cButtonstate8 = !digitalRead(Button8);
cButtonstate9 = !digitalRead(Button9);
cButtonstate10 = !digitalRead(Button10);
cButtonstate11 = !digitalRead(Button11);
cButtonstate12 = !digitalRead(Button12); //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
LED_7 = digitalRead(LED7);
LED_8 = digitalRead(LED8);
LED_9 = digitalRead(LED9);
LED_10 = digitalRead(LED10);
LED_11 = digitalRead(LED11);
LED_12 = digitalRead(LED12); //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 analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(7));
if(!LED_1 && !LED_2 && !LED_3 && !LED_4 && !LED_5 && !LED_6 && !LED_7 && !LED_8 && !LED_9 && !LED_10 && !LED_11 && !LED_12) //if no LED is shining
{
mole = moleVals[random(12)];
//TO DO***
//array to hold the LED pins and then random 1-12 use as position in array
//to identify the mole (pin) to switch on
digitalWrite(mole,HIGH); //turn the random LED on
moletime = time;
beep1();
}
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(LED_7&&cButtonstate7&&!pButtonstate7) //if the LED is shining and the button is pressed
{
score++;
digitalWrite(LED7, LOW); //the score is increasing by one and the LED is turning off
moletime = 0; //the moletime is set to zero
}
if(LED_8&&cButtonstate8&&!pButtonstate8) //if the LED is shining and the button is pressed
{
score++;
digitalWrite(LED8, LOW); //the score is increasing by one and the LED is turning off
moletime = 0; //the moletime is set to zero
}
if(LED_9&&cButtonstate9&&!pButtonstate9) //if the LED is shining and the button is pressed
{
score++;
digitalWrite(LED9, LOW); //the score is increasing by one and the LED is turning off
moletime = 0; //the moletime is set to zero
}
if(LED_10&&cButtonstate10&&!pButtonstate10) //if the LED is shining and the button is pressed
{
score++;
digitalWrite(LED10, LOW); //the score is increasing by one and the LED is turning off
moletime = 0; //the moletime is set to zero
}
if(LED_11&&cButtonstate11&&!pButtonstate11) //if the LED is shining and the button is pressed
{
score++;
digitalWrite(LED11, LOW); //the score is increasing by one and the LED is turning off
moletime = 0; //the moletime is set to zero
}
if(LED_12&&cButtonstate12&&!pButtonstate12) //if the LED is shining and the button is pressed
{
score++;
digitalWrite(LED12, 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
digitalWrite(LED7, LOW);
digitalWrite(LED8, LOW);
digitalWrite(LED9, LOW);
digitalWrite(LED10, LOW);
digitalWrite(LED11, LOW);
digitalWrite(LED12, 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
pButtonstate7 = cButtonstate7;
pButtonstate8 = cButtonstate8;
pButtonstate9 = cButtonstate9;
pButtonstate10 = cButtonstate10;
pButtonstate11 = cButtonstate11;
pButtonstate12 = cButtonstate12; //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);
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
digitalWrite(LED7, LOW);
digitalWrite(LED8, LOW);
digitalWrite(LED9, LOW);
digitalWrite(LED10, LOW);
digitalWrite(LED11, LOW);
digitalWrite(LED12, LOW); //if the moletime is expired, then set all LEDs to low
if(buttonstart)
{
game = menu; //if the startbutton is pushed, go back in the menu state
}
}
break;
}
}