Continuing after While Loop?

Hi, This is my first post, thank you for any help in advance.

I have written the following code. The idea is that it waits for a button press. Once it sees it, it starts to flicker the board pins using the while loop. It does a check of millis to see how much time has gone by and after 30 seconds it should exit the while loop and then turn on an LED. Once the button is released, it should go back to the beginning.

My issue is that after the while loop, it gets stuck and doesn't continue on to check for a button press.

Do you notice my mistake?

const int buttonPin = 13;
const int buttonLEDPin = 12;
const int board1Pin = 11;
const int board2Pin = 10;
const int board3Pin = 9;
const int board4Pin = 8;
const int resetLEDPin = 7;
unsigned long settime;
unsigned long currtime;
unsigned long diff;

void setup() {
  pinMode(buttonPin, INPUT);             //Set button input
  pinMode(buttonLEDPin, OUTPUT);         //Set button LED output
  pinMode(resetLEDPin, OUTPUT);          //Set reset LED output
  pinMode(board1Pin, OUTPUT);            //Set board1 output
  pinMode(board2Pin, OUTPUT);            //Set board2 output
  pinMode(board3Pin, OUTPUT);            //Set board3 output
  pinMode(board4Pin, OUTPUT);            //Set board4 output
}

void loop() {
  int buttonstate;
  int timecheck;
  timecheck = 0;
  buttonstate = digitalRead(buttonPin);
  if (buttonstate == 0){
    digitalWrite(buttonLEDPin, HIGH);
    settime = millis();
  }
  else {
    digitalWrite(buttonLEDPin, LOW);
  }
  while (buttonstate == 0 && timecheck == 0) {
    currtime = millis();
    diff = currtime - settime;     //find time since last event
    if (diff > 30000) {            //time limit for treatment set to 30 seconds
      timecheck = 1;
    }
    digitalWrite(board1Pin, HIGH);
    digitalWrite(board2Pin, HIGH);
    digitalWrite(board3Pin, HIGH);
    digitalWrite(board4Pin, HIGH);
    delay(70);
    digitalWrite(board1Pin, LOW);
    digitalWrite(board2Pin, LOW);
    digitalWrite(board3Pin, LOW);
    digitalWrite(board4Pin, LOW);
    delay(50);
    buttonstate = digitalRead(buttonPin);
  }
  while (buttonstate == 0) {
    digitalWrite(resetLEDPin, HIGH);
  }
  digitalWrite(resetLEDPin, LOW);
  digitalWrite(buttonLEDPin, LOW);
}
  while (buttonstate == 0) {
    digitalWrite(resetLEDPin, HIGH);
  }

Once buttonState is zero and this while loop is entered how will buttonState ever change ?

I think you are using this:

while (buttonstate == 0) {

  • digitalWrite(resetLEDPin, HIGH);*
  • }*

to show that the timer has now been reset.. but this would lead to an infinite loop.
Try:

if(buttonstate == 0)
{

  • digitalWrite(resetLEDPin, HIGH);*
  • delay(100);*
  • digitalWrite(resetLEDPin, LOW);*
    }