How to stop loop with a toggle switch?

So I have a very basic problem: I want to run a program with a toggle switch, but have the program stop when I flip the switch again. So I want to be able to:

Flip switch
Program begins
Flip switch back (before loop completion)
Program stops, returns to original state

The program runs fine when I flip the switch, but I am unable to stop the loop using the toggle.

Background: I am pretty new to Arduino, and this is code I pulled and tweaked a bit. It is for brewing a teabag, then lifting it up at the end. I want to be able to turn it off with the switch while the program is running. I left out some code that I thought was irrelevant, let me know if I should include.

// SERVO SETUP
#include <Servo.h>
Servo myservo;

// HARDWARE SETUP
const int buttonPin = 2;
const int buzzerPin = 12;
const int ledPin = 13;

/////SERVO postions
const int brewingVal = 85; // WHEN THE STICK IS LOWERED TO SUBMERGE THE TEABAG
const int readyVal = 120; // WHEN THE STICK IS RAISED, WAITING FOR ACTION
const int movingVal = 95; // WHEN THE STICK STIRS THE ONBREWING TEA
/////



// STATE 0 READY, 1 BREWING!
int state = 0;
int old_state = 1;

void setup() {
  myservo.attach(10);
  pinMode(buttonPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
 int buttonState1 = digitalRead(buttonPin);
  
   if (buttonState1 == LOW) {
    state = 0;
    if (state != old_state) {
    
      digitalWrite(ledPin, LOW);
      myservo.write(readyVal);
      
      old_state = 0;
    }   
           
  }
  if (buttonState1 == HIGH) {
   state = 1;
    if (state != old_state) {
    
     digitalWrite(ledPin, HIGH);
      beep();
      startBrew();
      endBrew();
      beep();
      for (int i = 1; i< 15; i++) {
        digitalWrite(ledPin, LOW);
        delay(200);
        digitalWrite(ledPin, HIGH);
        delay(200);}
  old_state = 1;  
  }
    
  }
}

how about the switch dropping you into a while loop that only breaks when the switch is back in the "run" mode?

Or just hook the power to the board in series with the switch. Then you are sure to stop the program when you turn the switch off.

Goofballtech:
how about the switch dropping you into a while loop that only breaks when the switch is back in the “run” mode?

What function in the while loop would allow the board to read the state change of the toggle switch?

I experimented with while loops here, but couldn’t get anything to change:

void loop() {
  
  state = digitalRead(buttonPin);
  
  while (state == HIGH && old_state == LOW) {
    digitalWrite(ledPin, LOW);
      myservo.write(readyVal);
    
    old_state = HIGH;

  }
  
  while (state == LOW && old_state == HIGH) {
    
    digitalWrite(ledPin, HIGH);
      beep();
      startBrew();
      endBrew();
      beep();
      for (int i = 1; i< 15; i++) {
        digitalWrite(ledPin, LOW);
        delay(200);
        digitalWrite(ledPin, HIGH);
        delay(200);}
    old_state = LOW;
  }
  
  while (state == LOW && old_state == LOW) {
    
    digitalWrite(ledPin, LOW);
      myservo.write(readyVal);
    state = digitalRead(buttonPin);
    break;
  }
  
  while (state == HIGH && old_state == HIGH) {
    digitalWrite(ledPin, HIGH);
      beep();
      startBrew();
      endBrew();
      beep();
      for (int i = 1; i< 15; i++) {
        digitalWrite(ledPin, LOW);
        delay(200);
        digitalWrite(ledPin, HIGH);
        delay(200);}
    break;
  }
}

Goofballtech:
Or just hook the power to the board in series with the switch. Then you are sure to stop the program when you turn the switch off.

If I hook up the board in series with the switch, it won’t reset the servo back to a default position when I turn it off (at least not that I know of).

There is no rule saying that you can't check the state of a switch while inside the while loop...

while (switch){
 if (!switch){
   break;
  }
}

This won't work because you are not reading the state of the switch INSIDE the while loop. You have to be able to sense the state change in order for the program to know when to break.

Your criteria was

Flip switch
Program begins
Flip switch back (before loop completion)
Program stops, returns to original state

Didn't talk about servo positions... in all honesty i really didn't read the whole code in any case... i was kinda just being a smart a$$ talking about using the switch for board power...