'''
Hi all.
In This code we want servo motor to rotate 360° when temperature(temValue) >30 then stop once it completed 360°and then when temperature becomes less than 30 it will rotate 0° means that it will return to its original position.
Now the probelm is when temperature becomes more than 30 degree the servo motor rotates 360° but continues rotatating until the conditions of temperature less than 30 degree while it supposed to rotate only for 360° then stop
Same goes when temperature is less than 30 degree
Here is the code #include <Servo.h> #include <DHT.h> #define DHTPIN D4
DHT dht(DHTPIN, DHT11);// Including library for dht
int temValue=0; // varible to get the data of temperature only
Servo S1;
void setup() {
S1.attach(0); // put your setup code here, to run once:
S1.write(0);
delay(2000);
}
void loop() {
float h = dht.readHumidity(); // DHT will read the data of humidity
float t = dht.readTemperature();// DHT will read the data of temperature
temValue=dht.readTemperature();
if (temValue >30)
{
S1.write(360);
Serial.println("Temperature High");
delay(1000);
}
else if (temValue <30)
{
S1.write(0);
Serial.println("Temperature is Cool");
delay(1000);
Where did you get a servo that will turn 360 degrees? Usually a 360 degree servo is a continuous servo or a servo that has had its feedback potentiometer disconnected. Such a servo is no longer a servo, but a gear motor whose speed and direction are controlled by a servo signal. You can no longer position such a servo.
The command servo.write(360); will not evaluate to turn 360 degrees. It will be the same as servo.writeMicroseconds(360);
See the Servo library reference.
void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds
I tried to write "writeMicroseconds(1000000)
So it is supposed to stop after 1 seconds right? But it doesn't stop.
From my code it seems that whenever the tempValue equal or greater than 30, the motor will rotate. As the code is in the void loop, the system would continously read the input from the sensor..
lets say for example, during the 1st iteration of the loop, the temp is equal to 30, so the motor will rotate. Then during the 2nd iteration of the loop, the temperature is still 30, so the motor will rotate again. Thats probably why I see my motor doesnt stop rotating. So it needs to add another condition for the motor position. keep track of what the last setting of the motor was,make a condition if the tempValue is still 30, but the motor is already at the desired position,dont make it rotate again.
When I changed to myservo.attach(0) and connected to pin 3 I can see that motor rotates around three complete cycles then it changes its direction and rotate with no stopping in that direction.