Arduino Uno Servo Motor Push Button

Hi, I am working on a project where im essentially creating a flicking device through arduino with push button and servo motor. My code right now makes it so when i push the button once it goes one direction and i have to push it again for it to go back. Basically i need it so when i push the button once it flicks down and back up to the initial position. My code right now is below, any help would be greatly appreciated.

#include <Servo.h>
int button = 4; //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);
}

void loop()
{
press = digitalRead(button);
if (press == LOW)
{
if(toggle)
{
servo.write(130);
toggle = !toggle;
}
else
{
servo.write(80);
toggle = !toggle;
}
}
delay(500); //delay
}

When you detect that the button has [u]become[/u] pressed (rather than when it [u]is[/u] pressed) move the servo to 130, wait a while then move it back to 80. Do not move the servo again until the button [u]becomes[/u] pressed again.

Look at the StateChangeDetection example in the IDE for methods to do the detection of a change of state.

If the program does nothing but control the servo then use of delay() is the easiest way to implement the wait otherwise a different technique will be required.

Here is a short sketch to illustrate what UKHeliBob said. It uses the state change detection method to sense the button becoming pressed (as opposed to is pressed). I changed the pinMode for the button to INPUT_PULLUP. If you have an external pullup that change is not necessary.

#include <Servo.h>
const byte button = 4; //button pin, connect to ground to move servo

Servo servo;

boolean buttonState = HIGH;
boolean lastButtonState = HIGH;

void setup()
{
   pinMode(button, INPUT_PULLUP); //arduino monitor pin state
   servo.attach(7); //pin for servo control signal
   digitalWrite(5, HIGH);
}

void loop()
{
   buttonState = digitalRead(button);
   if (buttonState != lastButtonState)
   {
      if (buttonState == LOW)
      {
         servo.write(130);
         delay(300);  // give the servo time to move.  Adjust as necessary
         servo.write(80);
      }
      lastButtonState = buttonState;
   }
}

Read the how to use this forum sticky to see how to properly post code.

That helps alot. I tested it with the system and it is doing what we need it to, however the servo motor will do the loop when you press the button and when you release the button. Is there anywhere to modify the code so that is just flicks and comes back to the initial position for just one button press?

You may need to debounce the button switch. An easy way is to hardware debounce the switch with a 0.1uf cap between the input pin and ground (across the switch). Or use software debounce.

On my test setup (Uno and servo) the servo only sweeps on button press, not on release.

So inputting into the code a debounce for the button using the millis commands and such should fix it?

I don't know. It is worth a try. The best explanation the difference in what your circuit does versus what my test circuit does is switch bounce. I just caps to debounce my switches. Have done it like that for decades.