Servo.write help

Hi, I have finally got my arduino out again and have begun making a very basic robot. I have built a chassis made up of a motor for drive and a servo for steering, and when I made it I made sure that the servo went back in both directions far enough. However when I ran the command servo.write(0); it never went very far back at all, only just turning left....

Is there anyway to make it go back further? I have tried servo.write(-20); but that just wrote it to 0. I know the servo will go back further though!!

Thanks

Simple code for testing servo operation using the serial monitor (for IDE 0019 and higher).

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later

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

void setup() {
  Serial.begin(9600);
  myservo.attach(7);
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(10);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);
    int n;
    char carray[6];
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray); 
    myservo.writeMicroseconds(n);
    //myservo.write(n);
    readString="";
  } 
}

My code is just a simple test code, taken from the sweep example code, If I change the myservo.write to 0 then I get the servo going to just a short distance to the left of what I believed to be the centre, if I put servo to write 180 it goes almost all the way round to be almost facing the wrong direction. If I put 20 it faces in a straight line, if I put 30 it goes right a small amount and so on until I reach 180.

#include <Servo.h> 
 
Servo myservo; 
 
int pos = 0; 
 
void setup() 
{ 
  myservo.attach(9); 
} 
 
 
void loop() 
{
  myservo.write(90);
}

@ zoomkat: I got the error ‘String’ does not name a type, so could not run your code, sorry.

I should probably add that I know, or at least remember very little about the arduino :stuck_out_tongue:

@ zoomkat: I got the error 'String' does not name a type, so could not run your code, sorry.

Try the code again. Looks like my copy/paste may have had issues.

Looks like it was my fault, I am running Arduino 0018 - probably because of how I installed it on Ubuntu... But it looks as though I am going to have to install the String library to get it working :) I shall post the result once I have done that

That would make sense :) But how would I go about resetting the servo's yoke?

Sorry for all the questions. Also zoomkat I did try your code on an up to date arduino program but the nothing seems to happen at all when I upload it.

ohhh that was so obvious and I it never even crossed my mind! Thanks for the help :)

Below is a version I used with IDE 0018 and the WString library.

// zoomkat 7-30-10 serial servo test IDE 0018
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// http://arduino.cc/en/uploads/Tutorial/String.zip for WString.h

#include <WString.h> //provides easy string handling
String readString = String(100);
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
      Serial.begin(9600);
        myservo.attach(9);
        }

void loop() {

        while (Serial.available()) {
        delay(10);  
          if (Serial.available() >0) {
        char c = Serial.read();  //gets one byte from serial buffer
        readString.append(c); } //makes the string readString
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
      int n;
      n = atoi(readString); //convert string to number
      myservo.writeMicroseconds(n);
      //myservo.write(n);
      readString="";
      } 
   }