4

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
6

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
  • don't know why, but mine FS90R continuous servo is stopped with servo.write(95); – invis Oct 31 at 20:13
2

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
}

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