/*include <Servo.h>
// Define the analog pin for the NTC thermistor
const int ntcPin = A0;
// Define the pin for the servo motor
const int servoPin = 9;
// Define the target temperature
const float targetTemperature = 25.0; // Set your target temperature here
Servo waterValveServo; // Create a servo object to simulate a valve
void setup() {
waterValveServo.attach(servoPin); // Attaches the servo on pin 9
Serial.begin(9600); // Start serial communication for monitoring
}
void loop() {
int ntcValue = analogRead(ntcPin); // Read the value from the thermistor
// Assuming a linear relationship just for simulation. Replace with a proper conversion.
float temperature = map(ntcValue, 0, 1023, 0, 100);
// Variable to hold the servo position.
int valvePosition;
// If the temperature is above the target, map the temperature above the target to the servo's angle range
if (temperature > targetTemperature) {
// Here we map each degree above the target temperature to 10 degrees of servo rotation.
valvePosition = map(temperature, targetTemperature, 100, 0, 180);
} else {
// If the temperature is below the target, the valve is closed (servo at 0 degrees).
valvePosition = 0;
}
// Constrain the position to make sure it's within the servo's limit
valvePosition = constrain(valvePosition, 0, 180);
// Move the servo to the position based on temperature
waterValveServo.write(valvePosition);
// Output the temperature and servo position to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C, Valve Position: ");
Serial.println(valvePosition);
delay(1000); // Wait for a second before reading again
}*/
#include <Servo.h>
// Define the analog pin for the NTC thermistor
const int ntcPin = A0;
// Define the pin for the servo motor
const int servoPin = 9;
// Define the target temperature and range
const float targetTemperature = 25.0; // Set your desired target temperature here
const float proportionalRange = 10.0; // Range for proportional control (±10°C around target)
Servo thermostatServo; // Create a servo object
void setup() {
thermostatServo.attach(servoPin); // Attach the servo to pin 9
thermostatServo.write(90); // Initialize to mid-position (neutral)
Serial.begin(9600); // Start serial communication for monitoring
}
void loop() {
int ntcValue = analogRead(ntcPin); // Read the value from the thermistor
// Assuming a linear relationship just for simulation. Replace with a proper conversion.
float temperature = map(ntcValue, 0, 1023, 0, 100);
// Calculate the difference from the target temperature
float temperatureDifference = temperature - targetTemperature;
// Map the temperature difference proportionally to the servo range (0 to 180 degrees)
// - If temperatureDifference is -proportionalRange, servo = 0
// - If temperatureDifference is +proportionalRange, servo = 180
int servoPosition = map(temperatureDifference, -proportionalRange, proportionalRange, 0, 180);
// Constrain the servo position to be within valid bounds (0 to 180 degrees)
servoPosition = constrain(servoPosition, 0, 180);
// Move the servo to the calculated position
thermostatServo.write(servoPosition);
// Output the temperature and servo position to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C, Servo Position: ");
Serial.println(servoPosition);
delay(1000); // Wait for a second before reading again
}