#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Keypad.h>
Servo servo1;
Servo servo2;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat LCD I2C dan ukuran (sesuaikan)
const int buzzerPin = 9; // Pin untuk 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 );
const int IRProximity1 = 6; // Pin untuk Sensor IR Proximity 1
const int IRProximity2 = 7; // Pin untuk Sensor IR Proximity 2
int halanganCount1 = 0;
int timer = 10; // Waktu mundur 10 detik
bool accessGranted = false;
void setup() {
servo1.attach(8); // Servo 1 dihubungkan ke pin 8
servo2.attach(9); // Servo 2 dihubungkan ke pin 9
pinMode(IRProximity1, INPUT);
pinMode(IRProximity2, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Selamat Datang");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan 3 Coin");
lcd.setCursor(0, 1);
lcd.print("Waktu: 10s");
}
void loop() {
char key = keypad.getKey();
int irValue1 = digitalRead(IRProximity1);
int irValue2 = digitalRead(IRProximity2);
if (timer > 0 && halanganCount1 < 3) {
// Menghitung waktu mundur jika masih dalam case 1 dan belum mencapai 3 halangan
lcd.setCursor(0, 1);
lcd.print("Waktu: " + String(timer) + "s ");
delay(1000);
timer--;
}
if (halanganCount1 == 3) {
// Jika sudah ada 3 halangan di Sensor IR Proximity1
accessGranted = true;
lcd.clear();
lcd.print("Silahkan Pilih 1-2");
}
if (timer == 0 && !accessGranted) {
// Waktu habis dan belum mencapai 3 halangan
lcd.clear();
lcd.print("Waktu Habis");
delay(2000);
lcd.clear();
lcd.print("Masukkan 3 Coin");
timer = 10; // Reset timer
}
if (accessGranted) {
if (key == '1') {
if (irValue2 == LOW) {
// Jika Sensor IR Proximity2 terhalang
servo1.write(120);
delay(1000);
servo1.write(0);
lcd.clear();
lcd.print("Silahkan ambil");
delay(2000);
servo2.write(0);
delay(2000);
servo2.write(90); // Kembalikan servo2 ke posisi awal (90 derajat)
lcd.clear();
lcd.print("Masukkan 3 Coin");
halanganCount1 = 0; // Reset jumlah halangan
timer = 10; // Reset timer
accessGranted = false; // Reset akses
} else {
lcd.clear();
lcd.print("Maaf, barang habis");
servo2.write(180);
delay(2000);
servo2.write(90); // Kembalikan servo2 ke posisi awal (90 derajat)
lcd.clear();
lcd.print("Masukkan 3 Coin");
halanganCount1 = 0; // Reset jumlah halangan
timer = 10; // Reset timer
accessGranted = false; // Reset akses
}
}
}
}