#include <LiquidCrystal.h>
#include <IRremote.h>
// Inisialisasi LCD dengan pin yang digunakan
const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int button1Pin = 2; // Push Button 1 untuk On/Off timer
const int button2Pin = 3; // Push Button 2 untuk Play/Pause timer
int timerRunning = 0; // Status Timer (0: Off, 1: On)
int timerPaused = 1; // Status Timer (0: Play, 1: Pause)
unsigned long startTime;
unsigned long currentTime;
unsigned long elapsedTime = 3600; // 60 menit dalam detik
// Inisialisasi IR Receiver
#define PIN_RECEIVER 13 // Signal Pin of IR receiver
IRrecv receiver(PIN_RECEIVER);
decode_results results;
unsigned long lastIRTime = 0; // Waktu terakhir tombol IR ditekan
const unsigned long irDebounceDelay = 300; // Debounce delay untuk IR (300ms)
void setup() {
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Timer:");
lcd.setCursor(0, 1);
displayTime(elapsedTime);
receiver.enableIRIn(); // Start the receiver
}
void loop() {
static unsigned long lastDebounceTime1 = 0;
static unsigned long lastDebounceTime2 = 0;
unsigned long debounceDelay = 200;
// IR Remote handling
if (receiver.decode()) {
unsigned long currentTime = millis();
if ((currentTime - lastIRTime) > irDebounceDelay) {
int command = receiver.decodedIRData.command; // variable receiver IR Remote command
if (command == 162) { // Tombol ON/OFF IR
Serial.println(command);
toggleTimer();
}
if (command == 169) { // Tombol PLAY/PAUSE IR
Serial.println(command);
togglePause();
}
else {
Serial.println("Command tidak ditemukan");
}
lastIRTime = currentTime; // Update waktu terakhir tombol ditekan
}
receiver.resume();
}
// Push Button handling
if ((millis() - lastDebounceTime1) > debounceDelay && digitalRead(button1Pin) == LOW) {
lastDebounceTime1 = millis();
toggleTimer();
}
if ((millis() - lastDebounceTime2) > debounceDelay && digitalRead(button2Pin) == LOW) {
lastDebounceTime2 = millis();
togglePause();
}
// Timer running logic
if (timerRunning == 1 && timerPaused == 0) {
currentTime = millis();
unsigned long remainingTime = elapsedTime - (currentTime - startTime) / 1000;
if (remainingTime <= 0) {
timerRunning = 0;
remainingTime = 0;
}
lcd.setCursor(0, 1);
displayTime(remainingTime);
}
}
void toggleTimer() {
if (timerRunning == 0) {
timerRunning = 1;
timerPaused = 1;
} else {
timerRunning = 0;
elapsedTime = 3600; // Reset ke 60 menit
lcd.setCursor(0, 1);
displayTime(elapsedTime);
}
updateStatus();
}
void togglePause() {
if (timerRunning == 1) {
if (timerPaused == 1) {
timerPaused = 0;
startTime = millis();
} else {
timerPaused = 1;
elapsedTime -= (millis() - startTime) / 1000;
}
updateStatus();
}
}
void displayTime(unsigned long timeInSeconds) {
unsigned long minutes = timeInSeconds / 60;
unsigned long seconds = timeInSeconds % 60;
lcd.setCursor(8, 0);
if (minutes < 10) lcd.print("0");
lcd.print(minutes);
lcd.print(":");
if (seconds < 10) lcd.print("0");
lcd.print(seconds);
}
void updateStatus() {
lcd.setCursor(0, 1);
if (timerRunning == 1) {
lcd.print("ON ");
} else {
lcd.print("OFF");
}
lcd.setCursor(4, 1);
if (timerPaused == 1) {
lcd.print("PAUSE");
} else {
lcd.print("PLAY ");
}
}