#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#define TEMP_SENSOR_PIN A0
#define BUZZER_PIN 10
#define LED_GREEN 11
#define LED_RED 12
// Inisialisasi LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C 0x27, ukuran LCD 16 kolom x 2 baris
// Keypad setup
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Baris keypad terhubung ke pin Arduino 9-6
byte colPins[COLS] = {5, 4, 3, 2}; // Kolom keypad terhubung ke pin Arduino 5-2
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int tempThreshold = 30; // Batas suhu default
int currentTemp;
int tempInput = 0; // Variabel sementara untuk input suhu dari keypad
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // Memulai LCD dengan ukuran 16x2
lcd.backlight(); // Menyalakan lampu latar LCD
pinMode(TEMP_SENSOR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("Temp Limit:");
lcd.setCursor(0, 1);
lcd.print("Cur Temp: ");
}
void loop() {
currentTemp = analogRead(TEMP_SENSOR_PIN) * (5.0 / 1023.0) * 100; // Konversi ke Celsius
char key = keypad.getKey();
// Mengambil input dari keypad untuk threshold suhu
if (key) {
if (key >= '0' && key <= '9') {
tempInput = (tempInput * 10) + (key - '0'); // Menyimpan input sementara
}
if (key == '#') { // Reset threshold ke nilai default
tempThreshold = 30; // Mengatur ulang batas suhu
tempInput = 0; // Reset input
}
if (key == '*') { // Konfirmasi input sebagai batas suhu
tempThreshold = tempInput;
tempInput = 0; // Reset tempInput setelah disimpan
}
}
// Menampilkan threshold dan suhu saat ini di LCD
lcd.setCursor(11, 0);
lcd.print(" "); // Hapus nilai sebelumnya
lcd.setCursor(11, 0);
lcd.print(tempThreshold); // Tampilkan batas suhu
lcd.setCursor(11, 1);
lcd.print(" "); // Hapus nilai sebelumnya
lcd.setCursor(11, 1);
lcd.print(currentTemp); // Tampilkan suhu saat ini
// Logika untuk alarm suhu tinggi
if (currentTemp >= tempThreshold) {
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, HIGH);
tone(BUZZER_PIN, 1000); // Buzzer ON
lcd.setCursor(0, 1);
lcd.print("ALERT! HIGH TEMP");
} else {
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_RED, LOW);
noTone(BUZZER_PIN); // Buzzer OFF
lcd.setCursor(0, 1);
lcd.print("Cur Temp: ");
}
delay(500); // Membaca setiap 0,5 detik
}