/*
Forum: https://forum.arduino.cc/t/millis-function-not-working/1351125
Wokwi: https://wokwi.com/projects/422154291993325569
*/
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int currentLed = 2;
int triesCounter = 0;
int triesChecker = 0;
int roundNum = 1;
unsigned long interval = 200;
unsigned long interval2 = 500;
unsigned long prevTime1 = 0;
unsigned long prevTime2 = 0;
char* win1 = "You won the LED \n\n game, Difficulty 1. \n Congragulations!";
char* win2 = "You won the LED \n\n game, Difficulty 2. \n Congragulations!";
char* loss1 = "You lost the LED \n\n game, Difficulty \n\n 1! Try again!";
char* loss2 = "You lost the LED \n\n game, Difficulty \n\n 2! Try again!";
void blink(int blinkLed) {
digitalWrite(blinkLed, HIGH);
delay(200);
digitalWrite(blinkLed, LOW);
delay(200);
digitalWrite(blinkLed, HIGH);
delay(200);
digitalWrite(blinkLed, LOW);
delay(200);
}
void displayRoundandTries() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Round: ");
display.print(roundNum);
display.print("\n\nTries: ");
display.print(triesCounter);
display.display();
}
void displaySituation(char* situation) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 10);
display.println(situation);
display.display();
}
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
pinMode(12, INPUT_PULLUP);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
Serial.println("Do you want to play Difficulty 1 or Difficulty 2?");
displayRoundandTries();
}
void loop() {
/*
while (Serial.available() == 0){
}
int difficulty = Serial.parseInt();
switch(difficulty){
case 1:
difficulty_1();
case 2:
difficulty_2();
}
*/
difficulty_2();
}
void difficulty_1() {
while (triesChecker == 0) {
unsigned long currentTime1 = millis();
if (digitalRead(12) == 0) {
if (currentLed == 3) {
blink(3);
interval -= 20;
triesCounter = 0;
roundNum += 1;
if (roundNum > 9) {
displaySituation(win1);
triesChecker = 2;
break;
} else {
displayRoundandTries();
}
} else {
blink(currentLed);
triesCounter += 1;
if (triesCounter > 3) {
displaySituation(loss1);
triesChecker = 1;
break;
} else {
displayRoundandTries();
}
}
}
digitalWrite(currentLed, HIGH);
//Serial.println(currentTime1);
//Serial.println(prevTime1);
if (currentTime1 - prevTime1 >= interval) {
digitalWrite(currentLed, LOW);
currentLed = currentLed + 1;
if (currentLed > 5) {
currentLed = 2;
}
prevTime1 = currentTime1;
}
}
}
void difficulty_2() {
int randomLED = random(2, 6); // Generate a random LED once
while (triesChecker == 0) {
unsigned long currentTime2 = millis();
if (digitalRead(12) == 0) {
if (randomLED == 3) { // If the white LED (assuming it's on pin 3) is lit
blink(3);
triesCounter = 0;
roundNum += 1;
if (roundNum > 5) {
displaySituation(win2);
triesChecker = 2;
break;
} else {
displayRoundandTries();
}
} else {
blink(randomLED);
triesCounter += 1;
if (triesCounter > 3) {
displaySituation(loss2);
triesChecker = 1;
break;
} else {
displayRoundandTries();
}
}
}
// Control LED timing
if (currentTime2 - prevTime2 >= interval2) {
digitalWrite(randomLED, LOW); // Turn off the previous LED
randomLED = random(2, 6); // Get a new random LED
digitalWrite(randomLED, HIGH); // Turn on the new LED
prevTime2 = currentTime2;
}
}
}