How do I use a push-button to terminate a loop???

Greetings everyone.

I am monitoring a push button for presses.

Each button press increments a button counter, which a Switch statement uses to toggle between cases.
Everything is working fine, as seen in the following stripped-down code snippet:

void loop() {
  buttonState = digitalRead(buttonPin);
  delay(10);
   if (buttonState != lastButtonState) {
        if (buttonState == HIGH) {
            buttonPushCounter++;
     } 

  switch (buttonPushCounter){

case 0:
break;

case 1:
break;

case 2:
break;

etc...

However, the final case statement needs to be put into a loop of some kind, and the contents of that loop need to be performed until the button is pressed, which will break it out of the final case statement and begin the process again.

What is the simplest way to code…. “do something until a button is pressed”??

I have tried many variations of the digital read within the While condition, but nothing seems to work as I need it to.
I have tried:

Case 5:
while (digitalRead(buttonPin)==LOW)
{
…………
}
break;

I have tried:

While (digitalRead(buttonPin)==HIGH), while (digitalRead(buttonPin)!=LOW), !=HIGH, <>LOW, <>HIGH….etc..

Nothing seems to be working. I simply need the loop to function UNTIL the button is pressed, and then terminate.

Can anyone offer guidance? Please?

Thanks!

-Dave

ddeprospero:
Greetings everyone.

I am monitoring a push button for presses.

Each button press increments a button counter, which a Switch statement uses to toggle between cases.
Everything is working fine, as seen in the following stripped-down code snippet:

void loop() {

buttonState = digitalRead(buttonPin);
  delay(10);
   if (buttonState != lastButtonState) {
        if (buttonState == HIGH) {
            buttonPushCounter++;
//  put your switch-case block to handle button-presses here 
       }
        else { // the buttonState must be LOW
// now you believe the switch is up, code for that here
        }
    }

Is 10ms enough time to debounce the button? It depends on the button and the action of press/release.
How fast are your fingers anyway?

Well, that part works fine. No problems, no double presses, etc. That was part of some existing code that was in an Arduino tutorial here on arduino.cc - I did not modify anything.

The problem would be down in "case 5". I need some way of breaking out of a loop with ANOTHER button press.... so I press the button several times to get down into that case, and then once in a loop within that case, I need to press the button another time to break out (thus starting loop() over again).

How would I accomplish this?

So in case 5 you may want to wait for the first button to release or maybe not, with change in state if so.
Then you need to watch for the other button to do what you want.
Perhaps it is already pressed and you wait for it to be released then pressed again, I don't know.

Waiting for press;

delay(10); // or you can put it after
while (!digitalRead(otherButtonPress));

Add: you know if your code leads right back to case 5 then it may look 'stuck'.