Servo won't stop rotating
I suppose you are using a continuous servo otherwise you won't be able to make it turn 90° more than twice (a normal servo has a varying angle of only 180°).
Now if you are using a continuous servo, you have to be aware that the value you pass to myservo.write()
does not represent an angle anymore!
With a continuous servo, the value passed to myservo.write()
now means a speed of rotation where, but the rnage of acceptable values is the same as for a normal servo, i.e. [0; 180]
:
0
means max speed clockwise90
means no motion180
means max speed counter-clockwise
Now if you want to turn the servo exactly 90°, then you must know the max speed of the servo, and from that, compute the time during which you must let it turn:
// Start turning clockwise
myservo.write(0);
// Go on turning for the right duration
delay(TURN_TIME);
// Stop turning
myservo.write(90);
The problem here is to compute TURN_TIME
. For this, you must check the datasheet of your servo.
On my own servo, a Feetech Micro 1.3kg Continuous Rotation Servo FS90R, the max speed is:
- 0.12s/60° when powered with 4.8V
- 0.10s/60° when powered with 6V
However, with the Arduino UNO, the supplied voltage should be exactly 5V, neither 4.8V, nor 6V.
If we take a linear approximation, then we can apply the following formula to find out the speed T
(in s/60°):
T = (0.12 - 0.10) * (V - 4.8) / (4.8 - 6.0) + 0.12
Hence, for 5V, we can take it for granted that the max speed should be:
T = (0.12 - 0.10) * (5.0 - 4.8) / (4.8 - 6.0) + 0.12 = 0.116667
Since we need 90°, this means we must run the servo at its max speed during:
T' = T * 1.5 = 0.175s
Hence we now have the following program:
#include <Servo.h>
#define TURN_TIME 175
Servo myservo;
void setup {
myservo.attach(10);
// Initially the servo must be stopped
myservo.write(90);
}
void loop() {
// Start turning clockwise
myservo.write(0);
// Go on turning for the right duration
delay(TURN_TIME);
// Stop turning
myservo.write(90);
// Wait for 12h
delay(12 * 3600 * 1000);
}
Of course, you will need some eperiments to find the exact right values; you may also find out that the servo may be sensitive to noise and even when it should not move (value = 90
), it does move (not fast, but still it moves).
That was useful. I was getting the same rotation thing when I had servos at 90. What I did to stop that was use the detach command. Using the Particle Photon with a continuous servo attached.
bool state = LOW;
Servo servo1;
Servo servo2;// create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 90; // variable to store the servo position
void setup()
{
servo1.attach(D0);
servo2.attach(D1);
Spark.function("test", testFunction);
servo1.attach(10);
servo1.write(90);
servo2.attach(10);
servo2.write(90);
}
void loop()
{
servo2.detach(), servo1.detach();
delay (25);
servo2.attach(D0), servo1.attach(D1);
delay (25);
servo1.write(0), servo2.write(180);
delay (1000);
servo2.write(180), servo1.write(0);
delay (1000);
//I did this to make the servo not move when it is at 90.
servo2.detach(), servo1.detach();
delay (25);
servo1.write(90), servo2.write(90);
delay (1000);
//end
//You must attach the servos again to make them move again.
servo2.attach(D0), servo1.attach(D1);
delay (25);
//end
servo1.write(180), servo2.write(0);
delay (1000);
servo2.write(180), servo1.write(100);
delay (1000);
}
int testFunction(String args) {
return 200; // This is checked in the web app controller for validation
}