//Define pins for buttons and leds #define buttonC1 D5 #define buttonC2 D10 #define buttonC3 D9 #define buttonC4 D8 #define ledC1 D0 #define ledC2 D2 #define ledC3 D3 #define ledC4 D4 // Arrays for buttons and leds const int arr_Buttons[] = {buttonC1, buttonC2, buttonC3, buttonC4}; const int arr_Leds[] = {ledC1, ledC2, ledC3, ledC4}; //Flags for losing bool b_Lose = false; bool b_Correct = false; //Array of levels and max number of levels int arr_Levels[4]; const int maxLevels = 4; //Delays float delayT = 550; float delayP = 250; void setup() { Serial.begin(9600); int x; //Set modes for leds and buttons by iterating the arrays for (int i = 0; i < 4; i++) { pinMode(arr_Buttons[i], INPUT); pinMode(arr_Leds[i], OUTPUT); } //Wait for the button to be pressed while(!digitalRead(D5)){ x=micros(); randomSeed(micros()); } /* while(1){ x = random(0, 4); Serial.println(x); delay(1000); }*/ } //New level with random number between 0-3 int newLevel() { int x = random(0, 4); Serial.print(x); return x; } //Control an specific led void controlLed(int index, bool value) { digitalWrite(arr_Leds[index], value); // Turn on the selected LED } //Leds ON void ledsON() { for (int i = 0; i < 4; i++) { digitalWrite(arr_Leds[i], HIGH); } } //Leds Off void ledsOFF() { for (int i = 0; i < 4; i++) { digitalWrite(arr_Leds[i], LOW); } } //Blink all leds void blinkLeds(int times) { for (int i = 0; i < times; i++) { ledsON(); delay(delayP); ledsOFF(); delay(delayP); } } //For next level blink 1 time void nextLevel() { blinkLeds(1); } //Sequence of win void Win() { for (int i = 0; i < 4; i++) { digitalWrite(arr_Leds[i], HIGH); delay(1000); } blinkLeds(3); } //Sequence of lose void Lose() { for (int i = 0; i < 4; i++) { digitalWrite(arr_Leds[i], HIGH); } delay(1000); for (int i = 0; i < 4; i++) { digitalWrite(arr_Leds[i], LOW); delay(1000); } } //All the game void loop() { int k = 0; //Variable for checking level while (!b_Lose && k < maxLevels) { //Continue while not losing arr_Levels[k] = newLevel(); //New level nextLevel(); //Sequence of next level for (int i=0; i