Let's Make Robots!

Servo coding (solved)

It is working now, thanks to everyone who helped me out!!!

 

 

I am trying to make my servo work,but if I start my arduino, with code and servo attached, it just goes to one position and stays there twitching. Can someone help me?

second try

I changed the delay to two seconds, but the servo only goes to the first 100 degree and does not move after that unless if I turn the servo. Could it be the servo? I use:http://www.dealextreme.com/p/sg90-mini-servo-with-gears-and-parts-2kg-torque-35764

Code:

 

#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() 

 myservo.write(0);                  // sets the servo position according to the scaled value 

  delay(2000);  

 myservo.write(100);                  // sets the servo position according to the scaled value 

  delay(2000);                        // waits for the servo to get there 

   myservo.write(0);                  // sets the servo position according to the scaled value 

  delay(2000);    

 myservo.write(100);                  // sets the servo position according to the scaled value 

  delay(2000);                        // waits for the servo to get there 

 

 

First try

I used a part of the knob controlled servo example.

Code:

 

 

#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() 

 myservo.write(0);                  // sets the servo position according to the scaled value 

  delay(20);  

 myservo.write(100);                  // sets the servo position according to the scaled value 

  delay(20);                        // waits for the servo to get there 

   myservo.write(0);                  // sets the servo position according to the scaled value 

  delay(20);    

 

AttachmentSize
arduino_servo figure 119.97 KB
figure 2_a_or_b28.92 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

If your servo is taking its power from the Arduino, try an external power source.

Your delays are not long enough to allow your servo to get to where it is going. As soon as it trys to move to a position, 20ms later it is told to go somewhere else. Make your delays longer.

Agreed, try delay(500) or even longer

I tried, but it still only goes to one position

The second one of these is not neeted

myservo.write(0);                  // sets the servo position according to the scaled value 

delay(20);

Also as Chris said your delays need to be longer.

So instead of:

void loop() 

{  myservo.write(0);                  // sets the servo position according to the scaled value 

  delay(20);  

  myservo.write(100);                  // sets the servo position according to the scaled value 

  delay(20);                        // waits for the servo to get there 

  myservo.write(0);                  // sets the servo position according to the scaled value 

  delay(20);    

}

You should have:

void loop() 

 myservo.write(0);                  // sets the servo position to zero degrees

  delay(750);                         // waits for the servo to get there

 myservo.write(100);              // sets the servo position to 100 degrees

  delay(750);                        // waits for the servo to get there     

}

I tried this one two, but that did not work either

If your bot is doing a lot of things simultaneously in addition to moving servos, you could also benefit from a "servo counter" variable that counts how long since you gave the myservo command (in this case, your targe would be 750 or so). Once the servo is in place, you can turn it off. This way the servo won't sit there pulling your current and making noise. once the servo is told to move again, just reset the variable.

I am not building a robot yet, so I am only using the wall outlet. but do can you give an example of an counter( I could not find it on ebay)

Let's get your servo working first, then we can worry about prettying up your code. What's the bot going to do?

Again. Have you tried with a separate power supply for the servo?