1

I would like to move my servo motor (SM-S2309S) between random angles. In order to do so, I used this simple code:

#include <Servo.h>
Servo servoMain; // Define our Servo

// SM-S2309S angular speed
float speed = 0.12/60;

void move(int from, int to)
{
  // initiate the move to the new angle
  servoMain.write(to);
  // wait for the movement to actually happen over the actual angle to move through
  delay(1000 * speed * int(from-to));
}

void setup()
{
   servoMain.attach(10); // servo on digital pin 10
   Serial.begin(115200);
   // move to center as a start
   servoMain.write(90);
   delay(1000);
}

void loop()
{
  // the previous angle, initially set as center (per the setup() call)
  static int oldangle = 90;
  // apparently 15 to 165 deg is a safe range
  int newangle = random(15,165);
  Serial.println(newangle);
  move(oldangle,newangle);
  oldangle = newangle;
}

It starts by placing the arm in the center (in the setup() function) and then should generate a random number (the new angle to go to), in a loop.

What I get is the initial position, then two moves (counterclockwise, then clockwise). The output on the serial console shows 22 and 64, which is consistent with the two moves.

Then everything stops. There is no error message on the console (I do not expect one to be, but I am not sure if there could be any feedback from the Arduino, being a complete beginner). What can be the reason for this stop, after two successful loops?

4
  • Does it STOP or it RUNS but nothing happens?
    – Dat Ha
    Dec 29, 2016 at 15:51
  • @DatHa: sorry, I do not really understand your question - should there be a feedback informing me whether the program still runs or has stopped/crashed? I saw the two random values for the angle output on the console (associated with the two movements of the servo motor) and then nothing else. How can i check if the program is still running or whether it has crashed (it should at least output the new random angles if it was still running, I believe).
    – WoJ
    Dec 29, 2016 at 16:03
  • 1
    flash a led in the background
    – Dat Ha
    Dec 29, 2016 at 16:04
  • @DatHa: ok, thank you very much for the hint. I will look at how to have the LED blink independently of the main loop() (I guess that this is what you meant, otherwise the output of the new angles on the console would have been enough, no matter of the servo moves or not)
    – WoJ
    Dec 29, 2016 at 16:31

1 Answer 1

5

Just a small mistake! Not much big of a deal :D

In the move function

void move(int from, int to) { // initiate the move to the new angle servoMain.write(to); // wait for the movement to actually happen over the actual angle to move through delay(1000 * speed * int(from-to)); }

suppose the values passed are 20, 35 then the last line would be 1000*speed*(-15) Which is negative therefore the arduino delay function converts the - signed bit to a very large unsigned number.

All you have to do is to take the magnitude of from - to ie |from - to | by calling the abs() function.

your move function would look like this:

void move(int from, int to) { // initiate the move to the new angle servoMain.write(to); // wait for the movement to actually happen over the actual angle to move through delay(1000 * speed * abs(int(from-to))); }

Just alter your move function or copy mine and you'll have no problem at all!

3
  • Ah! Thank you! I used int() when I actually meant abs() to exactly handle the case you mention.
    – WoJ
    Dec 29, 2016 at 17:16
  • 1
    Hahaha Pleasure is mine!
    – Manav
    Dec 29, 2016 at 17:27
  • 1
    “The arduino delay function hangs as it does not allow negative parameter” is absurd nonsense. That is, it is wrong, wrong, wrong. The parameter to delay() is an unsigned long, which is never negative. When (as in your example) from-to is -15, the delay called for is about 2147483618 ms, or about 3.5 weeks. So delay() isn't hung, it's just doing what it was told to do. Dec 29, 2016 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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