Toggling servo position

I wrote a sketch to toggle the servo position and my leds between 90 degrees and 180, and turn one led on and one off. But nothing is happening...Any help would be appreciated.

#include <Servo.h>

Servo myservo;
const int buttonPin = 2;
const int ledRED = 13; // the pin that the LED is attached to
const int ledGREEN =  12; //green led
int servopos = 180;

void setup() {
  myservo.attach(5); // attach servo to pin 5
  // initialize the LED pin as an output:
  pinMode(ledRED, OUTPUT); // led as ouput
  pinMode(ledGREEN, OUTPUT); //led as output
  pinMode(buttonPin, INPUT); // nitialize pushbuttonpin as input
  myservo.write(servopos);
}

void loop() {
  if(buttonPin == HIGH) { 
    if(servopos == 180) { 
       digitalWrite(ledGREEN, LOW);
       digitalWrite(ledRED, HIGH);
       myservo.write(90);
       servopos = 90;
       delay(300);
    }
    else { 
      digitalWrite(ledRED, LOW);
      digitalWrite(ledGREEN, HIGH);
       myservo.write(180);
       servopos = 180;
       delay(300);
    }
  }
}

buttonPin is never HIGH, its value is 2

You meant, perhaps  if (digitalRead (buttonPin))

you sir deserve an award.

You might try the below to see if it works.

//zoomkat servo-LED button toggle test 11-12-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Mr. Zoomkat,
I need to mod this so that the servo toggles after a button press has been detected. In other words I don't want to hold the button down, but send a pulse to the arduino, move the servo 90 degrees, and then wait until another pulse is sent. Do I need to use the boolean command to do that? How do I toggle the state?
Thanks for any help!
Jim in Sweden

You need to get rid of the delays if you want the code to respond to the button quickly. Look at the blink without delay sketch.

You will also need to debounce the button, there is also an example sketch for that.

Mark

eatabean:
Mr. Zoomkat,
I need to mod this so that the servo toggles after a button press has been detected. In other words I don't want to hold the button down, but send a pulse to the arduino, move the servo 90 degrees, and then wait until another pulse is sent. Do I need to use the boolean command to do that? How do I toggle the state?
Thanks for any help!
Jim in Sweden

Have you tried the code?