Servo Program Help

Hello,

I'm trying to make a very simple program to move my hs-422 servo, but my code is not working. It will only move the servo one time. It gets stuck on "delay" and will not move past that point. I know I'm making a noob mistake and any help would be greatly appreciated….

Thanks,
Drew Davis

#include <Servo.h>

Servo myservo;

void setup()
{
myservo.attach(9);
}

void loop()
{

myservo.write(10);

delay(100);

myservo.write(180); //* was previously set to 250 by mistake.

}

Is this a continuous servo?

If not, servo.write() is limited from 0 to 180 degrees. Also, it sets a servo position, so if you send two commands with the same value you have no way of knowing if the second command is run.

Try this:

myservo.write(45);
delay(100);
myservo.write(135);

It does not get stuck on delay(). It goes to 250 and stays there, because you never tell it to go to a different position.

Can your servo actually get to 250?

First off I did not mean to type 250 both times, and when I tested it it both were not at 250. Sorry for the confusion. Also, I was under the impression that I could use any numbers between 180 and 1023.

Kenth, I tried your code and it got stuck on the delay again.

Any more suggestions?

Thanks,
Drew Davis

Maybe you should read the docs before you use a command:

The argument should be the angle in degrees.

The data sheet for that servo talks about needing a "servo stretcher".
http://www.servocity.com/html/hs-422_super_sport_.html

you may need to dig into the servo command to see if it can drive it with the Arduino's standard pulse width.

In any case for testing, try 0 and 45.

Thanks for the link! I changed the program so that the value is not more than 180, but it still gets stuck on the delay.

Got any ideas?

Thanks,
Drew Davis

Got any ideas?

Two. actually. One is for you to go look at the link that was posted. It shows that the servo should be positioned using writeMicroseconds(), rather than write().

The other is for you to add Serial.begin() and Serial.print() statements to your code to see what it is doing. It is NOT, unless you have royally hosed stuff up, getting stuck in delay().

How are you powering the servo? Not from the Arduino, I hope.

Just got your third post. I don't know what a stretcher is. Also how would I control it with PWM? This is one of my first sketches.

While writing this the servo just started flinching then went back to being locked . I don't know why.

Thanks,
Drew Davis

Read the datasheet for the servo, and then use writeMicroseconds() as Paul suggested to set two values. I am guessing this is a non-standard servo and the correspondence from degrees to microseconds is not working right when you use the servo.write() command.

  1. I am powering it off the Arduino. Is there a better way? The book i'm reading told me to run it right off the arduino.

  2. Where would I look at what ever it prints, and what would I put in the brackets after the serial.begin?

  3. I did not see the writeMicroseconds(). Would I still use a value between o and 180?

Thanks,
Drew Davis

  1. I am powering it off the Arduino. Is there a better way? The book i'm reading told me to run it right off the arduino.

Look at the link that was posted.

Current Drain (4.8V): 8mA/idle and 150mA no load operating
Current Drain (6.0V): 8.8mA/idle and 180mA no load operating

The Arduino digital pins are capable of 40mA MAX. The total output that the Arduino can manage is 200mA. Your servo is, with no load, taxing the Arduino.

  1. Where would I look at what ever it prints, and what would I put in the brackets after the serial.begin?

In the Serial Monitor. Any value that the Arduino supports, between 9600 and 115200. Faster is better.

  1. I did not see the writeMicroseconds(). Would I still use a value between o and 180?

No. Look at the link. There is a pretty picture showing the range of values that the servo expects. These values are not angles, so they are appropriate only for the writeMicroseconds() method.

No, because it is in microseconds, not degrees.

The servo you chose does not appear to be standard. It requires 600 us for -90 and 2400 us for +90. A standard servo only runs from 1000 - 2000.

You should always power the servo separately, they can draw a lot of current and put noise on the power supply.

Hello,

I changed "myservo.write();" to "myservo.writeMicroseconds()" but it still did not precede past the delay. Also, I put " Serial.begin(9600);
Serial.print(9600);" in my void loop but this is what I got…

9600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096….

Also,

  1. What is "noise"?

  2. I did not really chose this servo. I was on a FTC robotics team and that is what came with all the robots. I just pulled it off and plugged it into the Arduino. I don't have a extra power supply but I will get one when I can.

Thanks for helping me,
Drew Davis

drewdavis:
I changed "myservo.write();" to "myservo.writeMicroseconds()" but it still did not precede past the delay. Also, I put " Serial.begin(9600);
Serial.print(9600);" in my void loop but this is what I got…

9600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096009600960096….

That means it is not getting "stuck on the delay"

It prints 9600 for every iteration of the loop.

Well, I'm not sure that I see the value in printing 9600 over and over, but, whatever. It clearly means that the delay() is not hanging the loop() function, since, clearly, loop() gets called over and over.

Obviously, you are not stuck inthe delay. every 9600 is an execution of the loop.
Your servo is not responding as you expect. I don't know if I can provide anymore info, especially since you did not bother to tell us what values you used in your servo.writeMicroseconds() call.

Did you bother to read the values your servo needs that I wrote above?

Hello,

Sorry that I did not give you the complete info. Like I said i'm new to Arduino and I did not know what info you need. Also, I have read every post and link that you gave me. I just did not understand the links to well. Please keep in mind that i'm only 16 and I don't know anybody who knows anything about Arduino or electrons in general.

Here is the code that is currently running on the Arduino ( I used the values you suggested; 600 and 2400)

#include <Servo.h>

Servo myservo; // create servo object to control a servo

void setup()
{

myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
Serial.begin(9600);
Serial.print(9600);

myservo.writeMicroseconds(600);
delay(100);
myservo.writeMicroseconds(2400);

}

Thanks for bearing with me,
Drew Davis

I found the problem!!!

There needs do be a delay after the second "myservo.writeMicroseconds" the servo did not have any time to move and was stuck on the first "myservo.writeMicroseconds" because of the missing delay! It would keep printing because the loop keep running; the servo just could not keep up. Also, incase anybody finds it helpful I tried using "myservo.write" and that works to.

Thank's for trying to help me,
Drew Davis