If statement

Do I use the if statement to tell the code to loop through let's say when I push a button, and stops if the button is no longer pressed? Basically, I want a loop to run through completely at the push of a button, then stop at the end, push the button, it starts again! Yes? No?

You could possibly use "if" if you're already in a looping construct, or it may be more appropriate to use "while"


You should not block your main loop.
Do this instead;

#define ButtonPin  4

unsigned long nowMillis=0;
unsigned long debounceTimer=0;
void setup() {
  // put your setup code here, to run once:

void loop() {
    if ((digitalRead(ButtonPin)==1) && ((nowMillis - debounceTimer) > 250))
      //Call function
       debounceTimer = millis();
void myFunction() {
  // put your code here
  //Will run once every time you press your button

Unfortunately, that is only half a debounce code!

As written, it will repeat continuously whilst the button is pressed. It is likely that is not what is desired; you need the other half of the debounce code to definitively identify that the button has in fact been released before detecting a new press.

And a quarter second is probably just a little excessive for a debounce time; it is possible to press a button at least five times a second, so the debounce time probably should not exceed 50 ms.

Paul, sounds like you understand what I need. The button would tell a robotic arm to move to a point and back. So I need the button to send this info to make the action come back and end until the button is pressed again.