I suppose you are using a continuous servo otherwise you wouldn'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 range of acceptable values is the same as for a normal servo, i.e. [0; 180]
:
0
means max speed clockwise
90
means no motion
180
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 experiments 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).
90
degrees value, hence the servo would stay forever in that position.