#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Encoder.h>
#include <DallasTemperature.h>
#include <OneWire.h>
int pinA = 3; // Connected to CLK on KY-040
int pinB = 4; // Connected to DT on KY-040
int relayPin = 5; // Relay trigger pin
int buzzerPin = 6; // Buzzer pin
int DS18B20pin = 8; // Temp read pin
int setTimer = 15; // Initial timer value in minutes
int setTemp = 50; // Initial temperature value
int curTemp = 50; // Current temperature reading
bool encoderSW = false; // Encoder mode switch
bool mainSW = false; // Main mode switch
unsigned long timerStartMillis = 0; // Store the start time of the timer
bool timerRunning = false; // Timer running flag
/* Functions */
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD initialization
Encoder encoder(pinA, pinB); // Encoder initialization
OneWire oneWire(DS18B20pin);
DallasTemperature sensors(&oneWire); // DS18B20 setup
// Read encoder value and handle limits
int readEncoder(int value, int maxVal, int minVal);
// Update the LCD display
void updateDisp();
// Start and update the countdown timer
void countdownTimer(unsigned long timerDuration);
void regulateTemperature(int setTemp);
float getTemperatureF();
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
pinMode(7, INPUT_PULLUP); // encoderSw
pinMode(8, INPUT_PULLUP); // mainSW
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
sensors.begin(); // Initialize the DS18B20 sensor
}
void loop() {
updateDisp();
// Check if the encoder mode switch button is pressed
if (digitalRead(7) == LOW) {
encoderSW = !encoderSW;
delay(50);
}
// Check if the main mode switch button is pressed
if (digitalRead(9) == LOW) {
mainSW = !mainSW;
delay(50);
}
// Handle encoder input based on mode
if (encoderSW) {
if (!mainSW) {
setTimer = readEncoder(setTimer, 60, 0);
}
} else {
setTemp = readEncoder(setTemp, 75, 38);
}
curTemp = getTemperatureF();
// Handle timer start and stop based on main mode switch
if (mainSW) {
regulateTemperature(setTemp, curTemp);
setTemp = readEncoder(setTemp, 75, 38);
if (!timerRunning) {
timerStartMillis = millis() / 1000; // Store the start time in seconds
timerRunning = true;
}
} else {
timerRunning = false;
}
// Update the LCD display during timer operation
if (timerRunning) {
countdownTimer(setTimer * 60); // Convert setTimer to seconds
}
delay(100);
}
// Read encoder value and handle limits
int readEncoder(int value, int maxVal, int minVal) {
int encoderValue = encoder.read();
if (encoderValue > 0) {
value++;
if (value > maxVal)
value = maxVal;
} else if (encoderValue < 0) {
value--;
if (value < minVal)
value = minVal;
}
encoder.write(0);
return value;
}
// Update the LCD display
void updateDisp() {
lcd.setCursor(0, 0);
lcd.print("Set:");
lcd.print(setTemp);
lcd.print("F ");
lcd.print("Temp:");
lcd.print(curTemp);
lcd.print("F ");
lcd.setCursor(0, 1);
if (!mainSW) {
lcd.print("Time: ");
lcd.setCursor(6, 1);
lcd.print(setTimer);
lcd.print(":00 ");
}
}
// Start and update the countdown timer
void countdownTimer(unsigned long timerDuration) {
unsigned long currentSeconds = millis() / 1000; // Current time in seconds
if (currentSeconds - timerStartMillis >= timerDuration) {
timerRunning = false;
mainSW = false; // Set mainSW to false when the timer reaches 0:00
tone(buzzerPin, 262, 3000); //Buzzer for 3 seconds (262 Hz)
for(int i = 0; i < 15; i++){
//buzzer command
lcd.setCursor(6, 1);
lcd.print(" ");
delay(100);
lcd.setCursor(6, 1);
lcd.print("0:00");
delay(100);
}
} else {
unsigned long remainingSeconds = timerDuration - (currentSeconds - timerStartMillis);
unsigned long minutes = remainingSeconds / 60;
unsigned long seconds = remainingSeconds % 60;
lcd.setCursor(6, 1);
if (seconds < 10) {
lcd.print(String(minutes, DEC) + ":0" + String(seconds, DEC));
} else {
lcd.print(String(minutes, DEC) + ":" + String(seconds, DEC));
}
}
}
void regulateTemperature(int setTemp, float curTemp)
{
if(curTemp > setTemp)
{
digitalWrite(relayPin, HIGH);
}
else
{
digitalWrite(relayPin, LOW);
}
}
float getTemperatureF() {
// Function to read the DS18B20 temperature in Fahrenheit
sensors.requestTemperatures(); // Request temperature readings
float tempC = sensors.getTempCByIndex(0); // Get the temperature in Celsius
float tempF = (tempC * 9.0 / 5.0) + 32; // Convert to Fahrenheit
return tempF;
}
Loading
ds18b20
ds18b20