
final code
Matthew Oh and 2 OthersEscher BriereMason Clish
#include <OneWire.h>#include <DallasTemperature.h>#include <Servo.h>
// Pin configurationsconst int oneWireBus = A0; // Pin where DS18B20 is connectedconst int servoPin = 12; // Pin for the servo motor
// Create a OneWire instance to communicate with the DS18B20 sensorOneWire oneWire(oneWireBus);
// Pass our OneWire reference to DallasTemperatureDallasTemperature sensors(&oneWire);
// Create servo objectServo myServo;
// Temperature threshold (in Celsius)const float tempThresholdCelsius = 30.0; // Temperature in Celsius (corresponds to ~86°F)
float currentTemp = 0.0; // To keep track of the current temperature
void setup() { // Start serial communication for debugging Serial.begin(9600);
// Start the Dallas Temperature library sensors.begin();
// Attach the servo to the pin myServo.attach(servoPin);
// Initialize the servo position to 0 degrees myServo.write(0);}
void loop() { // Request temperature readings from the DS18B20 sensors.requestTemperatures();
// Get temperature in Celsius from the first sensor (assuming only one sensor is connected) float tempC = sensors.getTempCByIndex(0);
// Check if the temperature is valid if (tempC == DEVICE_DISCONNECTED_C) { Serial.println("Error: Could not read temperature data."); return; }
// Print temperature to Serial Monitor Serial.print("Temperature: "); Serial.print(tempC); Serial.println(" °C");
// Check if the temperature exceeds the threshold (30°C) if (tempC > tempThresholdCelsius) { // If the temperature is above 30°C, move the servo to 90 degrees (stay there) if (currentTemp <= tempThresholdCelsius) { myServo.write(0); // Move servo to 90 degrees Serial.println("Servo moved to 90 degrees."); } } else { // If the temperature is below 30°C, move the servo to 0 degrees (stay there) if (currentTemp > tempThresholdCelsius) { myServo.write(90); // Move servo to 0 degrees Serial.println("Servo moved to 0 degrees."); } }
// Update the current temperature for the next loop iteration currentTemp = tempC;
// Wait 1 second before reading the temperature again delay(1000);}
// Pin configurationsconst int oneWireBus = A0; // Pin where DS18B20 is connectedconst int servoPin = 12; // Pin for the servo motor
// Create a OneWire instance to communicate with the DS18B20 sensorOneWire oneWire(oneWireBus);
// Pass our OneWire reference to DallasTemperatureDallasTemperature sensors(&oneWire);
// Create servo objectServo myServo;
// Temperature threshold (in Celsius)const float tempThresholdCelsius = 30.0; // Temperature in Celsius (corresponds to ~86°F)
float currentTemp = 0.0; // To keep track of the current temperature
void setup() { // Start serial communication for debugging Serial.begin(9600);
// Start the Dallas Temperature library sensors.begin();
// Attach the servo to the pin myServo.attach(servoPin);
// Initialize the servo position to 0 degrees myServo.write(0);}
void loop() { // Request temperature readings from the DS18B20 sensors.requestTemperatures();
// Get temperature in Celsius from the first sensor (assuming only one sensor is connected) float tempC = sensors.getTempCByIndex(0);
// Check if the temperature is valid if (tempC == DEVICE_DISCONNECTED_C) { Serial.println("Error: Could not read temperature data."); return; }
// Print temperature to Serial Monitor Serial.print("Temperature: "); Serial.print(tempC); Serial.println(" °C");
// Check if the temperature exceeds the threshold (30°C) if (tempC > tempThresholdCelsius) { // If the temperature is above 30°C, move the servo to 90 degrees (stay there) if (currentTemp <= tempThresholdCelsius) { myServo.write(0); // Move servo to 90 degrees Serial.println("Servo moved to 90 degrees."); } } else { // If the temperature is below 30°C, move the servo to 0 degrees (stay there) if (currentTemp > tempThresholdCelsius) { myServo.write(90); // Move servo to 0 degrees Serial.println("Servo moved to 0 degrees."); } }
// Update the current temperature for the next loop iteration currentTemp = tempC;
// Wait 1 second before reading the temperature again delay(1000);}