Simple button exit or entry

Hi all, I know this is simple, but I have been looking at this program too long and just don't see the answer. I have tried both while and if loops but neither checks the button continuously, only whenever it loops around to check the condition.

I need to have a button held to start a test, check 3v3, then ir_Test, etc., but if the button is released, I'd like to exit from the loop and go back to the else statement, clear all the outputs and wait for the button again.

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(testPoint6, HIGH);
    digitalWrite(testPoint7, HIGH);   //  this needs to change to LOW for actual test, but is high for scope testing.....
    digitalWrite(sw_12VDC, HIGH);
    delay (500);                    // wait .5 seconds for 12 V to come up and board to start.
  
      check3V3 ();
      // should exit to else condition if button not pressed anymore
      
      ir_Test ();
      // should exit to else condition if button not pressed anymore


      voltage_Conv_RED();
     // should exit to else condition if button not pressed anymore

      
      voltage_Conv_GRN();
      // should exit to else condition if button not pressed anymore

    
  } else {
    // turn board and LEDs off until new board is inserted:
    digitalWrite(testPoint6, LOW);
    digitalWrite(testPoint7, LOW);
    digitalWrite(sw_12VDC, LOW);  
    digitalWrite(led_ir_detect, LOW);
    digitalWrite(13, LOW);

    lcd.home (); // go home
    lcd.clear(); // go home
    lcd.print("Waiting..."); 
    delay (1000);       // stop fading on LCD

  }
}

I think you need to describe the behaviour you want as a state-machine, then you can
program in each transition between states accordingly. Sounds like you have an idle state
waiting for a button press, a button-down state which progresses through some sub-states
as you call each sub function.

More simply you might want to add lines like this around inside loop():

  if (digitalRead (buttonPin) == LOW)
  {
    tidy_up () ;
    return ;
  }

Statements like return and break are useful for exiting functions and loops respectively.

That's exactly what I tried, but it didn't work, I'll play with that again and maybe I did a typo or something. Thanks. I'll let you know.

Davewerld:
only whenever it loops around to check the condition.

That's the correct way for it to happen. You need to design the rest of your program with that in mind. loop() should repeat hundreds or thousands of times per second.

You may get some ideas from Planning and Implementing a Program

...R

Worked, it was a typo on my part. I did

buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {

but when I cut and pasted, I only had

if (buttonState == LOW) {

And that doesn't check anything new. Thanks for looking, it helped me out.

Robin, thanks, I understand that, I just missed that digitalRead and then got lost. I think I was looking for something a lot harder than the reality.