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.

it only goes like 5% anticlock ways and back

Can you post a picture of you setup? Normally servos cant be turned (easily by hand) if there is a signal going to them. They will move on startup as the servo tries to gather the signal being sent to it.

 Geir Andersen told me to power the servo externally and when I did it was pretty hard to turn

Specifically 0 to 45 and 100 to 180. Does the servo move to a different angle?

yes if i use only one part of the code

What do you mean by only one part of the code?

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

I have, the servo is more powerfull, butt does only go to one position

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)