#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// START OF SETUP
#define DHTPIN 15
#define DHTTYPE DHT22
#define BUZZERPIN 13
#define BUTTONPOWERPIN 32
#define BUTTONSETTINGPIN 33
#define BUTTONPLUSPIN 25
#define BUTTONMINUSPIN 26
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool isSystemOn = false;
bool isCelsius = true;
bool isMuted = false;
float thresholdFlu = 37.5f;
int currentMenu = 0;
bool isMenuSettingActive = false;
unsigned long startPressTime = 0;
bool isHolding = false;
const int holdDuration = 1500;
void setup() {
Serial.begin(115200);
dht.begin();
lcd.init();
currentMenu = 0;
isMenuSettingActive = false;
isSystemOn = false;
pinMode(BUZZERPIN, OUTPUT);
// Semua tombol menggunakan INPUT_PULLUP
pinMode(BUTTONPOWERPIN, INPUT_PULLUP);
pinMode(BUTTONSETTINGPIN, INPUT_PULLUP);
pinMode(BUTTONPLUSPIN, INPUT_PULLUP);
pinMode(BUTTONMINUSPIN, INPUT_PULLUP);
}
// END OF SETUP
// START OF LOOP
void loop() {
int digitalReadButtonPower = digitalRead(BUTTONPOWERPIN);
if (digitalReadButtonPower == LOW){
if (startPressTime == 0) startPressTime = millis();
// Deteksi Hold (Power Toggle)
if ((millis() - startPressTime > holdDuration) && !isHolding) {
isHolding = true;
if (isSystemOn) {
matikanSistem();
} else {
nyalakanSistem();
}
}
} else {
if (startPressTime > 0) {
// Deteksi Klik Singkat (Hanya jika sistem ON dan tidak hold)
if (!isHolding && isSystemOn && !isMenuSettingActive) {
tampilkanSuhu(dht.readTemperature(), isCelsius);
}
startPressTime = 0;
isHolding = false;
}
}
if (isSystemOn){
if (isMenuSettingActive){
// BUTTON SETTING CONFIGURATION PLUS MINUS
int digitalReadButtonPlus = digitalRead(BUTTONPLUSPIN);
int digitalReadButtonMinus = digitalRead(BUTTONMINUSPIN);
if (digitalReadButtonPlus == LOW){
konfigurasiThermo(currentMenu, true);
tampilkanMenuSetting(currentMenu);
delay(300);
}
if (digitalReadButtonMinus == LOW){
konfigurasiThermo(currentMenu, false);
tampilkanMenuSetting(currentMenu);
delay(300);
}
}
// BUTTON SETTING THERMO MENU
int digitalReadButtonSetting = digitalRead(BUTTONSETTINGPIN);
if (digitalReadButtonSetting == LOW){
currentMenu++;
if (currentMenu == 4){
currentMenu = 0;
isMenuSettingActive = false;
} else {
isMenuSettingActive = true;
}
if (isMenuSettingActive){
tampilkanMenuSetting(currentMenu);
} else {
tampilkanSuhu(dht.readTemperature(), isCelsius);
}
delay(300);
}
}
}
// END OF LOOP
// START OF SYSTEM ON / OFF
void nyalakanSistem() {
isSystemOn = true;
lcd.backlight();
lcd.clear();
lcd.print("Sistem ON");
tone(BUZZERPIN, 2000, 200);
delay(1000);
lcd.clear();
lcd.print("Ready to Scan");
}
// --- FUNGSI POWER MATI ---
void matikanSistem() {
lcd.clear();
lcd.print("Good Bye...");
tone(BUZZERPIN, 1000, 500);
delay(1500);
isSystemOn = false;
isMenuSettingActive = false;
currentMenu = 0;
lcd.clear();
lcd.noBacklight(); // Matikan lampu LCD untuk hemat baterai
}
// END OF SYSTEM ON / OFF
// START OF LCD THERMOMETER
void tampilkanSuhu(float suhu, bool isCelsius){
lcd.setCursor(0, 0);
lcd.print("Temp : ");
float suhuTampil = isCelsius ? suhu : (suhu * 1.8f) + 32.0f;
lcd.print(suhuTampil, 1);
if (isCelsius) {
lcd.print("\xDF""C ");
} else {
lcd.print("\xDF""F ");
}
statusDeteksiSuhu(suhu);
if (!isMuted){
bunyikanToneDeteksiSuhu(suhu);
}
}
void statusDeteksiSuhu(float suhu){
lcd.setCursor(0, 1);
if (suhu < 35.0f) {
lcd.print("Status: RENDAH ");
} else if (suhu < thresholdFlu){
lcd.print("Status: NORMAL ");
} else {
lcd.print("Status: DEMAM ");
}
}
// END OF LCD THERMOMETER
// START OF BUZZER OR SOUND NOTIF
void bunyikanToneDeteksiSuhu(float suhu){
if (suhu >= thresholdFlu){
for (int i = 0; i < 3; i++){
tone(BUZZERPIN, 1400);
delay(80);
noTone(BUZZERPIN);
delay(50);
}
} else {
tone(BUZZERPIN, 1400);
delay(200);
noTone(BUZZERPIN);
}
}
// END OF BUZZER OR SOUND NOTIF
// START OF SETTING THERMOMETER
void tampilkanMenuSetting(int menuIndex){
switch (menuIndex){
case 1:
lcd.setCursor(0, 0);
lcd.print("Fungsi 1 ");
lcd.setCursor(0, 1);
if (isCelsius){
lcd.print("Celsius ");
} else {
lcd.print("Fahrenheit ");
}
break;
case 2:
lcd.setCursor(0, 0);
lcd.print("Fungsi 2 ");
lcd.setCursor(0, 1);
lcd.print("Demam: ");
lcd.print(thresholdFlu, 1);
lcd.print("\xDF""C");
break;
case 3:
lcd.setCursor(0, 0);
lcd.print("Fungsi 3 ");
lcd.setCursor(0, 1);
lcd.print("Bunyi: ");
if (isMuted){
lcd.print("OFF ");
} else {
lcd.print("ON ");
}
break;
}
}
void konfigurasiThermo(int menuIndex, bool isPlus){
switch (menuIndex){
case 1:
isCelsius = !isCelsius;
break;
case 2:
if (isPlus){
thresholdFlu = thresholdFlu + 0.1f;
} else {
thresholdFlu = thresholdFlu - 0.1f;
}
break;
case 3:
isMuted = !isMuted;
break;
}
}
// END OF SETTING THERMOMETER