#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneButton.h>
// Pin Rotary Encoder
#define CLK 2
#define DT 3
#define SW 4
// Ukuran LCD
#define LCD_ROWS 2
#define LCD_COLS 16
// Inisialisasi LCD I2C
LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS);
volatile int counter = 100; // Nilai Suhu terendah
volatile int lastCLKState = LOW;
volatile int position = 0; // Variabel posisi rotary encoder
// OneButton objects
OneButton enter(SW, true); // Pin SW rotary Encoder
bool displaySuhu = true; // True = Home Screen False = Menu & SubMenu
// Variabel untuk menu
int currentLayer = 0; // 0: Home, 1: Menu, 2: Sub Menu
int menuIndex = 0; // Logo ">"
int topIndex = 5; // Indeks item menu paling atas yang ditampilkan
const char* menuItems[] = {"Heater ", "Timer Heater ", "Timer Kipas ", "Kontak "};
const int menuItemCount = sizeof(menuItems) / sizeof(menuItems[0]);
void handleEncoder() { // Bagian pembacaan putaran rotary encoder
int CLKState = digitalRead(CLK);
int DTState = digitalRead(DT);
if (CLKState != lastCLKState && CLKState == HIGH && displaySuhu == true) {
if (DTState == LOW) {
counter++;
} else {
counter--;
}
}
if (CLKState != lastCLKState && CLKState == HIGH && !displaySuhu) {
if (DTState == LOW) {
position--;
} else {
position++;
}
}
if (counter >= 500) { counter = 500; } else if (counter <= 100) { counter = 100; } // Set nilai batas Counter
if (position >= 4) {position = 4;} else if (position <= 0) {position = 0;} // Set nilai batas Position
lastCLKState = CLKState;
}
void setup() {
// Initialize Serial
Serial.begin(9600);
// Initialize LCD
lcd.init();
lcd.backlight();
// Attach interrupt to encoder
attachInterrupt(digitalPinToInterrupt(CLK), handleEncoder, CHANGE);
// Konfigurasi OneButton
enter.setClickMs(200);
enter.setDebounceMs(25);
enter.attachClick(onButtonClick);
enter.attachDoubleClick(onButtonDoubleClick);
enter.attachLongPressStart(onButtonLongPressStart);
enter.attachMultiClick(onButtonTripleClick);
}
void loop() {
enter.tick(); // Periksa status tombol
// Menampilkan layar sesuai dengan layer saat ini
if (currentLayer == 0) {
//displaySuhu = true;
lcd.setCursor(0, 0);
lcd.print("Suhu: ");
lcd.println(counter);
//Serial.print("Pos: "); Serial.println(position); // Serial Debugging
} else if (currentLayer == 1) { displayMenu(); } // Menampilkan Menu
else if (currentLayer == 2) { adjustHeater(); } // Pengaturan Heater/panas
else if (currentLayer == 3) { timer_heater(); } // Pengaturan Timer Heater
else if (currentLayer == 4) { timer_kipas(); } // Pengaturan Timer Kipas
else if (currentLayer == 5) { kontak(); } // Menampilkan informasi cs
}
void displayMenu() {
displaySuhu = false;
//Serial.print("Pos: "); Serial.println(position); // Serial Debugging
menuIndex = constrain(position, 0, menuItemCount - 1);
if (menuIndex < topIndex) {
topIndex = menuIndex;
} else if (menuIndex > topIndex + 1) {
topIndex = menuIndex - 1;
}
lcd.setCursor(0, 0);
if (topIndex == menuIndex) {
lcd.print("> ");
} else {
lcd.print(" ");
}
lcd.print(menuItems[topIndex]);
lcd.setCursor(0, 1);
if (topIndex + 1 < menuItemCount) {
if (topIndex + 1 == menuIndex) {
lcd.print("> ");
} else {
lcd.print(" ");
}
lcd.print(menuItems[topIndex + 1]);
} else {
lcd.print(" "); // Kosongkan baris jika tidak ada item lagi
}
}
void adjustHeater() {
displaySuhu = true;
lcd.setCursor(0, 0);
lcd.print("Adjust Heater: ");
lcd.setCursor(0, 1);
lcd.print("Value: ");
lcd.print(counter);
lcd.print(" ");
}
void timer_heater() {
displaySuhu = false;
lcd.setCursor(0,0);
lcd.print("timer heater");
}
void timer_kipas() {
displaySuhu = false;
lcd.setCursor(0,0);
lcd.print("timer kipas");
}
void kontak() {
displaySuhu = false;
lcd.setCursor(0,0); lcd.print("Produk AB Blitar");
lcd.setCursor(0,1); lcd.print("0813 4709 0077");
}
// Fungsi untuk menangani klik tombol
void onButtonClick() {
if (currentLayer == 1) { // MENU
if (menuIndex == 0) { currentLayer = 2; lcd.clear(); } // Masuk submenu Heater
if (menuIndex == 1) { currentLayer = 3; lcd.clear(); } // Masuk submenu Timer Heater
if (menuIndex == 2) { currentLayer = 4; lcd.clear(); } // Masuk submenu Timer Kipas
if (menuIndex == 3) { currentLayer = 5; lcd.clear(); } // Masuk submenu informasi
}
//else if (currentLayer == 2) { currentLayer = 1; lcd.clear(); } // Kembali ke Menu setelah mengatur nilai Heater
//else if (currentLayer == 3) { currentLayer = 1; lcd.clear(); } // Kembali ke Menu setelah mengatur nilai Timer Heater
//else if (currentLayer == 4) { currentLayer = 1; lcd.clear(); } // Kembali ke Menu setelah mengatur nilai Timer Kipas
//else if (currentLayer == 5) { currentLayer = 1; lcd.clear(); } // Kembali ke Menu setelah melihat Informasi
}
/*
// Fungsi untuk menangani klik tombol
void onButtonClick() {
if (currentLayer == 1) {
if (menuIndex == 0) {
// Masuk ke Sub Menu untuk mengatur counter
currentLayer = 2;
lcd.clear();
} else {
// Tindakan untuk item menu lainnya
}
} else if (currentLayer == 2) {
// Kembali ke Menu setelah mengatur nilai counter
currentLayer = 1;
lcd.clear();
}
}
*/
// Fungsi untuk menangani penekanan tombol lama
void onButtonLongPressStart() {
displaySuhu = false;
if (currentLayer == 0) { currentLayer = 1; lcd.clear(); } // Masuk Menu setelah menahan tombol selama 3 detik
}
// Fungsi untuk menangani klik tombol dua kali
void onButtonDoubleClick() {
// Kembali ke Menu dari Sub Menu setelah klik dua kali
if (currentLayer == 2) { currentLayer = 1; lcd.clear(); } // Kembali ke Menu setelah mengatur nilai Heater
if (currentLayer == 3) { currentLayer = 1; lcd.clear(); } // Kembali ke Menu setelah mengatur nilai Timer Heater
if (currentLayer == 4) { currentLayer = 1; lcd.clear(); } // Kembali ke Menu setelah mengatur nilai Timer Kipas
if (currentLayer == 5) { currentLayer = 1; lcd.clear(); } // Kembali ke Menu setelah melihat Informasi
}
// Fungsi untuk menangani klik tombol tiga kali
void onButtonTripleClick() {
if (currentLayer == 1) { currentLayer = 0; lcd.clear(); } // Kembali ke Home dari Menu setelah klik tiga kali
}