Servo coding (solved)

arduino_servo.png (17522Bytes)
arduino_servo_a_or_b.png (18126Bytes)

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);    

 

If your servo is taking its

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

Too fast

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.

unnessary code

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     

}

Agreed, try delay(500) or

Agreed, try delay(500) or even longer

add a counter

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.

Thank you

I tried, but it still only goes to one position

Thanks

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

Again. Have you tried with a

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

I have

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

Is you servo connected correctly?
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.

I am not building an robot yet

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)

** Geir Andersen told me to**

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

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

yes

yes if i use only one part of the code

how about a button?

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 

  }

 

 

counter variable

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

So the servo is going to different angles-
What do you mean by only one part of the code?

no still not

it only goes like 5% anticlock ways and back

Are your grounds tied together?

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.