Button to begin loop

So the project I want is:

push a button to begin and continuously run a loop.

What I have:

I push a button and the loop runs once.

What I've programmed:
I'm using an if statement to say if I push the button, run the loop. Of course, since the loop begins with the "if the button is pushed" requirement I have to push the button each time to initiate the loop. I've tried moving the if statement out of the loop, but then the loop just runs regardless of whether or not I push the button.

What I need:

Your help!

Use the state change detection method to sense when the button becomes pressed and set a flag. Then if the flag is set run the loop. If the button becomes pressed again, clear the flag to stop the loop.

It seems OP only wants to start the loop with a button.

Try a While() loop at the end of setup().

while(digitalRead(buttonPin)); // connect button between pin and ground, with pull up enabled in pinMode

Leo..

I guess button wasn't quite the right term, perhaps tactile switch is more accurate like this one:

I am trying to have nothing happen until I push the tactile switch. Once pressed, I would like the code to run in a loop until the sun burns out. Or I push a second tactile switch. Whichever happens first.

Here's the code I have

int ledAPin = 13;
int ledBPin = 12;
int ledCPin = 11;
int ledDPin = 10;
int ledEPin = 9;
int pushbuttonAPin = 3;
int pushbuttonBPin = 5;
void setup() {
pinMode(ledAPin, OUTPUT);
pinMode(ledBPin, OUTPUT);
pinMode(ledCPin, OUTPUT);
pinMode(ledDPin, OUTPUT);
pinMode(ledEPin, OUTPUT);
pinMode(pushbuttonAPin, INPUT_PULLUP);
pinMode(pushbuttonBPin, INPUT_PULLUP);

}

void loop ()
{
 if(digitalRead(pushbuttonAPin) == LOW)
 {
   digitalWrite(ledAPin,HIGH);
   delay(1000);
   digitalWrite(ledAPin,LOW);
   digitalWrite(ledBPin,HIGH);
   delay(1000);
   digitalWrite(ledBPin,LOW);
   digitalWrite(ledCPin,HIGH);
   delay(1000);
   digitalWrite(ledCPin,LOW);
   digitalWrite(ledDPin,HIGH);
   delay(1000);
   digitalWrite(ledDPin,LOW);
   digitalWrite(ledEPin,HIGH);
   delay(1000);
   digitalWrite(ledEPin,LOW);
   
 }
}

(URL and code tags added - moderator)

You can use the while function, it should loop indefinitely. Newbie myself, hope this gets you what you need.

What @Wawa said. :slight_smile: