#define BLYNK_TEMPLATE_ID "TMPL67QIpsEuj"
#define BLYNK_TEMPLATE_NAME "Chase the Lights ESP32 with Blynk"
#define BLYNK_AUTH_TOKEN "fpP-v6CB54du4b28MNI6KcpBPaXjqXIn"
#include <LiquidCrystal_I2C.h>
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
int relay1_state = 0;
int relay2_state = 0;
int relay3_state = 0;
int relay4_state = 0;
//Change the virtual pins according the rooms
#define button1_vpin V0
#define button2_vpin V1
#define button3_vpin V2
#define button4_vpin V3
//------------------------------------------------------------------------------
// This function is called every time the device is connected to the Blynk.Cloud
// Request the latest state from the server
BLYNK_CONNECTED() {
Blynk.syncVirtual(button1_vpin);
Blynk.syncVirtual(button2_vpin);
Blynk.syncVirtual(button3_vpin);
Blynk.syncVirtual(button4_vpin);
}
// LCD configuration, number of rows and cols
int lcdColumns = 16;
int lcdRows = 2;
// Define pins for LEDs, buttons, and buzzer
#define redRelay 19
#define greenRelay 18
#define yellowRelay 5
#define blueRelay 17
#define redButton 15
#define greenButton 2
#define yellowButton 4
#define blueButton 16
#define buzzer 23
#define defultinterval 1500 //hold time for game over page
// Arrays to store LEDs and buttons
int relays[4] = { redRelay, greenRelay, yellowRelay, blueRelay };
int buttons[4] = { redButton, greenButton, yellowButton, blueButton };
int buttonstatus[4] = { 0, 0, 0, 0 };
// Variables to control game state
int randset = 0;
int count = 0;
int score = 0;
int highscore = 0;
int level = 1;
int statusSum = 1;
int interval = defultinterval;
unsigned long strttime = 0;
// LCD object
LiquidCrystal_I2C lcd_1(0x27, lcdColumns, lcdRows);
// Function prototypes
void gameover();
void updatescore();
//--------------------------------------------------------------------------
// This function is called every time the Virtual Pin state change
//i.e when web push switch from Blynk App or Web Dashboard
BLYNK_WRITE(button1_vpin) {
relay1_state = param.asInt();
digitalWrite(redRelay, relay1_state);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button2_vpin) {
relay2_state = param.asInt();
digitalWrite(greenRelay, relay2_state);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button3_vpin) {
relay3_state = param.asInt();
digitalWrite(yellowRelay, relay3_state);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button4_vpin) {
relay4_state = param.asInt();
digitalWrite(blueRelay, relay4_state);
}
//--------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
// Initialize LCD
lcd_1.init();
lcd_1.backlight();
// Set pin modes
pinMode(buzzer, OUTPUT);
for (int i = 0; i < 4; i++) {
pinMode(relays[i], OUTPUT);
pinMode(buttons[i], INPUT_PULLUP);
digitalWrite(relays[i], LOW);
}
// Display initial message
// Serial.begin(9600);
lcd_1.setCursor(0, 0);
lcd_1.print("CHASE THE LIGHTS");
lcd_1.setCursor(0, 1);
lcd_1.print(" Starting in: ");
lcd_1.print("3");
delay(1000);
lcd_1.setCursor(14, 1);
lcd_1.print("2");
delay(1000);
lcd_1.setCursor(14, 1);
lcd_1.print("1");
delay(1000);
lcd_1.clear();
}
void loop() {
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
listen_push_buttons();
// // // Generate a random LED to light up
// randset = random(0, 4);
// // Check if the level is complete
// if (count == 4) {
// interval -= 50;
// level += 1;
// count = 0;
// }
// // // Display game information on the LCD
// lcd_1.setCursor(0, 0);
// lcd_1.print("Lvl: ");
// lcd_1.print(level);
// lcd_1.print(" Scr: ");
// lcd_1.print(score);
// lcd_1.setCursor(0, 1);
// lcd_1.print("High Scr: ");
// lcd_1.print(highscore);
// // // set random LED ON
// digitalWrite(relays[randset], 1);
// strttime = millis();
// // // Wait for user input or timeout
// while (1) {
// if (millis() - strttime > interval) {
// Serial.println("timeup");
// break;
// }
// statusSum = 0;
// for (int i = 0; i < 4; i++) {
// buttonstatus[i] = digitalRead(buttons[i]);
// statusSum += buttonstatus[i];
// }
// if (statusSum != 4 ) break;
// }
// // // Check user input and update game state
// if (statusSum != 4 && buttonstatus[randset] == 1) {
// updatescore();
// } else if (statusSum != 4 && buttonstatus[randset] != 1) {
// gameover();
// } else if (statusSum == 4) {
// gameover();
// }
// // // Turn off all LEDs and delay before next iteration
// for (int i = 0; i < 4; i++) {
// digitalWrite(relays[i], LOW);
// }
// delay(1000);
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
void listen_push_buttons(){
//--------------------------------------------------------------------------
if(digitalRead(buttons[0]) == LOW){
delay(200);
control_relay(1);
Blynk.virtualWrite(button1_vpin, relay1_state); //update button state
}
//--------------------------------------------------------------------------
else if (digitalRead(buttons[1]) == LOW){
delay(200);
control_relay(2);
Blynk.virtualWrite(button2_vpin, relay2_state); //update button state
}
//--------------------------------------------------------------------------
else if (digitalRead(buttons[2]) == LOW){
delay(200);
control_relay(3);
Blynk.virtualWrite(button3_vpin, relay3_state); //update button state
}
//--------------------------------------------------------------------------
else if (digitalRead(buttons[3]) == LOW){
delay(200);
control_relay(4);
Blynk.virtualWrite(button4_vpin, relay4_state); //update button state
}
//--------------------------------------------------------------------------
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
void control_relay(int relay){
//------------------------------------------------
if(relay == 1){
relay1_state = !relay1_state;
digitalWrite(relays[0], relay1_state);
Serial.print("Relay1 State = ");
Serial.println(relays[0]);
delay(50);
}
//------------------------------------------------
else if(relay == 2){
relay2_state = !relay2_state;
digitalWrite(relays[1], relay2_state);
delay(50);
}
//------------------------------------------------
else if(relay == 3){
relay3_state = !relay3_state;
digitalWrite(relays[2], relay3_state);
delay(50);
}
//------------------------------------------------
else if(relay == 4){
relay4_state = !relay4_state;
digitalWrite(relays[3], relay4_state);
delay(50);
}
//------------------------------------------------
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
void gameover() {
// Display game over message, play buzzer, and reset game state
for (int i = 0; i < 4; i++) {
digitalWrite(relays[i], 1);
}
Serial.println("\n*******GAME OVER*******");
lcd_1.setCursor(0, 1);
lcd_1.print("** GAME OVER **");
Serial.print("Score: ");
Serial.println(score);
Serial.print("level: ");
Serial.println(level);
// play the gameover tone
tone(buzzer, 800);
delay(150);
noTone(buzzer);
delay(10);
tone(buzzer, 850);
delay(200);
noTone(buzzer);
delay(5000);
// update highscore if made
score > highscore ? highscore = score : highscore = highscore;
// reset the scores for next game
score = 0;
level = 1;
count = 0;
interval = defultinterval;
lcd_1.setCursor(0, 0);
lcd_1.print(" Restart: Green ");
// hold until green button is pressed
while (digitalRead(buttons[1]) == 1) {}
lcd_1.setCursor(0, 1);
lcd_1.print(" ");
lcd_1.setCursor(0, 1);
lcd_1.print(" Starting in: ");
lcd_1.print("3");
delay(1000);
lcd_1.setCursor(14, 1);
lcd_1.print("2");
delay(1000);
lcd_1.setCursor(14, 1);
lcd_1.print("1");
delay(1000);
lcd_1.clear();
}
void updatescore() {
// Update score and play a sound
tone(buzzer, 700);
delay(80);
noTone(buzzer);
Serial.println("\n**Score Up**");
count += 1;
score += 1;
Serial.print("Score: ");
Serial.println(score);
Serial.print("level: ");
Serial.println(level);
}