#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 awal 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 Coin");
lcd.setCursor(0, 1);
lcd.print("Waktu: " + String(timer) + "s");
}
void loop() {
char key = keypad.getKey();
int irValue1 = digitalRead(IRProximity1);
int irValue2 = digitalRead(IRProximity2);
if (!accessGranted) {
if (irValue1 == LOW) {
// Sensor IR Proximity 1 terhalang
if (halanganCount1 == 0) {
lcd.setCursor(0, 1);
lcd.print("Waktu: " + String(timer) + "s ");
}
halanganCount1++;
if (halanganCount1 == 3) {
accessGranted = true;
lcd.clear();
lcd.print("Silahkan Pilih 1-2");
}
}
if (timer > 0) {
lcd.setCursor(0, 1);
lcd.print("Waktu: " + String(timer) + "s ");
delay(1000);
timer--;
}
if (timer == 0 && !accessGranted) {
lcd.clear();
lcd.print("Waktu Habis");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan Coin");
timer = 10;
}
}
if (accessGranted) {
if (key == '1') {
if (irValue2 == LOW) {
// Tombol '1' ditekan dan Sensor IR Proximity 2 terhalang
servo1.write(120); // Gerakan servo 1
delay(1000);
servo1.write(0); // Kembali ke posisi awal
lcd.clear();
lcd.print("Silahkan ambil");
delay(2000);
servo2.write(0); // Gerakan servo 2 ke 0 derajat
delay(2000);
lcd.clear();
lcd.print("Masukkan Coin");
accessGranted = false;
} else {
lcd.clear();
lcd.print("Maaf, barang habis");
servo2.write(180); // Gerakan servo 2 ke 180 derajat
delay(2000);
servo2.write(90); // Kembali ke posisi awal
delay(2000);
lcd.clear();
lcd.print("Masukkan Coin");
accessGranted = false;
}
}
}
}