#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
OneWire oneWire(7); // Pin yang terhubung ke data pin sensor DS18B20
DallasTemperature sensors(&oneWire);
int setPoint = 5;
const int relayPin = 6; // Pin untuk relay
const int buttonIncreasePin = 2;
const int buttonDecreasePin = 8;
const int buttonDisplayPin = 4;
unsigned long lastDebounceTimeIncrease = 0;
unsigned long lastDebounceTimeDecrease = 0;
unsigned long lastDebounceTimeDisplay = 0;
unsigned long debounceDelay = 400;
bool displayMode = false;
bool needUpdate = true;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("WELCOME...");
delay(2000);
lcd.clear();
pinMode(buttonDisplayPin, INPUT_PULLUP);
pinMode(buttonIncreasePin, INPUT_PULLUP);
pinMode(buttonDecreasePin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
sensors.begin();
}
void loop() {
if (digitalRead(buttonDecreasePin) == LOW) {
if ((millis() - lastDebounceTimeDecrease) > debounceDelay) {
if (setPoint > -30) {
setPoint--;
displayMode = false;
needUpdate = true;
}
lastDebounceTimeDecrease = millis();
}
}
if (digitalRead(buttonIncreasePin) == LOW) {
if ((millis() - lastDebounceTimeIncrease) > debounceDelay) {
if (setPoint < 10) {
setPoint++;
displayMode = false;
needUpdate = true;
}
lastDebounceTimeIncrease = millis();
}
}
if (digitalRead(buttonDisplayPin) == LOW) {
if ((millis() - lastDebounceTimeDisplay) > debounceDelay) {
displayMode = true;
needUpdate = true;
lastDebounceTimeDisplay = millis();
}
}
if (needUpdate || (millis() - lastDebounceTimeDisplay) > 2000) {
lastDebounceTimeDisplay = millis();
lcd.clear();
sensors.requestTemperatures();
float t = sensors.getTempCByIndex(0); // Membaca suhu dalam Celsius
Serial.print("Reading Temperature: ");
Serial.println(t);
if (isnan(t)) {
lcd.setCursor(2, 1);
lcd.print("Error");
} else {
// Logic to control the relay
if (t > setPoint + 2) {
digitalWrite(relayPin, LOW); // Turn off the relay
} else if (t < setPoint - 2) {
digitalWrite(relayPin, HIGH); // Turn on the relay
}
if (displayMode) {
lcd.setCursor(2, 0);
lcd.print("Temp : ");
lcd.setCursor(2, 1);
lcd.print(t);
lcd.print((char)223);
lcd.print("C");
} else {
lcd.setCursor(4,0);
lcd.print("Set Suhu");
lcd.setCursor(6,1);
lcd.print(setPoint);
}
}
needUpdate = false;
}
}