Switch that starts entire arduino program


I’m working on a project and I have been mulling over on how to program a switch that initiates my entire arduino program along with when the switch is turned on, the program runs for a certain amount of time then stops.

could anyone enlighten me on how to program this?

I guess the switch could be in the power supply?

Then for the code, look at the blink without delay example. Do all the work in setup() so that when the time’s up and it moves on to loop() there’s nothing there to do. The sketch will still continue to run loop() but will look like it’s stopped since there’s nothing there.

That’s off the top of my head… E&OE, YMMV, Ts and Cs apply…

Assuming an active low switch (ie pressing the button makes it go LOW)

void setup(){
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);  // turn on pullup resistor
while(digitalRead(buttonPin) == HIGH);  //  Note the semicolon, while it's HIGH do nothing.

//  rest of setup here

void loop(){
//  loop code here

Hold the reset pin low, when released your program will start.

Thank you Delta, your suggestion worked like a charm.