Servo Help

Would someone please help me figure out why my servo code isnt working?

#include <Servo.h>

Servo leftWheel ;
Servo rightWheel ;

void setup()
{
rightWheel.attach(9 ) ;
leftWheel.attach(8 ) ;
}

void loop()
{
rightWheel.write(100);
delay(15) ;
rightWheel.write(90);
delay(15) ;
}

thanks! also, how does the refresh function in the servo library work?

You sure you have the servos wired correctly?

Also the servos should be running on separate power as he arduino can't power them and you might damage MCU

Post your diagram.

Rick321123:
how does the refresh function in the servo library work?

"refresh" is used with the SoftwareServo library; you're using the Servo library and the "refresh" calls occur automatically.

When you get a deeper understanding of how timers work you might need to use the SoftwareServo library but until then just stay away from it :wink:

void setup()
{
  rightWheel.attach(9 ) ;
  leftWheel.attach(8 ) ;
}

Is that a space after the (9 ) and (8 )?.... not sure if it would make a difference though?

And when you say it "isn't working", what does that actually mean? What are you expecting to happen, and what actually happens?

Are you expecting the vehicle to move forward?- cos each time it does the write in loop() it stays where it is since the value hasn't changed. If I understand servoes correctly, the value is an absolute position, not an increment.

Is that a space after the (9 ) and (8 )?

It is.

not sure if it would make a difference though?

It doesn't.

The use of "wheel" and "servo" leads me to suspect these are continuous rotation ex-servos.
Finding the "stop" position can be tricky, and it may be better to use the writeMicroseconds method for finer control.

Have a look for some of zoomkat's servo control sketches.

Watch this tutorial and see if it helps!

Servo test code you can try to make sure your servos are set up correctly.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

Connect servos to digital pins marked "~" for PWM control. D8 is not marked "~"

Connect servos to digital pins marked "~" for PWM control. D8 is not marked "~"

Thats wrong, any pin will do. The ~ only applies for PWM through the analogWrite function which is not the type of PWM that a servo expects.

This is the type of signal that a servo expects -

Use the servo library and you can attach a servo to which ever pin you choose.

Avoid using pins 0 and 1 though as you will probably want them for serial comms.

Duane B

rcarduino.blogspot.com