#define BLYNK_PRINT Serial
// Define Blynk configuration details first
#define BLYNK_TEMPLATE_ID "TMPL6n80WhwTc"
#define BLYNK_TEMPLATE_NAME "MILLIS"
#define BLYNK_AUTH_TOKEN "91_OuGVdLeHZ8A9fj4epx4EI2Ec-ekws"
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <BlynkSimpleEsp32.h> // Include the Blynk library for ESP32
// Wi-Fi credentials
char ssid[] = "Wokwi-GUEST"; // Replace with your Wi-Fi SSID
char pass[] = ""; // Replace with your Wi-Fi password
// Define LED pins
int led1 = 5;
int led2 = 18;
// Variables for timing LED2
unsigned long led2Millis; // To track when LED2 was turned on
int led2Duration = 5000; // Duration for LED2 to stay on (5 seconds)
int led2Active = false;
void setup() {
// Set up the LED pins as outputs
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.begin(115200);
// Initialize Blynk with the authentication token and Wi-Fi credentials
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}
// Function to control led1 using Blynk virtual button V1
BLYNK_WRITE(V0) {
int led1State = param.asInt(); // Read the state of virtual button V1
digitalWrite(led1, led1State); // Set led1 based on the button state
}
// Function to control led2 using Blynk virtual button V2
BLYNK_WRITE(V1) {
int led2State = param.asInt(); // Read the state of virtual button V2
if (led2State == 1) { // If button V2 is pressed
digitalWrite(led2, HIGH); // Turn on led2
led2Millis = millis(); // Start timer for led2
led2Active = true; // Set flag to indicate led2 is active
}
}
void loop() {
Blynk.run(); // Run Blynk connection handler
// Check if led2 needs to be turned off after 5 seconds
if (led2Active && (millis() - led2Millis >= led2Duration)) {
digitalWrite(led2, LOW); // Turn off led2
led2Active = false; // Reset led2 active status
}
}