Problem with while loop

Hi,

I'm having a problem with my while loop. I created a while loop for a countdown timer. During this timer I want my game ready to play. But the problem is, that my functions for this are executed once. But these functions have to check all the time when you can play the game. (To see if the leds/buttons are on or off).

void loop(){
  if(started == true){
    //start game
    digitalWrite(ledStart, LOW);
    
      input();
      checkState();
      timerStart();

  }

void input(){ 
  for (int i = 0; i<8; i++) {
          int reading = digitalRead(buttonPins[i]);
          if (reading != lastButtonStates [i]) {
            lastDebounceTime = millis();
          }
          if ((millis() - lastDebounceTime) > debounceDelay) {
            if (reading != buttonStates[i]) {
              buttonStates[i] = reading;
              if (buttonStates[i] == HIGH) {
                ledStates[i] = !ledStates[i];
                if(ledStates[i]==HIGH){
                  //you failed    
                  setAll_a(255,0,0,0);
                  setAll_b(255,0,0,0);
                  delay(100);
                  setAll_a(0,0,0,0);
                  setAll_b(0,0,0,0);
                  delay(100);
                  setAll_a(255,0,0,0);
                  setAll_b(255,0,0,0);
                  delay(100);
                  setAll_a(0,0,0,0);
                  setAll_b(0,0,0,0);
                  level = 0;
                  updateLevel();
                  checkState();
                }
              }
            }
          }
          digitalWrite(ledPins[i], ledStates[i]); 
          lastButtonStates[i] = reading;
  }
}

void checkState(){
  Serial.println("checking");
    int total = 0;
    for(int i=0; i<8; i++){
      total += ledStates[i];
    } 
    
    if(total==0){
      CylonBounce(0xf, 0xf, 0xf, 255, 2);
      ledMatrix(255, 0, 0, 0);
      level++;
      updateLevel();
    }
}

void timerStart(){
       count_nr = 10;
       split_count_nr();
       color_scheme = 1;

       do {
       delay(1000);                            // counting speed
          run_color_scheme();      
          translate_digits();                    // translate digit1 values to led's with color settings
          count_nr--;                              // increment digit1 --         
          split_count_nr();
          Stream_LEDdata(); 
       }

        while (count_nr > 0 );
           
        if (count_nr == 0){
          started = false;
        }
}

Can somebody tell me where it goes wrong? Thanks :slight_smile:

You can define a function within a function. You have define input() within the loop() function. Check the other functions, too.

econjack:
You can define a function within a function.

I presume you meant "cannot"

...R

ennef:
I'm having a problem with my while loop.

Don't use a WHILE or FOR loop unless it will complete within a few microseconds. For longer "loops" allow the loop() function to do the iteration.

Have a look at how millis() is used to manage timing without blocking in Several things at a time

...R

Robin2:
I presume you meant "cannot"

...R

Yep...it was late...

ennef:
void loop(){
if(started == true){
//start game
digitalWrite(ledStart, LOW);

input();
checkState();
timerStart();

}

does the loop() ends here? if so, i guess u are missing '}'