#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the LCD
float temp = 30; // Example temperature value
float humidity = 24; // Example humidity value
const int BUTTON_PIN = 7; // Tombol untuk lock/unlock
int buttonState = HIGH; // State tombol saat ini
int lastButtonState = HIGH; // State tombol sebelumnya
unsigned long lastDebounceTime = 0; // Waktu terakhir tombol ditekan
const unsigned long debounceDelay = 50; // Debounce delay
// Menu variables
int currentMenu = 0; // Tracks the current menu
int totalMenus = 3; // Total number of menus
// Timer variables
unsigned long previousMillis = 0; // Stores the last time the menu was updated
const long interval = 5000; // Interval for changing the menu (5 seconds)
bool isLocked = false; // Status lock menu
void setup() {
Serial.begin(9600);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Display the first menu
displayMenu(currentMenu);
// Initialize the button
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time
// Baca state tombol
int reading = digitalRead(BUTTON_PIN);
// Debounce tombol
if (reading != lastButtonState) {
lastDebounceTime = currentMillis;
}
if ((currentMillis - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// Jika tombol ditekan (LOW karena menggunakan INPUT_PULLUP)
if (buttonState == LOW) {
isLocked = !isLocked; // Toggle status lock
// if (isLocked) {
// lcd.setCursor(0, 1);
// lcd.print("LOCKED "); // Tampilkan status terkunci
// } else {
// lcd.setCursor(0, 1);
// lcd.print("UNLOCKED "); // Tampilkan status terbuka
// }
}
}
}
lastButtonState = reading;
// Jika tidak terkunci, lanjutkan perpindahan menu otomatis
if (!isLocked) {
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Simpan waktu terakhir menu diubah
// Pindah ke menu berikutnya
currentMenu = (currentMenu + 1) % totalMenus;
displayMenu(currentMenu);
}
}
}
// void loop() {
// unsigned long currentMillis = millis(); // Get the current time
// // Check if 5 seconds have passed
// if (currentMillis - previousMillis >= interval) {
// previousMillis = currentMillis; // Save the last time the menu was updated
// // Move to the next menu
// currentMenu = (currentMenu + 1) % totalMenus;
// displayMenu(currentMenu);
// }
// }
// Function to display the menu based on the currentMenu index
void displayMenu(int menuIndex) {
lcd.clear(); // Clear the LCD before displaying new content
switch (menuIndex) {
case 0:
lcd.setCursor(0, 0);
lcd.print("Tm ");
lcd.print(temp);
lcd.setCursor(7,0);
lcd.print("|Cd ");
lcd.setCursor(0, 1);
lcd.print("Hm ");
lcd.print(humidity);
lcd.setCursor(7,1);
lcd.print("|Ph ");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print("N ");
lcd.print(temp);
lcd.setCursor(7,0);
lcd.print("|P ");
lcd.setCursor(0, 1);
lcd.print("K ");
lcd.print(humidity);
lcd.setCursor(7,1);
lcd.print(" ");
break;
case 2:
// Display a custom message
lcd.setCursor(0, 0);
lcd.print("Tegangan");
lcd.setCursor(0, 1);
lcd.print("Arus");
break;
default:
lcd.setCursor(0, 0);
lcd.print("Invalid Menu");
break;
}
}