Interrupting a DO-WHILE loop with IF statements

I am trying to build a Whack-a Mole like game using servos to move the heads up and down. I have been practicing with Arduino for about a year. This is the first time that I am trying to use "loops within loops" and I have tried multiple ways (if,else-if, else...switch...for...while...and do-while) opportunities to get a result. I have referenced multiple texts and read multiple examples on the forum.

The hardware is 3 servos, and 4 pushbuttons, and I am using an Arduino Uno r3. The desired output is that when the player is initially prompted to do so, they will press each of the three buttons (reading 1 through 3) and the heads will each raise for one second and then drop. This part of the code works fine.

The 4th push button is used to start a loop. when the player is presented with a case (which is on a laptop). The player pushes the 4th button, and the heads begin to move up and down, until a choice is made on one of the three choice buttons (reading 1 through 3). At which point all of the heads drop, and the loop is broken so that even if the number of heads routines planned is not completed, the heads all drop (ideally it would also raise the selected choice, and I have code written for this, but I can't seem to break the loop). Then the loop should return for the next case, where the player would choose to press pushbutton 4 again, and the whole cycle repeats, waiting for a choice (reading 1-3).

My issue is that my code executes the loop (DO-WHILE), but I am unable to break the loop (IF statements with BREAKS), when the buttons are pressed. It just continues to loop until the number of loops (heads) completes.

Any help is truly appreciated :slight_smile:

/* This sketch controls a servo-controlled Whack a Mole game with 3 servos and 3 push buttons and a starter button with an ARDUINO UNO
  by Chad Jackson
  It is controlled by four push buttons
  Pressing Button A raises Head A
  Pressing Button B raises Head B
  Pressing Button C raises Head C
  Pressing Button D starts the heads moving up and down individually for 15 seconds
*/

#include<Servo.h> // include Servo library

int heads = 0;

int inPin1 = 1; // the switch is wired to Arduino D1 pin
int inPin2 = 2; // the switch is wired to Arduino D2 pin
int inPin3 = 4; // the switch is wired to Arduino D4 pin
int inPin4 = 7; // the switch is wired to Arduino D7 pin

int reading1; // the current reading from the input pin 1 Swtich A
int reading2; // the current reading from the input pin 2 Switch B
int reading3; // the current reading from the input pin 4 Switch C
int reading4; // the current reading from the input pin 7 Switch to initiate heads moving

Servo myservoA; // create servo motor object A
Servo myservoB; // create servo motor object B
Servo myservoC; // create servo motor object C

void setup()
{

  myservoA.attach(3); // attach servo motor to pin 3 of Arduino
  myservoB.attach(5); // attach servo motor to pin 5 of Arduino
  myservoC.attach(6); // attach servo motor to pin 6 of Arduino

  pinMode(inPin1, INPUT); // make pin 1 an input
  pinMode(inPin2, INPUT); // make pin 2 an input
  pinMode(inPin3, INPUT); // make pin 4 an input
  pinMode(inPin4, INPUT); // make pin 7 an input

  myservoA.write(55); // rotate servo motor A to 45 degrees or HOME
  myservoB.write(55); // rotate servo motor B to 45 degrees or HOME
  myservoC.write(55); // rotate servo motor C to 45 degrees or HOME

}

void loop() { //WAITING FOR SWTICH INPUT

  reading1 = digitalRead(inPin1); // store digital data in variable
  reading2 = digitalRead(inPin2); // store digital data in variable
  reading3 = digitalRead(inPin3); // store digital data in variable
  reading4 = digitalRead(inPin4); // store digital data in variable

  myservoA.write(55); // servo motor Lowers Head A
  myservoB.write(55); // servo motor Lowers Head B
  myservoC.write(55); // servo motor Lowers Head C


  if (reading1 == HIGH) { // check to see if switch ONE has been pushed
    //Raise Head ONE
    myservoA.write(135); // servo motor rotates 150 degrees to Raise Head ONE
    delay(1000); // wait 1000ms for rotation
    myservoA.write(55); // servo motor Lowers Head A
    myservoB.write(55); // servo motor Lowers Head B
    myservoC.write(55); // servo motor Lowers Head C
  }


  if (reading2 == HIGH) { // check to see if switch TWO has been pushed
    //Raise Head TWO
    myservoB.write(135); // servo motor rotates 150 degrees to Raise Head Two
    delay(1000); // wait 1000ms for rotation
    myservoA.write(55); // servo motor Lowers Head A
    myservoB.write(55); // servo motor Lowers Head B
    myservoC.write(55); // servo motor Lowers Head C
  }


  if (reading3 == HIGH) { // check to see if switch THREE has been pushed
    //Raise Head Three
    myservoC.write(135); // servo motor rotates 150 degrees to Raise Head THREE
    delay(1000); // wait 1000ms for rotation
    myservoA.write(55); // servo motor Lowers Head A
    myservoB.write(55); // servo motor Lowers Head B
    myservoC.write(55); // servo motor Lowers Head C
  }


  if (reading4 == HIGH) { //Moves heads up and down
    int heads = 1;

    do {
      if (reading1 == HIGH)
        break;
      if (reading2 == HIGH)
        break;
      if (reading3 == HIGH)  // check to see if a switch has been pushed
        break;

      //MOVES Servos
      myservoA.write(135); // servo motor Raises Head A
      delay(500); // wait 500ms for raising head
      myservoA.write(55); // servo motor Lowers Head A
      delay(500); // wait 500ms for lowering head
      myservoB.write(135); // servo motor Raises Head B
      delay(500); // wait 500ms for raising head
      myservoB.write(55); // servo motor Lowers Head B
      delay(500); //wait 500ms for lowering head
      myservoC.write(135); // servo motor Raises Head C
      delay(500); // wait 500ms for rotation
      myservoC.write(55); // servo motor Lowers Head C
      delay(500); //wait 500ms for lowering head

      heads = heads + 1;

    } while (heads != 5);


    //Program loops to top, waits for Button
  }
}

Indepthdude:
but I am unable to break the loop

The simple answer is not to use DO/WHILE at all. Just use a counter variable to keep track of the number of repeats and allow loop() to do the iteration - that's why it is there.

...R

I truly appreciate your time and reply.

However, I have now tried several variations of eliminating the DO-WHILE loop, and in each case it only executes the code one time after Button 4 is pushed. I want the heads to go up and down 5-15 seconds while the player decides on the choice they will make (reading 1-3).

The code I have works to initiate the loop when Button 4 is pushed, and repeats it the number of times I want (heads).

What I want is to interrupt that loop when you make a choice (reading 1-3) when any of the buttons go high on a push button press.

Here is the relevant code again:

  if (reading4 == HIGH) { //Moves heads up and down
    int heads = 1;

    do {
      if (reading1 == HIGH)
        break;
      if (reading2 == HIGH)
        break;
      if (reading3 == HIGH)  // check to see if a switch has been pushed
        break;

      //MOVES Servos
      myservoA.write(135); // servo motor Raises Head A
      delay(500); // wait 500ms for raising head
      myservoA.write(55); // servo motor Lowers Head A
      delay(500); // wait 500ms for lowering head
      myservoB.write(135); // servo motor Raises Head B
      delay(500); // wait 500ms for raising head
      myservoB.write(55); // servo motor Lowers Head B
      delay(500); //wait 500ms for lowering head
      myservoC.write(135); // servo motor Raises Head C
      delay(500); // wait 500ms for rotation
      myservoC.write(55); // servo motor Lowers Head C
      delay(500); //wait 500ms for lowering head

      heads = heads + 1;

    } while (heads != 5);

I took this coding example used from the MAKE: GETTING STARTED WITH ARDUINO, 3rd Ed. Book, page 221.

I tried to apply what you said and tried this code below. It only moves Servos B and C when no button is pressed, and runs the correct loop for Button 4 if I hold the button (thus keeping reading 4 = true), but again none of the choice buttons work (reading 1, 2, 3).

CODE SAMPLE 2

 if (reading4 == HIGH)  //Moves heads up and down
 
      //MOVES Servos
    myservoA.write(135); // servo motor Raises Head A
    delay(500); // wait 500ms for raising head
    myservoA.write(55); // servo motor Lowers Head A
    delay(500); // wait 500ms for lowering head
    myservoB.write(135); // servo motor Raises Head B
    delay(500); // wait 500ms for raising head
    myservoB.write(55); // servo motor Lowers Head B
    delay(500); //wait 500ms for lowering head
    myservoC.write(135); // servo motor Raises Head C
    delay(500); // wait 500ms for rotation
    myservoC.write(55); // servo motor Lowers Head C
    delay(500); //wait 500ms for lowering head
  
  for (int heads = 1; heads < 5;) {


    if (reading1 == HIGH)  // check to see if switch ONE has been pushed
      //Raise Head ONE
    myservoA.write(135); // servo motor rotates 150 degrees to Raise Head ONE
    delay(1000); // wait 1000ms for rotation
    myservoA.write(55); // servo motor Lowers Head A
    myservoB.write(55); // servo motor Lowers Head B
    myservoC.write(55); // servo motor Lowers Head C
    break;

    
    if (reading2 == HIGH)  // check to see if switch TWO has been pushed
      //Raise Head TWO
    myservoB.write(135); // servo motor rotates 150 degrees to Raise Head Two
    delay(1000); // wait 1000ms for rotation
    myservoA.write(55); // servo motor Lowers Head A
    myservoB.write(55); // servo motor Lowers Head B
    myservoC.write(55); // servo motor Lowers Head C
    break;

    if (reading3 == HIGH)  // check to see if switch THREE has been pushed
      //Raise Head Three

    myservoC.write(135); // servo motor rotates 150 degrees to Raise Head THREE
    delay(1000); // wait 1000ms for rotation
    myservoA.write(55); // servo motor Lowers Head A
    myservoB.write(55); // servo motor Lowers Head B
    myservoC.write(55); // servo motor Lowers Head C
    break;


    heads = heads + 1;

    //Program loops to top, waits for Button
  }
}

and also tried it with SWITCH CASES, but this does not work either. The individual raising and lowering with the readings work, and the loop works, but again the SWITCH CASES do not interrupt the loop, which was my desire.

  if (reading4 == HIGH) { //Moves heads up and down

    for (int heads = 1; heads < 5; heads++) {


      //MOVES Servos
      delay(500); // wait 500ms for raising head
      myservoA.write(135); // servo motor Raises Head A
      delay(500); // wait 500ms for raising head
      myservoA.write(55); // servo motor Lowers Head A
      delay(500); // wait 500ms for lowering head
      myservoB.write(135); // servo motor Raises Head B
      delay(500); // wait 500ms for raising head
      myservoB.write(55); // servo motor Lowers Head B
      delay(500); //wait 500ms for lowering head
      myservoC.write(135); // servo motor Raises Head C
      delay(500); // wait 500ms for rotation
      myservoC.write(55); // servo motor Lowers Head C
      delay(500); //wait 500ms for lowering head


      int game = 0;
      switch (game) {

        case 1:
          reading1 == HIGH; { // check to see if switch ONE has been pushed
            //Raise Head ONE
            myservoA.write(135); // servo motor rotates 150 degrees to Raise Head ONE
            delay(1000); // wait 1000ms for rotation
            myservoA.write(55); // servo motor Lowers Head A
            myservoB.write(55); // servo motor Lowers Head B
            myservoC.write(55); // servo motor Lowers Head C
            
            break;
          }

        case 2:
          reading2 == HIGH; { // check to see if switch TWO has been pushed
            //Raise Head TWO
            myservoB.write(135); // servo motor rotates 150 degrees to Raise Head Two
            delay(1000); // wait 1000ms for rotation
            myservoA.write(55); // servo motor Lowers Head A
            myservoB.write(55); // servo motor Lowers Head B
            myservoC.write(55); // servo motor Lowers Head C
           
            break;
          }

        case 3:
          reading3 == HIGH; { // check to see if switch THREE has been pushed
            //Raise Head Three
            myservoC.write(135); // servo motor rotates 150 degrees to Raise Head THREE
            delay(1000); // wait 1000ms for rotation
            myservoA.write(55); // servo motor Lowers Head A
            myservoB.write(55); // servo motor Lowers Head B
            myservoC.write(55); // servo motor Lowers Head C
            
            break;
          }

      }
     

      //Program loops to top, waits for Button
      }
    }

Thanks once again for your time and experience. It is truly appreciated.

Can you please post complete code?

    switch (game)
    {

      case 1:
        reading1 == HIGH;   // check to see if switch ONE has been pushed

The last line will not do anything; does it need to be part of an if statement?

Indepthdude:
However, I have now tried several variations of eliminating the DO-WHILE loop, and in each case it only executes the code one time after Button 4 is pushed. I want the heads to go up and down 5-15 seconds while the player decides on the choice they will make (reading 1-3).

I reckon you are still thinking in your original mindset. Don't use FOR and don't use delay() for timing.

Have a look at how millis() is used to manage timing without blocking in Several things at a time. Note how each function runs very briefly and returns to loop() so the next one can be called. And there may be dozens of calls to a function before it is actually time for it to do anything.

Also have a look at Planning and Implementing a Program

...R

If I'm reading correctly I think it's a timing issue the code can only do 1 line at anytime things happen fast so it appears multiple things happen but for the loop to break it has to be at that if statement right as you push the button.
I think the issue is The code paused at every delay so set a variable

CurrentTime =millis () then reference everything to current time and eliminate all delays so the code will constantly run and loop and be checking for a button press.

As it is now the code is going through each line and pausing at every delay when it gets to your if statement you probably have your finger off the button so you have to time it perfectly for it to break

I have tried to incorporate the "Blink without delay" example into my code to eliminate the delay statements.I tried to add the servoSweep code inside my ButtonX functions, so that there would be time for the servos to cycle.

Now, many iterations of code later... I am at the current version, and when I run the code, the following happens:

  1. Either all three heads rise and fall in little clock-cycle bursts (eh..eh..eh..eh..) or
  2. Only Head One (Button One) rises in little clock-cycle bursts and stays raised (no input from me). This happens 80% of the time when I reload the code.

Based on the the inching up and down, I think that my servoSweep code may not be able to execute in the state function as the TestButtonX code does? But I am not sure why not, if it does need to be outside of the TestButtonX functions.

I believe I understand what the servoSweep code is supposed to do, and how it works, but I am obviously not executing it. However, based on the fact that my heads rise or fall in little cycled increments, I believe my use of millis is off or incorrect, and I tried to accommodate the servos rising and falling by increasing my Button_X_Intervals to equate to the 1.5s to 3 s required to accommodate the servo moving. But I am still missing something...

I believe I understand the state concepts to change the various states so that they are either high/low or true/false. However, I am still missing something as my code is jittery, and is obviously not changing and holding the states.

Some of my lunch buddies from work who do C coding for our web products and they had me add the series of boolean statements in PushButtonFour, which seem redundant to me if my other TestButtonX cases worked.

I have already learned a lot about functions, serial debugging and the monitor, and now I am trying to master (understand) the use of millis and not using delay. For those of you that are good with the "Blink without delay" examples, I would genuinely appreciate any help realizing or pointing out my obvious mistakes in the code.

The relevant code is in a MS Word Doc Attachment.

I kept getting the error that the code is too long now.

"The message exceeds the maximum allowed length (9000 characters)."

Here is the code in a MS Word Doc. The last attachment did not take... as I tried to save it as a .docx

Here is the relevant code- Version 38.doc (243 KB)

I am not going to open a Word document.

Just post your program as a .ino file.

...R

Hi,
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

If your code is too big then attach it as an ino file.

Thanks.. Tom.. :slight_smile:

Thank you for your patience and help.

I appreciate your collective knowledge and willingness to try and help me improve the quality of the code I am trying to write.

I have already read the "how to post" but did not know you could attach the .ino file.

I will give it go. Please see the .ino attached :slight_smile:

WhackaMole_CJ_version38_FUNCTIONS___STATES.ino (15.6 KB)

That code looks like a bit of a dog's breakfast.

You seem to have a huge amount of unnecessary repetition. For example this appears 3 or 4 times

if (currentMillis - previousServoMillis >= servoInterval) {
        // its time for another move
        previousServoMillis += servoInterval;

        servoPosition = servoPosition + servoDegrees; // servoDegrees might be negative

        if (servoPosition <= servoMinDegrees) {
          // when the servo gets to its minimum position change the interval to change the speed
          if (servoInterval == servoSlowInterval) {
            servoInterval = servoFastInterval;
          }
          else {
            servoInterval = servoSlowInterval;
          }
        }
        if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees))  {
          // if the servo is at either extreme change the sign of the degrees to make it move the other way
          servoDegrees = - servoDegrees; // reverse direction
          // and update the position to ensure it is within range
          servoPosition = servoPosition + servoDegrees;
        }

Have a look at Planning and Implementing a Program - especially how the code is organized into small single-purpose functions.

Anyway, now that we can see your program you need to remind us what the problem is.

...R

His problem is the for loops, lets say he checks for button A to be pressed, he does this right at the start of his loop, then if the buttons are not pushed he goes into his

Move this
delay 500
move that
delay 500
move the other thing
delay 500

Etc

so he has to wait until all buttons are moved and then restarts the loop to check if buttons were pushed, in the current way it is coded he will never be able to interrupt the entire movement sequence because he only checks for buttos presses at the start of the loop and not during the moving.

dcosper:
His problem is the for loops, lets say he checks for button A to be pressed, he does this right at the start of his loop, then if the buttons are not pushed he goes into his

Move this
delay 500
move that
delay 500
move the other thing
delay 500

Etc

so he has to wait until all buttons are moved and then restarts the loop to check if buttons were pushed, in the current way it is coded he will never be able to interrupt the entire movement sequence because he only checks for buttos presses at the start of the loop and not during the moving.

I guess you missed the code attached in reply #11. Not a delay in sight :slight_smile:

Guess i did, good thing you caught that.

Hi,

You read the switches' pins in the setup section only. Should'nt you also read them in UpdateButtonStateX?

EDIT : Just saw your remark about testing.

Jacques

Trimmed a bit

#include<Servo.h> // include Servo library

int heads = 0;

int inPin1 = 1;
int inPin2 = 2;
int inPin3 = 4;
int inPin4 = 7;

int reading1;
int reading2;
int reading3;
int reading4;

int servoPosition = 90;
int servoSlowInterval = 80;
int servoFastInterval = 10;
int servoInterval = servoSlowInterval;
int servoDegrees = 2;

Servo myservoA;
Servo myservoB;
Servo myservoC;

void PushButtonFourLoop ();
void TestButtonOne();
void TestButtonTwo();
void TestButtonThree();

bool ButtonOnePressed = false;
bool ButtonTwoPressed = false;
bool ButtonThreePressed = false;

byte ButtonState1 = LOW;
byte ButtonState2 = LOW;
byte ButtonState3 = LOW;
byte ButtonState4 = LOW;

unsigned long currentMillis = 0;
unsigned long previousButtonOneMillis = 0;
unsigned long previousButtonTwoMillis = 0;
unsigned long previousButtonThreeMillis = 0;
unsigned long previousButtonFourMillis = 0;
unsigned long previousServoMillis = 0;

const int Button_One_Interval = 2500;
const int Button_Two_Interval = 2500;
const int Button_Three_Interval = 2500;
const int Button_Four_Interval = 3000;
const int buttonInterval = 50;
const int servoMinDegrees = 55;
const int servoMaxDegrees = 135;

void setup() {
   Serial.begin(9600);
   myservoA.attach(3);
   myservoB.attach(5);
   myservoC.attach(6);
   pinMode(inPin1, INPUT);
   pinMode(inPin2, INPUT);
   pinMode(inPin3, INPUT);
   pinMode(inPin4, INPUT);
   myservoA.write(55);
   myservoB.write(55);
   myservoC.write(55);
   reading1 = digitalRead(inPin1);
   reading2 = digitalRead(inPin2);
   reading3 = digitalRead(inPin3);
   reading4 = digitalRead(inPin4);
}

void loop() {
   currentMillis = millis();
   UpdateButtonState1 ();
   UpdateButtonState2 ();
   UpdateButtonState3 ();
   UpdateButtonState4 ();
   PushButtonFourLoop ();
   TestButtonOne ();
   TestButtonTwo ();
   TestButtonThree ();
}
void UpdateButtonState1 () {
   if (ButtonState1 == LOW) {
       if (currentMillis - previousButtonOneMillis >= Button_One_Interval) {
           ButtonState1 = HIGH;
           previousButtonOneMillis += Button_One_Interval;
    }
  }
   else {
       if (currentMillis - previousButtonOneMillis >= Button_One_Interval) {
           ButtonState1 = LOW;
           previousButtonOneMillis += buttonInterval;
    }
  }
}

void UpdateButtonState2 () {
   if (ButtonState2 == LOW) {
       if (currentMillis - previousButtonTwoMillis >= Button_Two_Interval) {
           ButtonState2 = HIGH;
           previousButtonTwoMillis += Button_Two_Interval;
    }
  }
   else {
       if (currentMillis - previousButtonTwoMillis >= Button_Two_Interval) {
           ButtonState2 = LOW;
           previousButtonTwoMillis += buttonInterval;
    }
  }
}

void UpdateButtonState3 () {
   if (ButtonState3 == LOW) {
       if (currentMillis - previousButtonThreeMillis >= Button_Three_Interval) {
           ButtonState3 = HIGH;
           previousButtonThreeMillis += Button_Three_Interval;
    }
  }
   else {
       if (currentMillis - previousButtonThreeMillis >= Button_Three_Interval) {
           ButtonState3 = LOW;
           previousButtonThreeMillis += buttonInterval;
    }
  }
}

void UpdateButtonState4 () {
   if (ButtonState4 == LOW) {
       if (currentMillis - previousButtonFourMillis >= Button_Four_Interval) {
           ButtonState4 = HIGH;
           previousButtonFourMillis += Button_Four_Interval;
    }
  }
   else {
       if (currentMillis - previousButtonFourMillis >= Button_Four_Interval) {
           ButtonState4 = LOW;
           previousButtonFourMillis += buttonInterval;
    }
  }
}

void TestButtonOne () {
   if (millis() - previousButtonOneMillis >= Button_One_Interval) {
       if (reading1 == HIGH) {
           Serial.println("The first button has been pressed");
           if (currentMillis - previousServoMillis >= servoInterval) {
               previousServoMillis += servoInterval;
               servoPosition = servoPosition + servoDegrees;
               if (servoPosition <= servoMinDegrees) {
                   if (servoInterval == servoSlowInterval) {
                       servoInterval = servoFastInterval;
          }
                   else {
                       servoInterval = servoSlowInterval;
          }
        }
               if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees))   {
                   servoDegrees = - servoDegrees;
                   servoPosition = servoPosition + servoDegrees;
        }
               myservoA.write(servoPosition);
      }
           previousButtonOneMillis += Button_One_Interval;
           ButtonState1 = ! ButtonState1;
    }
  }
   Serial.println("This is a function testing for a button push of Switch A");
}

void TestButtonTwo () {
   if (millis() - previousButtonTwoMillis >= Button_Two_Interval) {
       if (reading2 == HIGH) {
           Serial.println("The second button has been pressed");
           if (currentMillis - previousServoMillis >= servoInterval) {
               previousServoMillis += servoInterval;
               servoPosition = servoPosition + servoDegrees;
               if (servoPosition <= servoMinDegrees) {
                   if (servoInterval == servoSlowInterval) {
                       servoInterval = servoFastInterval;
          }
                   else {
                       servoInterval = servoSlowInterval;
          }
        }
               if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees))   {
                   servoDegrees = - servoDegrees;
                   servoPosition = servoPosition + servoDegrees;
        }
               myservoB.write(servoPosition);
      }
           previousButtonTwoMillis += Button_Two_Interval;
           ButtonState2 = ! ButtonState2;
    }
  }
   Serial.println("This is a function testing for a button push of Switch B");
}

void TestButtonThree () {
   if (millis() - previousButtonThreeMillis >= Button_Three_Interval) {
       if (reading2 == HIGH) {
           Serial.println("btn C pressed");
           if (currentMillis - previousServoMillis >= servoInterval) {
               previousServoMillis += servoInterval;
               servoPosition = servoPosition + servoDegrees;
               if (servoPosition <= servoMinDegrees) {
                   if (servoInterval == servoSlowInterval) {
                       servoInterval = servoFastInterval;
          }
                   else {
                       servoInterval = servoSlowInterval;
          }
        }
               if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees))   {
                   servoDegrees = - servoDegrees;
                   servoPosition = servoPosition + servoDegrees;
        }
               myservoC.write(servoPosition);
      }
           previousButtonThreeMillis += Button_Three_Interval;
           ButtonState3 = ! ButtonState3;
    }
  }
   Serial.println("btn C test");
}

void PushButtonFourLoop () {
   Serial.println("btn D test");
   if (currentMillis - previousButtonFourMillis >= Button_Four_Interval) {
       if (reading4 == HIGH) {
           Serial.println("btn D pressed");
           for (int i = 1; i < 3; i++) {
               if (currentMillis - previousServoMillis >= servoInterval) {
                   previousServoMillis += servoInterval;
                   servoPosition = servoPosition + servoDegrees;
                   if (servoPosition <= servoMinDegrees) {
                       if (servoInterval == servoSlowInterval) {
                           servoInterval = servoFastInterval;
            }
                       else {
                           servoInterval = servoSlowInterval;
            }
          }
                   if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees))   {
                       servoDegrees = - servoDegrees;
                       servoPosition = servoPosition + servoDegrees;
          }
                   myservoA.write(servoPosition);
                   myservoB.write(servoPosition);
                   myservoC.write(servoPosition);
        }
               TestButtonOne ();
               TestButtonTwo ();
               TestButtonThree ();
               if (ButtonOnePressed == true) {
                   ButtonOnePressed = false;
                   break;
        }
               if (ButtonTwoPressed == true) {
                   ButtonOnePressed = false;
                   break;
        }
               if (ButtonThreePressed == true) {
                   ButtonOnePressed = false;
                   break;
        }
      }
    }
       previousButtonFourMillis += Button_Four_Interval;
       ButtonState4 = ! ButtonState4;
  }
}

When we have more than one "anything", it is often a good choice to have them all sharing the same code.

As an example, see how we can manage any number of switches:

  byte switchPins[4] = {7, 8, 9, 10};
  byte switchStates[4] = {LOW, LOW, LOW, LOW};
  unsigned long switchChrono[4];
  int debounceTime = 50;

void debounce(byte ID, byte pin) {
  byte newState = digitalRead(pin);
  if (switchStates[ID] != newState)
    if(millis() - switchChrono[ID] >= debounceTime) {
      switchChrono[ID] = millis();
      switchStates[ID] = newState;
    }
}

bool pressed(byte ID) {
  debounce(ID, switchPins[ID]);
  return switchStates[ID] == HIGH;
}

void setup() {
  for (byte i; i < 4; i++) pinMode(switchPins[i], INPUT);
}

void loop() {
  if (pressed(0)) //do something
  if (pressed(1)) //do something
  if (pressed(2)) //do something
  if (pressed(3)) //do something
}

Jacques