#define BLYNK_TEMPLATE_ID "TMPL6WcgzySEw"
#define BLYNK_TEMPLATE_NAME "Project UAS"
#define BLYNK_AUTH_TOKEN "3EGVpIkdkhKv8GnYI07TGb8zcTZIF6bz"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
// Define constants for the ultrasonic sensor and buzzer
#define TRIG_PIN 2
#define ECHO_PIN 15
#define BUZZER_PIN 5 // Added buzzer pin definition
// Constant for the new virtual pin
#define DISTANCE_VIRTUAL_PIN V1
char ssid[] = "Wokwi-GUEST"; // Adjust with your WiFi SSID
char pass[] = ""; // Adjust with your WiFi password
BlynkTimer timer;
Servo jemuran_1;
// Global variable for clothesline control
int ldr, hujan;
bool status = false;
bool previousStatus = false; // New variable to store the previous status
// Variable for parking mode
bool parkingMode = true; // Assume parking mode is active by default
// Variable to store distance data as integer
int currentDistance = 0;
void setupLEDC() {
ledcSetup(0, 2000, 8);
ledcAttachPin(BUZZER_PIN, 0);
pinMode(BUZZER_PIN, OUTPUT); // Added pin mode for buzzer
}
void checkDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.0343 / 2;
currentDistance = static_cast<int>(distance); // Convert float to integer
Blynk.virtualWrite(DISTANCE_VIRTUAL_PIN, currentDistance);
if (parkingMode && currentDistance <= 50) {
ledcWrite(0, 125); // Activate buzzer
Blynk.virtualWrite(V4, 255);
Blynk.virtualWrite(V3, 0);
Serial.println("The car is close to the wall");
digitalWrite(BUZZER_PIN, HIGH); // Activate buzzer on pin D5
} else {
ledcWrite(0, 0); // Turn off the buzzer
Blynk.virtualWrite(V3, 255);
Blynk.virtualWrite(V4, 0);
Serial.println("The car can still move backward");
digitalWrite(BUZZER_PIN, LOW); // Deactivate buzzer on pin D5
}
}
BLYNK_WRITE(V0) {
int pinValue = param.asInt();
if (pinValue == 1 && !previousStatus) {
status = true;
jemuran_1.write(0); // Buka clothesline
Serial.println("Clothesline Open");
} else if (pinValue == 0 && previousStatus) {
status = false;
jemuran_1.write(180); // Tutup clothesline
Serial.println("Clothesline Close");
}
previousStatus = status;
}
void setup() {
Serial.begin(9600);
setupLEDC();
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
jemuran_1.attach(25);
jemuran_1.write(180);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(1000L, checkDistance);
}
void loop() {
Blynk.run();
timer.run();
ldr = map(analogRead(34), 0, 4096, 100, 0);
hujan = map(analogRead(35), 0, 4096, 0, 100);
if (status == true) {
jemuran_1.write(0);
Serial.println("Clothesline Open");
} else {
if (ldr < 30 || hujan > 40) {
jemuran_1.write(180);
Serial.println("Clothesline Close");
}
}
}