// Ultrasonic sensor pins
#define TRIG_PIN 27
#define ECHO_PIN 26
#define servoPin 14
#define LED2_PIN 19
#define BUZZER_PIN 23
#define LED_PIN 18
#define BLYNK_TEMPLATE_ID "TMPL67XgIKVBE"
#define BLYNK_TEMPLATE_NAME "uas"
#define BLYNK_AUTH_TOKEN "bZS2EHlJtKTufkhCa1ijMa_A9b5yME7w"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// LCD I2C Address
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myServo;
// Blynk credentials
char auth[] = "bZS2EHlJtKTufkhCa1ijMa_A9b5yME7w"; // Blynk token
char ssid[] = "Wokwi-GUEST"; // WiFi SSID
char pass[] = ""; // WiFi password
// Global variables
bool guardMode = false; // Guard Mode status
bool lampStatus = false; // Lamp status
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
myServo.attach(servoPin);
// Initialize LCD
lcd.init();
lcd.begin(16, 2);
lcd.backlight();
lcd.print("Mode:");
// Initialize pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Connect to WiFi and Blynk
Blynk.begin(auth, ssid, pass);
}
// Blynk function to control Guard Mode (V0)
BLYNK_WRITE(V0) {
guardMode = param.asInt(); // Read Guard Mode status from Blynk
if (guardMode) {
lcd.setCursor(0, 0);
lcd.print("Mode: GUARD ON ");
myServo.attach(servoPin); // Ensure servo is attached
myServo.write(90); // Open servo in Guard Mode
digitalWrite(LED_PIN, LOW); // Turn off LED in Guard Mode
} else {
lcd.setCursor(0, 0);
lcd.print("Mode: GUARD OFF");
myServo.attach(servoPin); // Ensure servo is attached for normal operation
digitalWrite(LED2_PIN, LOW); // Turn off LED2 when Guard Mode is OFF
noTone(BUZZER_PIN); // Ensure buzzer is off
}
}
// Blynk function to control Lamp (V1)
BLYNK_WRITE(V1) {
lampStatus = param.asInt(); // Read Lamp status from Blynk
digitalWrite(LED_PIN, lampStatus ? HIGH : LOW); // Turn lamp ON/OFF
}
// Main loop
void loop() {
Blynk.run(); // Run Blynk
// Ultrasonic sensor logic
long duration = 0;
int distance = 0;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Set duration and calculate distance
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
// Display distance on LCD
lcd.setCursor(0, 1);
lcd.print("jarak: ");
lcd.print(distance);
lcd.print(" cm ");
if (guardMode) {
// Guard Mode activated
if (distance > 0 && distance < 15) { // Detect object within 15 cm
digitalWrite(LED2_PIN, HIGH); // Turn on LED2 as indicator
tone(BUZZER_PIN, 3000, 100); // Buzzer ON
} else {
digitalWrite(LED2_PIN, LOW); // Turn off LED2
noTone(BUZZER_PIN); // Buzzer OFF
}
// Servo remains open at 90 degrees in Guard Mode
// LED_PIN remains off
} else {
// Normal Mode
// Control Servo and LED based on distance
if (distance > 0 && distance < 10) { // Close servo for object within 10 cm
myServo.write(0); // Close servo
digitalWrite(LED_PIN, HIGH); // LED ON when servo is closed
delay(3000);
} else {
myServo.write(90); // Open servo
if (!lampStatus) { // Only turn LED OFF if Blynk hasn't overridden it
digitalWrite(LED_PIN, LOW);
}
}
}
// Print distance to Serial Monitor
Serial.print("Distance: ");
Serial.print(distance); // Use distance variable
Serial.println(" cm");
delay(500); // Delay for stability
}