#define BLYNK_TEMPLATE_ID "TMPL6ImzkiZup"
#define BLYNK_TEMPLATE_NAME "EPS32 with 2 LED and Servo and Buzzer"
#define BLYNK_AUTH_TOKEN "odd7I1TGPy2Cr0Wrd_Up6xW37Xr-jSZQ"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN ; // Your Blynk Auth Token
char ssid[] = "Wokwi-GUEST"; // Your WiFi SSID
char pass[] = ""; // Your WiFi Password
#define RED_LED_PIN 12 // Pin for the red LED
#define BLUE_LED_PIN 13 // Pin for the blue LED
#define BUZZER_PIN 14 // Pin for the buzzer
#define SERVO_PIN 15 // Pin for the servo motor
BlynkTimer timer;
int buzzerFrequency = 1000; // Initial frequency for the buzzer
int servoPosition = 0; // Initial position for the servo
bool servoState = false;
bool ledState = false;
bool buzzerState = false;
bool servoButtonState = false;
unsigned long previousMillis = 0;
const long interval = 30000; // Interval for LED blinking and buzzer (30 seconds)
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
Blynk.run();
timer.run();
unsigned long currentMillis = millis();
// Control servo motor
if (servoState) {
moveServo(90); // Move servo to 90 degrees
} else {
moveServo(0); // Move servo to 0 degrees
}
// Control LED blinking
if (servoButtonState && (currentMillis - previousMillis >= interval)) {
previousMillis = currentMillis;
if (ledState) {
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
ledState = false;
} else {
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(BLUE_LED_PIN, HIGH);
ledState = true;
}
}
// Control buzzer
if (servoButtonState && (currentMillis - previousMillis >= interval)) {
if (buzzerState) {
tone(BUZZER_PIN, buzzerFrequency); // Turn on buzzer with frequency
} else {
noTone(BUZZER_PIN); // Turn off buzzer
}
buzzerState = !buzzerState; // Toggle buzzer state
delay(100); // Wait for buzzer to complete tone
}
}
void moveServo(int angle) {
ledcSetup(0, 50, 16); // Configure LED Controller
ledcAttachPin(SERVO_PIN, 0); // Attach servo pin to LED Controller
int dutyCycle = map(angle, 0, 180, 1000, 2000); // Map angle to duty cycle
ledcWrite(0, dutyCycle); // Write duty cycle to servo pin
}
// Handler for the servo button in Blynk app
BLYNK_WRITE(V1) {
servoButtonState = param.asInt(); // Get servo button state (0 or 1)
}
// Handler for the red LED button in Blynk app
BLYNK_WRITE(V2) {
int redLedState = param.asInt();
digitalWrite(RED_LED_PIN, redLedState);
}
// Handler for the blue LED button in Blynk app
BLYNK_WRITE(V3) {
int blueLedState = param.asInt();
digitalWrite(BLUE_LED_PIN, blueLedState);
}