#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>
#include <LiquidCrystal.h>
// Pin Definitions
#define ONE_WIRE_BUS 2 // DS18B20 on Arduino pin 2
#define SERVO_PIN 9 // Servo on Arduino pin 9
#define LED_RED 5 // Red LED on Arduino pin 5
#define LED_GREEN 4 // Green LED on Arduino pin 4
#define BUTTON_PIN 3 // Pushbutton on Arduino pin 3
#define POT_PIN A0 // Potentiometer on analog pin A0
// LCD Pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
Servo myservo;
bool systemActive = false;
int potValue = 0;
int tempThreshold = 0;
void setup() {
// Start the DS18B20 sensor
sensors.begin();
// Set up the LCD's number of columns and rows
lcd.begin(16, 2);
// Initialize static text on the LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.setCursor(0, 1);
lcd.print("Threshold: ");
// Initialize the servo
myservo.attach(SERVO_PIN);
// Initialize the LED pins as outputs
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
// Initialize the pushbutton pin as an input
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// Check if the button is pressed
if (digitalRead(BUTTON_PIN) == LOW) {
delay(500); // Debouncing delay
systemActive = !systemActive;
}
if (systemActive) {
// Read temperature
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
// Read potentiometer
potValue = analogRead(POT_PIN);
tempThreshold = map(potValue, 0, 1023, 0, 100); // Map potentiometer to temperature range
// Adjust servo based on temperature
if (temperatureC > tempThreshold) {
myservo.write(180);
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
} else {
myservo.write(90);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
}
// Update temperature value on LCD
lcd.setCursor(6, 0); // Set cursor to where the temperature value should start
lcd.print(temperatureC);
lcd.print(" C "); // Extra spaces to clear previous longer values
// Update threshold value on LCD
lcd.setCursor(12, 1); // Set cursor to where the threshold value should start
lcd.print(tempThreshold);
lcd.print(" C "); // Extra spaces to clear previous longer values
} else {
// Turn off LEDs and reset servo when system is inactive
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, LOW);
myservo.write(90); // Reset position
lcd.clear();
// Reinitialize static text on the LCD after clearing
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.setCursor(0, 1);
lcd.print("Threshold: ");
}
}