Help with Servo Control

i have a servo and i am wanting to use two button, one to turn it clockwise and the other to turn it anticlockwise. When i press either button nothing happens, any help would be much appreciated. There is an diagram of my wiring below called breadboard.bmp
This is the code i have so far.

#include <Servo.h> 
Servo servo;
int pos = 0;
int buttonCState = 0;
const int buttonCPin = 3;
int buttonACState = 0;
const int buttonACPin = 5;
void setup() 
{ 
  servo.attach(9);  
  pinMode(buttonCPin,INPUT);
  pinMode(buttonACPin,INPUT);
} 
void loop() 
{ 
  buttonCState = digitalRead(buttonCPin);
  buttonACState = digitalRead(buttonACPin);
  while(buttonCState == HIGH){
    if (pos < 180){
      pos = pos + 1;
      servo.write(pos);
    }
    while(buttonACState == HIGH){
      if (pos > 0){
        pos = pos - 1;
        servo.write(pos);
      }
    }
  } 
}

breadboard.bmp (628 KB)

Simple servo test code to see if you have wiring or power issues with your servo setup.

// 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
  } 
}

are you using standard or continious rotating servos?

Also something my professor just taught me is to get into the habit of not using the pictures to wire your arduino but try to use the actual schematic diagram to configure your circuit. It allows you to know where each input is going and where voltage and ground are. The pictures can often be confusing and easily mistaken when you try to mimic the diagram.

zoomkat, i have ran the code you provided and my servo works correctly.

flore1nr, I believe its a standard servo, it has a rotation angle of 180°. I wasn't wiring from the diagram, i did my wiring first then created the diagram so i could upload it with my post.

ok I see that now my apologies. So you are trying to move the servos 1 degree at a time you might wanna try using a for loop instead of an if statement and make sure to have some delay in there. The sweep example arduino provides might be helpful I'd suggest looking at how they programed the servos to move. http://arduino.cc/en/Tutorial/Sweep

Take the servos out of the equation for a moment - is your sketch detecting button presses at all?

Simple button test code for servo testing. Wires attached to pins 4 and 5 can be touched to ground to make the servo move. In the origional code posted, if the servo is to move slowly, a delay setup will be needed between the increasing/decreasing of the servo position values. look at the servo sweep example code.

//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
  
  /*else {
    servo1.write(90);
  }*/
}