#define BLYNK_TEMPLATE_ID "TMPL6tjMr4Ypq"
#define BLYNK_TEMPLATE_NAME "ESP32 WITH LED AND DOUBLE HORN SERVO"
#define BLYNK_AUTH_TOKEN "x_b7X5ygm5RLA5kUuci5CyFbI54Vo3uA"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// WiFi credentials
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Pins for LEDs
const int redLedPin = 13;
const int blueLedPin = 12;
// Pin for servo
const int servoPin = 14; // You can choose any available GPIO pin
// Servo object
Servo myServo;
// Variables to store LED states
int redLedState = LOW;
int blueLedState = LOW;
// Variable to store servo button state
int servoButtonState = LOW;
// Pin for LCD
const int lcdAddress = 0x27;
LiquidCrystal_I2C lcd(lcdAddress, 16, 2);
void setup() {
pinMode(redLedPin, OUTPUT);
pinMode(blueLedPin, OUTPUT);
// Initialize servo
myServo.attach(servoPin);
// Initialize LCD
lcd.begin(16, 2);
lcd.backlight();
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
Blynk.syncVirtual(V2); // Request the latest value of the servo button
// Move servo and blink LEDs if servo button is pressed
if (servoButtonState == HIGH) {
moveDoubleHornServo();
delay(1000); // Delay to ensure servo movement is completed before blinking LEDs
blinkLeds(30); // Blink LEDs continuously for 30 seconds
servoButtonState = LOW; // Reset the servo button state
// Display "Program Selesai" on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Program Selesai");
}
}
// Function to move servo in a double-horn shape
void moveDoubleHornServo() {
myServo.write(0); // Move to one horn position (0 degrees)
delay(500); // Adjust the delay based on the servo's speed
myServo.write(90); // Move to the center position (90 degrees)
delay(500); // Adjust the delay based on the servo's speed
myServo.write(180); // Move to the other horn position (180 degrees)
delay(500); // Adjust the delay based on the servo's speed
myServo.write(90); // Move back to the center position (90 degrees)
delay(500); // Adjust the delay based on the servo's speed
}
// Function to blink LEDs for a specified duration (in seconds)
void blinkLeds(int duration) {
unsigned long endTime = millis() + (duration * 1000);
while (millis() < endTime) {
digitalWrite(redLedPin, HIGH);
digitalWrite(blueLedPin, HIGH);
delay(500);
digitalWrite(redLedPin, LOW);
digitalWrite(blueLedPin, LOW);
delay(500);
}
}
// Blynk virtual pin handler for servo button
BLYNK_WRITE(V2) {
servoButtonState = param.asInt();
}