The essence of the program is to measure the temperature, and if the temperature is higher than 26 degrees, the servo rotates by 45 degrees, and if it's lower, it rotates by 179 degrees. However, a short circuit is occurring. At the moment of the first servo rotation, the temperature jumps to 40 degrees (the temperature is calculated from voltage), and after that, the servo continuously rotates back and forth because the temperature is fluctuating between 40 and 20 degrees. What could be the cause of this issue?
My code:
#include <Servo.h>
Servo myServo;
const int sensorPin = A0;
const float baselineTemp = 20.0;
int angle;
void setup() {
myServo.attach(9);
Serial.begin(9600);
}
void loop() {
int sensorVal = analogRead(sensorPin);
Serial.println("Sensor Value: ");
Serial.print(sensorVal);
float voltage = (sensorVal/1024.0) * 5.0;
Serial.println("Volts:");
Serial.print(voltage);
Serial.println("Celcius:");
float temperature = (voltage - .5) * 100;
Serial.print(temperature);
if (temperature < baselineTemp) {
myServo.write(179);
} else {
myServo.write(45);
}
delay(800);
}
Output in terminal:
sensorVal: 149
Volts:0.73
Celcius:22.75
sensorVal: 192
Volts:0.94
Celcius:43.75
sensorVal: 154
Volts:0.75
Celcius:25.20
sensorVal: 188
Volts:0.92
Celcius:41.80
sensorVal: 150
Volts:0.73
Celcius:23.24
sensorVal: 184
Volts:0.90
Celcius:39.84
sensorVal: 151
What could be the cause of this issue?
... the servo