#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>
#define ONE_WIRE_BUS 5 // DS18B20 data pin connected to digital pin 5
#define LED_PIN 13 // LED connected to digital pin 13
#define SERVO_PIN 9 // Servo motor control pin connected to digital pin 9
#define TEMPERATURE_THRESHOLD 30 // Temperature threshold in Celsius
OneWire oneWire(ONE_WIRE_BUS); // for communicating with the DS18B20 sensor
DallasTemperature sensors(&oneWire);// for handling temperature readings
Servo fanServo; // Create a Servo object to control the servo motor
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
pinMode(SERVO_PIN, OUTPUT);
sensors.begin();
fanServo.attach(SERVO_PIN);
}
void loop() {
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
if (temperature != DEVICE_DISCONNECTED_C) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
if (temperature >= TEMPERATURE_THRESHOLD) {
digitalWrite(LED_PIN, HIGH); // Turn on the LED
fanServo.write(90); // Rotate the servo to 90 degrees (representing the fan on position)
Serial.println("countdown");
for (int countdownValue = 59; countdownValue >= 0;)
{
Serial.println(countdownValue);
countdownValue--;
delay(1000);
}
}
else {
digitalWrite(LED_PIN, LOW); // Turn off the LED
fanServo.write(0); // Rotate the servo to 0 degrees
}
}
delay(2000); // Delay between readings
}