*PLEASE READ* servo question

Hello there, i am new to arduino and coding with arduino and i need help writing a code to control my servo!
what i am wanting to do is when i press a button, i need my servo to move to 180 degrees and stop, and when i press the button again i need the servo to return to 0 degrees and stop. can someone please help me!?
thank you!

Checkout zoomkat's posts.

I think there was a very recent Thread with an almost identical question. Use Google to search the forum over the last month and you may find it.

Have you got any servo code working yet?

Have you tried the various servo examples that come with the Arduino IDE?

...R

could it be this one ?

Toggle a servo using a button.

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

zoomkat:
Toggle a servo using a button.

And if you hold the button down, it oscillates between the two positions once each second.

Paul__B:

zoomkat:
Toggle a servo using a button.

And if you hold the button down, it oscillates between the two positions once each second.

True. The OP will need to determine if the button press will be momentary (less than the debounce delay), or extended. If the press goes on for a long time, he will probably need to add flags to his code to record button state changes and act upon the status of the flags for the toggle event.