Push button servo control help

Hello
Can someone guide me through making a code to control what angle the servo will go when i push a push button. I want it to go to a position and stay there when i push it once, then when i push it again, it will go back to original angle. Im using the push button on a joystick for this. I try it out but i cant seem to get it to work. I've got the push button pin going into the digital 2 pin. What am i doing wrong :frowning: Thanks a million !!

#include <Servo.h>         			// include a library that does all the hard work controlling the servo

Servo servo1;              			// declare servo #1
int gripbtn = 2; 								
int gripPOS = 0;				// intitialize gripper position variable
int buttonState = 0;  				// Variable for reading the pushbutton status
int directionState = 0;

void setup() {
  Serial.begin(9600);         
  pinMode(gripbtn, INPUT);			// let Arduino know that this is an input
  servo1.attach(11);                   		// pin #11 for servo 1

}
void loop() {
  gripPOS = digitalRead(gripbtn);		// digital input for push button

if (buttonState == HIGH) {
      directionState = 1;// The direction for the servo is clockwise

      // goes from 0 degrees to 180 degrees in steps of 1 degree
      for(gripPOS = 0; gripPOS < 180; gripPOS=gripPOS+1)
       {
         servo1.write(gripPOS);  // tell servo to go to position in variable ‘pos’ 
         delay(15);  // waits 15ms for the servo to reach the position 
       }
     }
 else if (directionState == 1) {
    // The button is pushed
    if (buttonState == HIGH) {
      directionState = 0;  // The direction for the servo is anti-clockwise 

       // goes from 180 degrees to 0 degrees in steps of 1 degree 
       for(gripPOS = 180; gripPOS>=1; gripPOS=gripPOS-1)
       {
         servo1.write(gripPOS);  // tell servo to go to position in variable ‘pos’ 
         delay(15);  // waits 15ms for the servo to reach the position 
       }
     }
   } 
}

ok, i figured it out. in the start of my void loop, i just changed it to

buttonState = digitalRead(gripbtn);

but now ... when i press the button it kinda stalls out and it goes to 0 degrees quick, then goes to 180 before i press the button. ....

#include <Servo.h>         			// include a library that does all the hard work controlling the servo

Servo servo1;              			// declare servo #1
int gripbtn = 2; 								
int gripPOS = 0;				// intitialize gripper position variable
int buttonState = 0;  				// Variable for reading the pushbutton status
int directionState = 0;

void setup() {
  Serial.begin(9600);         
  pinMode(gripbtn, INPUT);			// let Arduino know that this is an input
  servo1.attach(11);                   		// pin #11 for servo 1

}
void loop() {
  buttonState = digitalRead(gripbtn);		// digital input for push button

if (buttonState == HIGH && directionState == 0) {
      directionState = 1;// The direction for the servo is clockwise

      // goes from 0 degrees to 180 degrees in steps of 1 degree
      for(gripPOS = 0; gripPOS < 180; gripPOS=gripPOS+1)
       {
         servo1.write(gripPOS);  // tell servo to go to position in variable ‘pos’ 
         delay(15);  // waits 15ms for the servo to reach the position 
       }
       buttonState = LOW;
     }
 if (buttonState == HIGH && directionState == 1) {
    // The button is pushed
    if (buttonState == HIGH) {
      directionState = 0;  // The direction for the servo is anti-clockwise 

       // goes from 180 degrees to 0 degrees in steps of 1 degree 
       for(gripPOS = 180; gripPOS>=1; gripPOS=gripPOS-1)
       {
         servo1.write(gripPOS);  // tell servo to go to position in variable ‘pos’ 
         delay(15);  // waits 15ms for the servo to reach the position 
       }
     }
   } 
}

Made a couple of small changes.
What I did to find the problems was to put Serial.println(gripPOS) after servo1.write(gripPOS) in both for-loops. I only had the button connected. This way I could open serial monitor and watch what was actually going on with the position. Just a tip for next time :wink:
Why did you have Serial.begin(9600) by the way? Wouldn't need that.

Simple servo button toggle code.

//zoomkat servo button toggle test 4-28-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(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)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Why not just cut the code down to just one line. Also a state change flag would be better than a 500 millisecond delay, no?

if (press == LOW)
  {
   servo.write( toggle = !toggle ? 160 : 20); // If press is LOW, toggle is inverted and the output is follows.
  }