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.

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

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?

Try this steps:

1. Make sure the servo is plugged in correctly. The orange/white should be connected to arduino pin 9, the red to 5v, and the white/brown to G. If the servo doesn't move at all, look into powering it externally, but the board should be able to handle a single servo without a problem.

2. Connect a button or temporary switch. If you don't have a button, you can just use two wires, one to the arduino and the other to ground and touch them together for a second. If you don't have any sort of button, try Serial.

3. Try the following code. This code will make the servo go to the center at first. Then every time you push the button, it should swing to either side.

#include <Servo.h> 

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

boolean ServLeft=false;  //this variable tells the servo which direction to go. not sure if the name is accurate.

void setup() 

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

pinMode(12,INPUT);  //YOU NEED TO ATTACH A BUTTON OR SWITCH BETWEEN THIS PIN AND GROUND

digitalWrite(12,HIGH); //this activates the atmega's internal pullup resistor

myservo.write(90);

delay(2000);

void loop() 

{

if (digitalRead(12)==LOW){    //this button tells the arduino to change servo direction

  (ServLeft)=(!ServLeft); //toggles which direction the servo will

 if (ServLeft==true){

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

  delay(2000);  

 }

  if (ServLeft==false){

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

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

  }

 

 

it only goes like 5% anticlock ways and back

I read a bit further up that you had switched to an external power supply going to your servo. Did you tie the ground of this external supply to the ground of your brain? You should do this.

do you mean: to connect the negative servo leed to A or B on figure 2

or both