#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 = 7; // 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 countdownActive = false;
bool countdownActive2 = false;
unsigned long countdownStartTime = 0;
unsigned long countdownStartTime2 = 0;
const unsigned long countdownDuration = 15000; // 15 seconds
const unsigned long countdownDuration2 = 10000; // 10 seconds
bool accessGranted = false;
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 4 Coin");
}
void loop() {
// Case 1: Access Masuk
if (!accessGranted) {
if (digitalRead(irProximity1) == LOW) {
halanganCount1++;
if (halanganCount1 == 1) {
countdownActive = true;
countdownStartTime = millis();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan 3 Coin");
} else if (halanganCount1 == 2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan 2 Coin");
} else if (halanganCount1 == 3) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan 1 Coin");
} else if (halanganCount1 == 4) {
countdownActive = false;
countdownActive2 = true;
countdownStartTime2 = millis();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Silahkan Pilih");
lcd.setCursor(4, 1);
lcd.print("1-2");
accessGranted = true;
}
}
if (countdownActive) {
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - countdownStartTime;
if (elapsedTime >= countdownDuration) {
countdownActive = false;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Maaf...");
lcd.setCursor(2, 1);
lcd.print("Waktu Habis");
servo2.write(180);
delay(2000);
servo2.write(90);
digitalWrite(buzzerPin, HIGH);
delay(2000);
// delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan 4 Coin");
halanganCount1 = 0;
} else {
int remainingTime = (countdownDuration - elapsedTime) / 1000;
lcd.setCursor(0, 1);
lcd.print("waktu: " + String(remainingTime) + " detik ");
}
}
if (countdownActive2) {
unsigned long currentTime2 = millis();
unsigned long elapsedTime2 = currentTime2 - countdownStartTime2;
if (elapsedTime2 >= countdownDuration2) {
countdownActive2 = false;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Maaf...");
lcd.setCursor(2, 1);
lcd.print("Waktu Habis");
servo2.write(180);
delay(2000);
servo2.write(90);
digitalWrite(buzzerPin, HIGH);
delay(2000);
// delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan 4 Coin");
halanganCount1 = 0;
} else {
int remainingTime2 = (countdownDuration2 - elapsedTime2) / 1000;
lcd.setCursor(0, 1);
lcd.print("waktu: " + String(remainingTime2) + " detik ");
}
}
} else {
// Case 2: Proses Program
char key = keypad.getKey();
if (key == '1') {
countdownActive2 = false;
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 4 Coin");
halanganCount1 = 0;
accessGranted = false;
}
}
}
}