Using a Button to Start a While Loop

Hello! My problem is that I thought I had an understanding of how I could use an input from a button to jump back into a While Loop, but some reason I can't seem to get it right.

int pinCount = 12;
int timer = 300;
int pinArray[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int pinArrayInverse[] = {
  9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2};
int arduinoActivate = 11;
int sendTo2 = 0;
int whatDo = 0;
int arcDelay = 75;
int randomVar = 0;
int buttonPin = 14;
int buttonState = 0;
int powerTest = 0;
  
  void setup() {
    for (int cycle = 0; cycle < pinCount - 4; cycle++) {
      pinMode(pinArray[cycle], OUTPUT);
    }
    pinMode(buttonPin, INPUT); 
  } 
  
  void loop() {
  
    if (powerTest == 0) {
    buttonState = digitalRead(buttonPin);  
  
  while (buttonState == LOW) {
    buttonState = digitalRead(buttonPin);
    }
  }
  
    powerTest = 1;
    whatDo = random(1, 5);
    
  
  if (whatDo == 1); {
    randomVar = 4;
    for (int cycle = 0; cycle < randomVar; cycle++) {
    for (int cycle = 0; cycle < (pinCount); cycle++) {
      digitalWrite(pinArray[(cycle - 4)], LOW);
      digitalWrite(pinArray[(cycle - 0)], HIGH);
      buttonState = digitalRead(buttonPin);
      
      delay(arcDelay);
      buttonState = digitalRead(buttonPin);
   } 
  }
 }

 
  if (whatDo == 2); {
    randomVar = 4;
    for (int cycle = 0; cycle < randomVar; cycle++) {
    for (int cycle = 0; cycle < (pinCount); cycle++) {
      digitalWrite(pinArrayInverse[(cycle - 4)], LOW);
      digitalWrite(pinArrayInverse[(cycle - 0)], HIGH);
     
      delay(arcDelay);
      }
    }
  }
}

This is going to be the beginning of a program I am going to expand, and it's all going to be the same basic principles just in different ways. So once I understand what I am doing wrong here, everything should hopefully run smoothly. This program activates 8 LED and sends a 4 LED bar across them in a loop four times, and then does it backwards. Each one of the whatDo if statements is supposed to be 1 out of however many animations I decide to make.

My problem though is that I can't find a way to jump back to the While Loop in the beginning that doesn't exit until the input on pin 14 (A0) becomes HIGH. When I run the program it works fine, nothing happens until I send a 5v signal to pin 14 (A0). Then the main loop continues and will ignore the While Loop as long as int powerTest does not equal 0. I thought I could put

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
powerTest = 0;
}

on different lines of the program to change powerTest into 0 so that the if statement at the beginning of the main loop would activate the while loop, but instead the program turns on from the input like normally, and then turns off after the second animation without any input from me.

Help?

Thank you for your time!

for (int cycle = 0; cycle < pinCount - 4; cycle++)

What about the other pins?

if (whatDo == 1);

Is the semicolon intentional?

I don't want those pins to activate. The only reason that the other pins are in the array, is so that when the for loop runs it will make the 4 LED bar completely vanish off of the 8 bar when it reaches the other side with one LED at a time.

Does that make sense?

What about the semicolons?

Actually, no they are not. Is that what's causing the problem?

for (int cycle = 0; cycle < randomVar; cycle++) {
    for (int cycle = 0; cycle < (pinCount); cycle++) {
      digitalWrite(pinArray[(cycle - 4)], LOW);

Which "cycle" do you intend to use inside these nested loops?
Which one does the compiler use?

Is that what's causing the problem?

They're causing a problem.
Whether or not they're the cause of the problem I couldn't say.

I'm sorry, I don't know what the compiler is.

The first for loop is deciding how many times bar will go in that certain direction and the second for loop cause it to go in the direction.

Don't you think the use of two variables with the same name is confusing?

I didn't realize my mistake there. Thank you for pointing that out. This is my first project.

OK, so fix the problems discovered so far, and if the code still isn't doing what you want,use the auto format tool in the IDE and repost the code.

Okay, everything that you pointed out should be fixed now.

int pinCount = 12;
int timer = 300;
int pinArray[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int pinArrayInverse[] = {
  9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2};
int arduinoActivate = 11;
int sendTo2 = 0;
int whatDo = 0;
int arcDelay = 75;
int randomVar = 0;
int buttonPin = 14;
int buttonState = 0;
int powerTest = 0;

void setup() {
  for (int cycle = 0; cycle < pinCount - 4; cycle++) {
    pinMode(pinArray[cycle], OUTPUT);
  }
  pinMode(buttonPin, INPUT); 
} 

void loop() {

  if (powerTest == 0) {
    buttonState = digitalRead(buttonPin);  

    while (buttonState == LOW) {
      buttonState = digitalRead(buttonPin);
    }
  }

  powerTest = 1;
  whatDo = random(6);


  if (whatDo == 0) {
    randomVar = 4;
    for (int cycleTwo = 0; cycleTwo < randomVar; cycleTwo++) {
      for (int cycle = 0; cycle < (pinCount); cycle++) {
        digitalWrite(pinArray[(cycle - 4)], LOW);
        digitalWrite(pinArray[(cycle - 0)], HIGH);
        buttonState = digitalRead(buttonPin);

        delay(arcDelay);
        buttonState = digitalRead(buttonPin);
      } 
    }
  }

  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    powerTest = 0;
  }


  if (whatDo == 1) {
    randomVar = 4;
    for (int cycleTwo = 0; cycleTwo < randomVar; cycleTwo++) {
      for (int cycle = 0; cycle < (pinCount); cycle++) {
        digitalWrite(pinArrayInverse[(cycle - 4)], LOW);
        digitalWrite(pinArrayInverse[(cycle - 0)], HIGH);

        delay(arcDelay);
      }
    }
  }
}

I've discovered the issue is with this section of code:

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
powerTest = 0;
}

When it's not there, the program runs without stopping as it should, but what I thought this would do is check whether or not there's an input from pin 14, and if there is then powerTest = 0, which would cause the if statement at the very top to be true and then lock the program back into the while loop until pin 14 was given an input again.

But when I add the line of code, it ignores the if statement of (buttonState == HIGH) and will make powerTest whatever the "then" portion of the if statement is whether or not I'm giving pin 14 an input.

Thanks for your help! I fixed the problem by using another button to turn it off rather than the same one to turn it on. It must have been an issue with how fast it was detecting whether or not the button was pressed. I probably could have fixed it with a delay, but this is better anyway. Again though, thanks for your help!