I have an Arduino Uno R3 board and after some time not in use it seems to have corrupted. My code is below, basically I want to rotate a servo 90 degrees every 12 hours but as soon as I plug it in the servo starts rotating non-stop. Have tried multiple delay steps and shortening and lengthening the write but the same effect happens.

#include <Servo.h>

Servo myservo;

void setup {
  myservo.attach(10);
}

void loop() {
  myservo.write(90); 
  delay(3600000);
}
  • 4
    I guess that is a continuous servo. In this situation, with your code what you observe is expected: the servo runs forever at constant speed. If it was a normal servo your code would not work either, as you never change the 90 degrees value, hence the servo would stay forever in that position. – jfpoilpret Apr 28 '14 at 5:42
  • Note that your Arduino UNO is probably in perfect shape, anyway. – jfpoilpret Apr 28 '14 at 5:44
  • Not related to the servo issue, but I think your delay is 10hrs. – JRobert Apr 28 '14 at 17:39
up vote 5 down vote accepted

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 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 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).

  • Also note: The arduino crystal is not a reliable time source. It can be expected to lose or gain up to a minute per day, not including the offset of the actual time taken to execute the program. If you want accurate time, buy a real Time Clock (RTC) module, and set an alarm on it, which can then trigger a function on the Arduino every 12 hours. – cortices Nov 21 '15 at 1:53

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.