#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
const int buzzerPin = 10;
const int servoPin = 9;
const int buttonPin = 2;
const int ledPin = 13;
const int blinkingLedPin = 12;
const int totalSlots = 8;
Servo doorServo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
int currentSlots = totalSlots;
bool buttonPressed = false;
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(blinkingLedPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
doorServo.attach(servoPin);
doorServo.write(0);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Sisa Slot: ");
lcd.setCursor(0, 1);
lcd.print(currentSlots);
}
void loop() {
if (digitalRead(buttonPin) == LOW && !buttonPressed) {
buttonPressed = true;
if (currentSlots > 0) {
doorServo.write(90);
blinkingLed();
delay(1000);
doorServo.write(0);
delay(1000);
currentSlots--;
updateLCD();
stopBlinkingLed();
} else {
buzzBuzzer();
}
} else if (digitalRead(buttonPin) == HIGH && buttonPressed) {
buttonPressed = false;
}
}
void updateLCD() {
lcd.clear();
lcd.print("Sisa Slot: ");
lcd.setCursor(0, 1);
lcd.print(currentSlots);
}
void buzzBuzzer() {
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000);
delay(2000);
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
}
void blinkingLed() {
digitalWrite(blinkingLedPin, HIGH);
}
void stopBlinkingLed() {
digitalWrite(blinkingLedPin, LOW);
}