#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
Servo servo1;
Servo servo2;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int irProximity1 = 2; // Sensor Infrared Proximity 1
const int irProximity2 = 3; // Sensor Infrared Proximity 2
const int buzzerPin = 4; // Active Buzzer
const byte ROWS = 4; // Jumlah baris keypad
const byte COLS = 4; // Jumlah kolom keypad
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; // Ke pin baris keypad
byte colPins[COLS] = {13, 12, 11, 10}; // Ke pin kolom keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int halanganCount1 = 0;
bool accessGranted = false;
bool countdownActive = false;
unsigned long countdownStartTime = 0;
const unsigned long countdownDuration = 10000; // 10 seconds
void setup() {
servo1.attach(5); // Servo 1
servo2.attach(6); // Servo 2
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Selamat Datang");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan 3 Coin");
}
void loop() {
if (!accessGranted) {
// Case 1: Access Masuk
char key = keypad.getKey();
if (key == '1') {
if (digitalRead(irProximity1) == LOW) {
halanganCount1++;
if (halanganCount1 == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan 2 Coin");
countdownActive = true;
countdownStartTime = millis();
} else if (halanganCount1 == 2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan 1 Coin");
} else if (halanganCount1 == 3) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Silahkan Pilih");
accessGranted = true;
countdownActive = false;
}
}
}
if (countdownActive) {
unsigned long currentTime = millis();
if (currentTime - countdownStartTime >= countdownDuration) {
countdownActive = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waktu Habis");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan 3 Coin");
halanganCount1 = 0;
} else {
int remainingTime = (countdownDuration - (currentTime - countdownStartTime)) / 1000;
lcd.setCursor(0, 1);
lcd.print("Countdown: " + String(remainingTime) + "s ");
}
}
} else {
// Case 2: Proses Program
char key = keypad.getKey();
if (key == '1') {
if (digitalRead(irProximity2) == LOW) {
servo1.write(120);
delay(1000);
servo1.write(0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Silahkan ambil, terimakasih");
delay(2000);
servo2.write(0);
delay(2000);
servo2.write(90);
accessGranted = false;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Maaf, barang habis");
servo2.write(180);
delay(2000);
servo2.write(90);
digitalWrite(buzzerPin, HIGH);
delay(2000);
digitalWrite(buzzerPin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan 3 Coin");
halanganCount1 = 0;
accessGranted = false;
}
}
}
}