#include <Wire.h>
#include <LiquidCrystal_I2C.h>
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
int halanganCount1 = 0;
bool countdownActive = false;
unsigned long countdownStartTime = 0;
const unsigned long countdownDuration = 10000; // 10 seconds
void setup() {
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 (halanganCount1 < 3) {
if (digitalRead(irProximity1) == LOW) {
halanganCount1++;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan Coin");
if (halanganCount1 == 1) {
countdownActive = true;
countdownStartTime = millis();
lcd.setCursor(0, 1);
lcd.print("Countdown: 10s");
}
if (halanganCount1 == 3) {
countdownActive = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Silahkan Pilih");
}
}
} else {
countdownActive = false;
}
if (countdownActive) {
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - countdownStartTime;
if (elapsedTime >= countdownDuration) {
countdownActive = false;
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Maaf...");
lcd.setCursor(2, 1);
lcd.print("Waktu Habis");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan 3 Coin");
halanganCount1 = 0;
} else {
int remainingTime = (countdownDuration - elapsedTime) / 1000;
lcd.setCursor(0, 1);
lcd.print("Waktu: " + String(remainingTime) + " detik ");
}
}
}