#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C dan ukuran LCD (16 kolom, 2 baris)
const int sensorPin1 = A0; // Pin untuk sensor suhu 1
const int sensorPin2 = A1; // Pin untuk sensor suhu 2
const int potPin1 = A2; // Pin untuk potensiometer 1
const int potPin2 = A3; // Pin untuk potensiometer 2
const int buttonPin = 7; // Pin untuk tombol
float temperature1;
float temperature2;
int potValue1;
int potValue2;
bool showTemperature = true; // Variabel untuk menentukan mode tampilan (true: suhu, false: potensiometer)
bool buttonState;
bool lastButtonState = HIGH; // Simpan status tombol sebelumnya
void setup() {
Serial.begin(9600);
lcd.init(); // Inisialisasi LCD
lcd.backlight(); // Nyalakan backlight LCD
lcd.setCursor(0, 0);
lcd.print("-RFHProduction");
delay(100);
lcd.setCursor(0, 1);
lcd.print("----Bismillah----");
delay(6000); // Tahan pesan selama 2 detik
// Hapus layar sebelum mulai pembacaan suhu
lcd.clear(); // Bersihkan layar LCD
pinMode(buttonPin, INPUT_PULLUP); // Set pin tombol sebagai input dengan pull-up resistor internal
}
void loop() {
int sensorValue1 = analogRead(sensorPin1);
temperature1 = readTemperature(sensorPin1); // Baca suhu dari sensor 1
int sensorValue2 = analogRead(sensorPin2);
temperature2 = readTemperature(sensorPin2); // Baca suhu dari sensor 2
// Baca nilai dari dua potensiometer jika mode tampilan adalah potensiometer
if (!showTemperature) {
potValue1 = map(analogRead(potPin1), 0, 1023, -72, +16); // Pemetaan nilai potensiometer 1 ke rentang -30 hingga 30
potValue2 = map(analogRead(potPin2), 0, 1023, -72, +16); // Pemetaan nilai potensiometer 2 ke rentang -30 hingga 30
}
// Baca status tombol
buttonState = digitalRead(buttonPin);
// Jika tombol ditekan dan status tombol sebelumnya adalah tidak ditekan, toggle mode tampilan
if (buttonState == LOW && lastButtonState == HIGH) {
showTemperature = !showTemperature;
delay(10); // Tunda untuk menghindari bouncing pada tombol
}
// Simpan status tombol saat ini sebagai status sebelumnya untuk iterasi berikutnya
lastButtonState = buttonState;
if (showTemperature) {
// Tampilkan suhu pada LCD
lcd.setCursor(0, 0);
lcd.print("CHL 1: ");
lcd.print(temperature1);
lcd.print(" C ");
lcd.setCursor(0, 1);
lcd.print("CHL 2: ");
lcd.print(temperature2);
lcd.print(" C ");
} else {
// Tampilkan nilai potensiometer pada LCD
lcd.setCursor(1, 0);
lcd.print("H 1: ");
lcd.print(potValue1);
lcd.print(" dB ");
lcd.setCursor(1, 1);
lcd.print("H 2: ");
lcd.print(potValue2);
lcd.print(" dB ");
}
delay(100); // Tunda 100ms untuk debounce tombol
}
float readTemperature(int pin) {
int sensorValue = analogRead(pin); // Baca nilai analog dari sensor suhu
float voltage = sensorValue * (5.0 / 1023.0); // Ubah nilai analog menjadi tegangan (5V / 1023 langkah)
float temperature = voltage * 100.0; // Konversi tegangan menjadi suhu Celsius
return temperature;
}