#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// LCD Setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Servo Setup
Servo servoMotor;
// DS18B20 Temperature Sensor Setup
#define ONE_WIRE_BUS A0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Switch Setup
#define START_SWITCH 6
#define RESET_SWITCH 7
// LED Pins
#define RED_LED_PIN 5
#define BLUE_LED_PIN 3
// Variable to store the initial state for LCD display
bool isFirstStep = true;
// Variable to store the last state of the reset button
int lastResetState = HIGH;
// Delay between updates in milliseconds
const int updateDelay = 1000; // Set your desired delay
void setup() {
Serial.begin(9600); // Initialize serial communication
// Initialize LCD
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
// Attach servo to pin 8
servoMotor.attach(8);
// Set up Switch pins with improved debouncing
pinMode(START_SWITCH, INPUT_PULLUP);
pinMode(RESET_SWITCH, INPUT_PULLUP);
// Set up LED pins
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
// Start with the LCD displaying the initial message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TempWind Tracker");
// Start the DallasTemperature library
sensors.begin();
// Wait for the start button press
waitForStartButton();
}
void loop() {
// Measure temperature
float temperature = getTemperature();
// Update the LCD and Serial Monitor
updateLCD(temperature);
Serial.print("Temperature: ");
Serial.println(temperature);
// Move servo to detect wind direction
moveServo();
// Check and display LED color based on temperature
displayLEDColor(temperature);
// Check for reset button press with improved debouncing
int resetState = digitalRead(RESET_SWITCH);
if (resetState == LOW && lastResetState == HIGH) {
// Button is pressed, wait for release
delay(50);
while (digitalRead(RESET_SWITCH) == LOW) {
// Do nothing
}
// Reset and wait for start button press
waitForStartButton();
}
lastResetState = resetState;
// Add a delay between updates
delay(updateDelay);
}
void waitForStartButton() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TempWind Tracker");
// Wait for the start button press
while (digitalRead(START_SWITCH) == HIGH) {
// Do nothing
}
// Clear the LCD for the main loop
lcd.clear();
}
float getTemperature() {
// Request temperature from DS18B20
sensors.requestTemperatures();
// Read temperature from DS18B20
float temperature = sensors.getTempCByIndex(0);
// Check if the reading is valid
if (temperature == DEVICE_DISCONNECTED_C) {
Serial.println("Error: DS18B20 disconnected");
// Handle the error as needed
}
return temperature;
}
void moveServo() {
servoMotor.write(270);
delay(15); // Add a small delay for smooth servo movement
}
void displayLEDColor(float temperature) {
// Turn off all LEDs initially
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
// Test each LED independently
if (temperature >= 27.0) {
digitalWrite(RED_LED_PIN, HIGH);
} else if (temperature <= 26.99) {
digitalWrite(BLUE_LED_PIN, HIGH);
}
}
void updateLCD(float temperature) {
// Clear the display only when the temperature changes
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
}