#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Inisialisasi LCD dengan alamat I2C (sesuaikan dengan alamat LCD Anda, misalnya 0x27 atau 0x3F)
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int startButton = 2;
const int upButton = 3;
const int downButton = 4;
int counter = 0;
bool isStarted = false; // Status apakah program aktif atau tidak
void setup() {
// Atur pin mode untuk tombol
pinMode(startButton, INPUT);
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
// Inisialisasi LCD
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Press Start"); // Tampilkan pesan awal
}
void loop() {
// Periksa tombol start
if (digitalRead(startButton) == HIGH) {
isStarted = true; // Aktifkan program
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Counter Value:");
updateLCD();
delay(200); // Debounce sederhana
}
// Jika program aktif, periksa tombol up dan down
if (isStarted) {
// Periksa tombol up
if (digitalRead(upButton) == HIGH) {
counter++;
updateLCD();
delay(200); // Debounce sederhana
}
// Periksa tombol down
if (digitalRead(downButton) == HIGH) {
counter--;
updateLCD();
delay(200); // Debounce sederhana
}
}
}
// Fungsi untuk memperbarui nilai di LCD
void updateLCD() {
lcd.setCursor(0, 1);
lcd.print(" "); // Hapus nilai sebelumnya
lcd.setCursor(0, 1);
lcd.print(counter);
}