button to begin sequence.

im new to arduino so please forgive my stupidity if this is just a simple problem.

how can i use a button to begin a loop program?

what im trying to do is get three servos to move in a pre programmed manner (which ive already figured out) whenever a button is pressed.

so basically the button just needs to start the loop for one cycle and then have the arduino wait for the button to be pressed again before continuing through another cycle.

how can this be accomplished? thanks

Poll the button to check for a button press
When the button gets pressed call a function to carry out your loop
the function will then return to yoru main loop and again wait for the next button press

Here’s how I do it:

#include <Button.h>//http://www.arduino.cc/playground/uploads/Code/Button.zip

const byte ledPin = 13;
Button button = Button(12,PULLUP);

void setup(){
pinMode(ledPin,OUTPUT); //debug to led 13
}

void loop(){
if(button.uniquePress()){//button.isPressed() will cause the code to execute as long as button is pressed
//enter loop code here
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000);
//end loop code
}
}

:slight_smile:

Just add an if statement before the rest of your code in the loop() function to check the state of the button. Depending on how you wire the button (pull up or pull down) you will look for a High or a low on your button pin. Make sure to use a pull up or pull down resistor though so you don’t get a floating pin. Explained a little here:

Here is a push button example:
http://www.arduino.cc/en/Tutorial/Pushbutton

void loop()
{
if (button == HIGH) // Check for the opposite of what your button will normally be
{
“The rest of your code”
}
}

Edit: Guess I wasn’t fast enough. Good call using the button library to debounce. I always forget that. Here is info on debouncing. Basically it makes sure your program wont accidentally run more than once if you hold the button down for too long. http://www.arduino.cc/en/Tutorial/Debounce