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.

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

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

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.

I tried, but it still only goes to one position

Agreed, try delay(500) or even longer

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