#define BLYNK_TEMPLATE_ID "TMPL6iwOONNVy"
#define BLYNK_TEMPLATE_NAME "cbcb"
#define BLYNK_AUTH_TOKEN "nJ4hU4M0S39fnUJ8ELnvaEhQPq0uU0o4"
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
int PB = 12;
int slot_available = 5;
BLYNK_WRITE(V0) {
digitalWrite(32, param.asInt());
}
#define trigPinOut 4
#define echoPinOut 16
#include <ESP32Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
bool portalClosed = false; // Tambahkan variabel ini
Servo motor_servo;
Servo servoOut;
void setup() {
// put your setup code here, to run once:
pinMode(32, OUTPUT);
Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
lcd.init();
pinMode(PB, INPUT_PULLUP);
motor_servo.attach(14);
motor_servo.write(0);
Serial.begin(9600);
Serial.println( "SISTEM PARKIR" );
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("PARKIR");
lcd.setCursor(2,1);
lcd.print("KELOMPOK 9");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("JUMLAH SLOT TERSEDIA");
lcd.setCursor(7, 1);
lcd.print(slot_available);
pinMode(trigPinOut, OUTPUT);
pinMode(echoPinOut, INPUT);
servoOut.attach(17);
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
timer.run();
if (digitalRead(PB) == LOW) {
if (slot_available > 0) {
portal_open_btn();
slot_available--;
show_VehicleSlot();
portalClosed = false;
if (slot_available == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PARKIR PENUH!!!");
}
delay(1000);
}
}
Blynk.virtualWrite(V1, slot_available);
long durationOut, distanceOut;
digitalWrite(trigPinOut, LOW);
delayMicroseconds(2000);
digitalWrite(trigPinOut, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPinOut, LOW);
durationOut = pulseIn(echoPinOut, HIGH);
distanceOut = (durationOut/2) / 29.1;
Serial.print("Jarak: ");
Serial.print(distanceOut);
Serial.println(" cm");
if (distanceOut <= 5) { // Jarak sensor
servoOut.write(180);
delay(1000);
if (!portalClosed) { // Tambahkan kondisi untuk memeriksa apakah pintu sudah tertutup sebelumnya
if(slot_available < 5)
{
slot_available++;
}
show_VehicleSlot();
portalClosed = true; // Set flag pintuTertutup menjadi true
}
} else {
servoOut.write(90); // Sudut
delay(1000);
portalClosed = false;
}
}
void show_VehicleSlot() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("JUMLAH SLOT TERSEDIA");
lcd.setCursor(7, 1);
lcd.print(slot_available);
}
void portal_open_btn() {
motor_servo.write(90);
delay(2000);
motor_servo.write(0);
}