/*
the red led is the heating element
The wite LED can be any diode
update the DHTTYPE to match your sensor
*/
#include <LiquidCrystal.h>
#include "DHT.h"
const int button1Pin = 9; // Button 1 pin
const int button2Pin = 10; // Button 2 pin
int lastButton1State = HIGH; // Previous state of button 1
int lastButton2State = HIGH; // Previous state of button 2
volatile int temp = 40; // Default temperature
int tempChangeAmount = 5; // Amount by which temperature changes per button press
// Set the DHT Pin
#define DHTPIN 8
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define DHTTYPE DHT22 // Change this to match your sensor, DHT11 in my case
DHT dht(DHTPIN, DHTTYPE);
const int heater = 6;
void setup() {
pinMode(button1Pin, INPUT); // Button 1 with internal pull-up resistor
pinMode(button2Pin, INPUT); // Button 2 with internal pull-up resistor
// Set up the LCD's number of columns and rows:
lcd.begin(16, 2);
dht.begin();
// Print a message to the LCD
lcd.print("Temp: Humi: Tar:");
pinMode(heater, OUTPUT);
digitalWrite(heater, HIGH);
// Initialize Serial communication for debugging
Serial.begin(9600);
Serial.println("Setup complete.");
}
void loop() {
unsigned long currentMillis = millis();
// Check if it's time to update the LCD
static unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 500; // Update interval in milliseconds
if (currentMillis - lastUpdateTime >= updateInterval) {
lastUpdateTime = currentMillis;
// Read temperature in Fahrenheit
float t = dht.readTemperature();
float h = dht.readHumidity();
// Set the cursor to column 0, line 1
lcd.setCursor(0, 1);
if (isnan(t)) {
lcd.print("ERROR");
} else {
lcd.print(t);
lcd.setCursor(6, 1);
lcd.print(h);
//lcd.print("F"); // Clear any previous text after the temperature
lcd.print(" "); // Clear any previous text after the temperature to ensure no remnants
}
// Control the heater based on temperature
if (t < temp) {
digitalWrite(heater, LOW);
} else {
digitalWrite(heater, HIGH);
}
// Print current temperature set point
lcd.setCursor(13, 1);
if (temp < 100) {
lcd.print(" "); // Clear any potential leftover character
}
lcd.setCursor(13, 1);
lcd.print(temp);
}
// Button 1 state change detection
int reading1 = digitalRead(button1Pin);
if (reading1 != lastButton1State) {
if (reading1 == LOW) {
temp += tempChangeAmount;
if (temp > 99) {
temp = 99; // Limit temp to 99 degrees Fahrenheit
}
Serial.print("Button 1 pressed. Temp: ");
Serial.println(temp);
// Update LCD immediately after button press (optional)
lcd.setCursor(13, 1);
if (temp < 100) {
lcd.print(" "); // Clear any potential leftover character
}
lcd.setCursor(13, 1);
lcd.print(temp);
}
lastButton1State = reading1;
}
// Button 2 state change detection
int reading2 = digitalRead(button2Pin);
if (reading2 != lastButton2State) {
if (reading2 == LOW) {
temp -= tempChangeAmount;
if (temp < 0) {
temp = 0; // Limit temp to 0 degrees Fahrenheit
}
Serial.print("Button 2 pressed. Temp: ");
Serial.println(temp);
// Update LCD immediately after button press (optional)
lcd.setCursor(13, 1);
if (temp < 100) {
lcd.print(" "); // Clear any potential leftover character
}
lcd.setCursor(13, 1);
lcd.print(temp);
}
lastButton2State = reading2;
}
// Delay to debounce buttons (optional)
delay(50);
}