//defining the necessary variables and arrays
int leds[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
int ledCheck[9] = {25, 27, 29, 31, 33, 35, 37, 39, 41};
const long delayTime = 100;
unsigned long pastTime = 0;
int numOfLeds = sizeof(leds) / sizeof(leds[0]);
int act = 2;
int direct = 1;
int introOff = 0;
int checker = 50;
//here we make the leds' pins to output and the leds' outputs to inputs
void setup() {
for (int led : leds) {
pinMode(leds[led], OUTPUT);
}
for (int check : ledCheck) {
pinMode(ledCheck[check], INPUT);
}
pinMode(checker, INPUT);
}
/*this is a helper function for the intro ledWave, this will shut
down the unused leds during the intro*/
void reset() {
for (int i :leds) {
digitalWrite(leds[i], LOW);
}
}
//this function controlls the intro ledWave
void ledWave() {
//setting the timer and checking if it's the acting time
unsigned long currentTime = millis();
if (currentTime - pastTime >= delayTime) {
reset();
//going through the leds and making them HIGH state
digitalWrite(leds[act], HIGH);
act += direct;
pastTime = currentTime;
//if we reached one of the ends of the leds, we change the direction
if (act == numOfLeds - 1 || act == 0) {
direct = -direct;
}
}
else {
reset();
}
}
void gameOver() {
for (int i = 1; i == 3;) {
for (int i : leds) {
digitalWrite(i, HIGH);
}
delay(500);
for (int i : leds) {
digitalWrite(i, LOW);
}
i++;
}
}
/*this function giving a HIGH state to a random led pin and by that
making a led bright (we need a randomSeed() function to make it
really random)*/
int randomLed() {
randomSeed(analogRead(A1));
int randomNum = random(2, 11);
delay(100);
digitalWrite(randomNum, HIGH);
delay(500);
digitalWrite(randomNum, LOW);
return randomNum;
}
/*this function will guide our game, first we make an if statement to make
sure that the randomLed function runs only once at the beginnig of the
game, after that only when one of the leds' buttons pressed the randomLed
function happening again (we use and will use the introOff variable later) */
void game() {
if (introOff < 2) {
randomLed();
introOff = 10;
}
for (int i : ledCheck) {
if (digitalRead(i) == HIGH) {
if (randomLed() == (i - 15)) {
randomLed();
}
else {
gameOver();
}
}
}
}
/*here is the loop where we call the functions, this function will
always run, we controll here the ledWave starting and ending and the game
starting also*/
void loop() {
if (introOff == 0) {
ledWave();
}
if (digitalRead(checker) == HIGH) {
++introOff;
}
if (introOff > 0) {
game();
}
}
/*TO DO:
by checking if the index of the led and index of the button is the same, check if the user pressed the good button*/