Push button to trigger the start of a continuous loop

Hi,

Several similar questions have been posted before but none gave a solution that works for me, so I was hoping y’all could maybe take a look.

I’m trying to write my code so that after setup() runs and I enter loop(), I don’t want anything to happen until a push button is pressed. This push button simply determines the start of my “actual code” and nothing else.
I then want my “actual code” to execute and run for 5 hours. The button, of course, will not remain pressed for this duration. It is only meant to start the execution of the 5-hour-loop.

The way I thought I’d do this is:

int previous_push = HIGH;

void loop()
{
   push_state = digitalRead(pushbutton);

     if(push_state == LOW && previous_push == HIGH && millis() - push_time > debounce)
     {
       Serial.println("BUTTON PRESSED!");
   
       for(uint32_t start_time = millis(); millis() - start_time <= 5 * 60 * 60 * 1000); )
        {

             //EXECUTE MY INSTRUCTIONS UNTIL 5 HOURS ARE UP

         }
      }
  previous_push = push_state;
}

Apparently this way the for loop only executes continuously if I keep pressing the push button. Is there a better way to do this?

Given the fact that the interior is expected to run for five hours debouncing is not necessary. A simple while at the top of loop is what you need.

  1. Thanks for responding!

  2. No debouncing needed. Noted.

  3. Can you explain how the while would be set up? Do you mean:

void loop()
{
  while(!(push_state));            //Here, button pressed = 0; button not pressed = 1;
  
  for(uint32_t start_time = millis(); millis() - start_time <= 5 * 60 * 60 * 1000); )
        {

             //EXECUTE MY INSTRUCTIONS UNTIL 5 HOURS ARE UP

         }

}
  // Here, button pressed = 0; button not pressed = 1;
  while ( ! digitalRead(pushbutton) );

This makes sense but it’s not working. The program doesn’t wait for the button to be pressed and directly jumps into the “actual function” loop.

Here’s how I have it:

void setup()
{
  //Other setup stuff

  push_state = digitalRead(pushbutton);
  
  //Until push_state goes low, indicating that the button has been pressed, the while loop will keep an infinite loop going
  
  while(!(push_state));                        
  Serial.println("(BUTTON PRESSED!)");
}

void loop()
{
  for(uint32_t start_time = millis(); millis() - start_time <= (DURATION * 60 * 60 * 1000); )
    {
      doTheThing();
    }

}

There are two problems. Assuming the comment is correct there is a hardware problem. I suggest you start with a simple sketch that echos the state of the button.

The code (snippet that cannot possible compile) you posted is not even close to functionally equivalent to the code I posted. I suggest you reread post #3.

  1. Already checked to make sure the switch reads the way I thought it should. I wrote a sketch to print the state of the push button on a lcd. It's 1 when not pressed and 0 when pressed.\

  2. True, it isn't exactly how you suggested it, but even when I had it at the beginning of loop() it performed the same the way. It wouldn't wait for the button input and instead directly printed out "Button Pressed" and executed doTheThing()

EDIT: I SEE MY ERROR!!!! I am sorry, after your reply I moved everything to the start of the loop() and instead of using push_state I straight up put while(digitalRead(pushbutton)) instead (without !). I see what you meant. THANK YOU so much for taking out the time to help.