// Include LiquidCrystal_I2C library to control an LCD screen via I2C
#include <LiquidCrystal_I2C.h>
// Define the pins for the LEDs
const int blueLedPin = 13;
const int greenLedPin = 10;
const int redLedPin = 7;
// Define the analog pin for the temperature sensor
const int tempSensor = A0;
// Define the constant for the BETA coefficient
const int BETA = 3950;
// Create the LCD object with I2C library address 0x27, 16 columns, and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
// Set the LED pins as outputs
pinMode(blueLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
// Initialize the LCD
lcd.init();
// Turn on the LCD backlight
lcd.backlight();
}
void loop()
{
// Read the analog value from the temperature sensor
int val = analogRead(tempSensor);
// Convert the analog value to temperature in Celsius using the BETA coefficient
float temp = 1 / (log(1 / (1023.0 / val - 1)) / BETA + 1.0 / 298.15) - 273.15;
// Set the cursor to the first row and first column
lcd.setCursor(0, 0);
// Print "Condition" on the LCD
lcd.print("Condition:");
// Check if the temperature is above 25°C
if (temp > 25)
{
lcd.print(" High!");
digitalWrite(redLedPin, HIGH);
digitalWrite(blueLedPin, LOW);
digitalWrite(greenLedPin, LOW);
}
// Check if the temperature is 18°C or below
else if (temp <= 18)
{
lcd.print(" Low!");
digitalWrite(redLedPin, LOW);
digitalWrite(blueLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
}
// Otherwise print "Normal"
else
{
lcd.print("Normal");
digitalWrite(redLedPin, LOW);
digitalWrite(blueLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
}
// Set the cursor to the second row and first column
lcd.setCursor(0, 1);
// Print "Room Temp: " on the LCD
lcd.print("Room Temp: ");
// Print the temperature value on the LCD
lcd.print(temp, 1); // Display temperature with 1 decimal point
// Wait for 1 second
delay(1000);
}