How Do I clean this Loop

I'm trying to get this countdown timer to stop, after which I will be attempting to combine this code with a scoreboard.

I thought maybe I could move the Loop into the setup and then the countdown would end, after its done, and the Loop would be free for the scoreboard section thinking that would help keep the complications from combining the two to a minimum.

Is it possible for this counter to run in the setup?
And if so; How would I insert it?
Also, can I get the LED in pin 13 to light up (and stay lit) at the end of the counter?

#include <TM1637Display.h>

#define numberofseconds(time) ((time / 1000) % 60)
#define numberofminutes(time) (((time / 1000) /60) % 60)

#define gameled 13

const uint8_t OFF[] = {0, 0, 0, 0};
// in the libary, the byte order is .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01110111, B01101110};
//clock, data
TM1637Display display(7, 6);

// 1000ms in one sec, 1000x60x60 = 3600000ms = 1hour 300000 = 5min
unsigned long timeLimit = 60000;

void setup() {
Serial.begin(9600);

display.setBrightness(0x0d);

display.setSegments(OFF);

pinMode(gameled, OUTPUT);

}

void countdown() {

unsigned long timeRemaining = timeLimit - millis();

while (timeRemaining > 0) {
int seconds = numberofseconds(timeRemaining);
int minutes = numberofminutes(timeRemaining);

display.showNumberDecEx(seconds, 0, true, 2, 2);

display.showNumberDecEx(minutes, 0x80>>3, true, 2, 0);

timeRemaining = timeLimit - millis();
}
}

void displayText() {
display.setSegments(PLAY);
delay(2000);
}
void loop() {
displayText();
countdown();
digitalWrite(gameled, HIGH);

}

I'm using a TM1637 module for the display.

Try this.

#include <TM1637Display.h>

#define numberofseconds(_time_) ((_time_ / 1000) % 60)
#define numberofminutes(_time_) (((_time_ / 1000) /60) % 60)

#define gameled 13

const uint8_t OFF[] = {0, 0, 0, 0};
// in the libary, the byte order is .GFEDCBA 
//                       .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01110111, B01101110};

 
//clock, data
TM1637Display display(7, 6);

// 1000ms in one sec, 1000x60x60 = 3600000ms = 1hour 300000 = 5min
const unsigned long timeLimit = 60000;
unsigned long timeStart;
bool bcountdownDone;

void setup() 
{
    Serial.begin(9600);
  
    display.setBrightness(0x0d);

    display.setSegments(OFF);

    pinMode(gameled, OUTPUT);

    timeStart = millis();
    bcountdownDone = false;

}//setup

void countdown() 
{
    int
        seconds,
        minutes;
    static int
        lastseconds = -1;
    unsigned long
        timeRemaining,
        timeElapsed;

    timeElapsed = millis() - timeStart;
    timeRemaining = timeLimit - timeElapsed;
    seconds = numberofseconds(timeRemaining);
    minutes = numberofminutes(timeRemaining);
    if( seconds != lastseconds )
    {
        lastseconds = seconds;
        display.showNumberDecEx(seconds, 0, true, 2, 2);
        display.showNumberDecEx(minutes, 0x80>>3, true, 2, 0);
        
    }//if

    if( seconds == 0 && minutes == 0 )
    {
        digitalWrite( gameled, HIGH );
        bcountdownDone = true;                
        
    }//if
    
}//countdown

void displayText() 
{
    display.setSegments(PLAY);
    delay(2000);    //?
    
}//displayText

void loop() 
{
    if( !bcountdownDone )
        countdown();
    else
        displayText();
    
}//loop

Blackfin:
Try this.

Thank you SO MUCH!!!! that worked Perfectly! you're a life saver!!!

At the end it lights up the LED and reverts back to "play" or the const unit8_t PLAY[] (displaying Play)

Is there a way to have it end displaying or flashing all 0's?

How about:

#include <TM1637Display.h>

#define numberofseconds(_time_) ((_time_ / 1000) % 60)
#define numberofminutes(_time_) (((_time_ / 1000) /60) % 60)

#define gameled 13

const uint8_t ZEROS[] = {0b00111111, 0b00111111, 0b00111111, 0b00111111 }; 
const uint8_t OFF[] = {0, 0, 0, 0};
// in the libary, the byte order is .GFEDCBA 
//                       .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01110111, B01101110};

 
//clock, data
TM1637Display display(7, 6);

// 1000ms in one sec, 1000x60x60 = 3600000ms = 1hour 300000 = 5min
const unsigned long timeLimit = 60000;
unsigned long timeStart;
bool bcountdownDone;

void setup() 
{
    Serial.begin(9600);
  
    display.setBrightness(0x0d);

    display.setSegments(OFF);

    pinMode(gameled, OUTPUT);

    timeStart = millis();

}//setup

void countdown() 
{
    static bool
        bcountdownDone = false;
    int
        seconds,
        minutes;
    static int
        lastseconds = -1;
    unsigned long
        timeRemaining,
        timeElapsed;

    if( bcountdownDone )
        FlashZeros();
    else
    {
        timeElapsed = millis() - timeStart;
        timeRemaining = timeLimit - timeElapsed;
        seconds = numberofseconds(timeRemaining);
        minutes = numberofminutes(timeRemaining);
        if( seconds != lastseconds )
        {
            lastseconds = seconds;
            display.showNumberDecEx(seconds, 0, true, 2, 2);
            display.showNumberDecEx(minutes, 0x80>>3, true, 2, 0);
            
        }//if
    
        if( seconds == 0 && minutes == 0 )
        {
            digitalWrite( gameled, HIGH );
            bcountdownDone = true;                
            
        }//if
    
    }//else
    
}//countdown

void displayText() 
{
    display.setSegments(PLAY);
    delay(2000);    //?
    
}//displayText

void loop() 
{
    countdown();
    //displayText();
    
}//loop

void FlashZeros( void )
{
    static bool
        bState = true;      //reflect that countdown left digits at 0000 to begin
    static unsigned long
        timeFlash = 0;

    if( millis() - timeFlash > 300 )
    {
        timeFlash = millis();
        if( bState )
        {
            display.setSegments(OFF);
            bState = false;
            
        }//if
        else
        {
            display.setSegments(ZEROS);
            bState = true;
            
        }//else
        
    }//if
    
}//FlashZeros

Blackfin:
How about:

#include <TM1637Display.h>

#define numberofseconds(time) ((time / 1000) % 60)
#define numberofminutes(time) (((time / 1000) /60) % 60)

#define gameled 13

const uint8_t ZEROS[] = {0b00111111, 0b00111111, 0b00111111, 0b00111111 };
const uint8_t OFF[] = {0, 0, 0, 0};
// in the libary, the byte order is .GFEDCBA
//                       .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01110111, B01101110};

//clock, data
TM1637Display display(7, 6);

// 1000ms in one sec, 1000x60x60 = 3600000ms = 1hour 300000 = 5min
const unsigned long timeLimit = 60000;
unsigned long timeStart;
bool bcountdownDone;

void setup()
{
   Serial.begin(9600);
 
   display.setBrightness(0x0d);

display.setSegments(OFF);

pinMode(gameled, OUTPUT);

timeStart = millis();

}//setup

void countdown()
{
   static bool
       bcountdownDone = false;
   int
       seconds,
       minutes;
   static int
       lastseconds = -1;
   unsigned long
       timeRemaining,
       timeElapsed;

if( bcountdownDone )
       FlashZeros();
   else
   {
       timeElapsed = millis() - timeStart;
       timeRemaining = timeLimit - timeElapsed;
       seconds = numberofseconds(timeRemaining);
       minutes = numberofminutes(timeRemaining);
       if( seconds != lastseconds )
       {
           lastseconds = seconds;
           display.showNumberDecEx(seconds, 0, true, 2, 2);
           display.showNumberDecEx(minutes, 0x80>>3, true, 2, 0);
           
       }//if
   
       if( seconds == 0 && minutes == 0 )
       {
           digitalWrite( gameled, HIGH );
           bcountdownDone = true;                
           
       }//if
   
   }//else
   
}//countdown

void displayText()
{
   display.setSegments(PLAY);
   delay(2000);    //?
   
}//displayText

void loop()
{
   countdown();
   //displayText();
   
}//loop

void FlashZeros( void )
{
   static bool
       bState = true;      //reflect that countdown left digits at 0000 to begin
   static unsigned long
       timeFlash = 0;

if( millis() - timeFlash > 300 )
   {
       timeFlash = millis();
       if( bState )
       {
           display.setSegments(OFF);
           bState = false;
           
       }//if
       else
       {
           display.setSegments(ZEROS);
           bState = true;
           
       }//else
       
   }//if
   
}//FlashZeros

That works well! Thank you, you don't know how much this helps!!
Only issue is that It no longer displays "play" at the beginning. Only once when you first upload it, then on reset it just restarts the clock. But honestly Im not overly worried about that. I just thought it was fun to have Play at the beginning but its not then end of the world.

BubbleHockey:
That works well! Thank you, you don't know how much this helps!!
Only issue is that It no longer displays "play" at the beginning. Only once when you first upload it, then on reset it just restarts the clock. But honestly Im not overly worried about that. I just thought it was fun to have Play at the beginning but its not then end of the world.

It's easy to do: How long do you want it to show Play? 2-seconds? And then start the countdown and then flash indefinitely?

#include <TM1637Display.h>

#define numberofseconds(_time_) ((_time_ / 1000) % 60)
#define numberofminutes(_time_) (((_time_ / 1000) /60) % 60)

#define gameled 13

const uint8_t ZEROS[] = {0b00111111, 0b00111111, 0b00111111, 0b00111111 }; 
const uint8_t OFF[] = {0, 0, 0, 0};
// in the libary, the byte order is .GFEDCBA 
//                       .GFEDCBA
const uint8_t PLAY[] = {B01110011, B00111000, B01110111, B01101110};

 
//clock, data
TM1637Display display(7, 6);

// 1000ms in one sec, 1000x60x60 = 3600000ms = 1hour 300000 = 5min
const unsigned long timeLimit = 60000;
unsigned long timeStart;
bool bcountdownDone;

void setup() 
{
    Serial.begin(9600);
  
    display.setBrightness(0x0d);

    display.setSegments(OFF);

    pinMode(gameled, OUTPUT);
    
    display.setSegments(PLAY);
    timeStart = millis();           //used to time display of PLAY

}//setup


#define ST_SHOWPLAY     0
#define ST_COUNT        1
#define ST_FLASH_ZEROS  2
void countdown() 
{
    static byte
        stateCountdown = ST_SHOWPLAY;

    //the vars used by countdown state
    static bool
        bcountdownDone = false;
    int
        seconds,
        minutes;
    static int
        lastseconds = -1;
    unsigned long
        timeRemaining,
        timeElapsed;
    
    switch( stateCountdown )
    {
        case    ST_SHOWPLAY:
            //PLAY is showing from setup...time to start countdown?
            if( (millis() - timeStart) > 2000 )
            {
                stateCountdown = ST_COUNT;
                timeStart = millis();
                
            }//if
            
        break;

        case    ST_COUNT:
            timeElapsed = millis() - timeStart;
            timeRemaining = timeLimit - timeElapsed;
            seconds = numberofseconds(timeRemaining);
            minutes = numberofminutes(timeRemaining);
            if( seconds != lastseconds )
            {
                lastseconds = seconds;
                display.showNumberDecEx(seconds, 0, true, 2, 2);
                display.showNumberDecEx(minutes, 0x80>>3, true, 2, 0);
                
            }//if
        
            if( seconds == 0 && minutes == 0 )
            {
                digitalWrite( gameled, HIGH );
                stateCountdown = ST_FLASH_ZEROS;                
                
            }//if
        
        break;

        case    ST_FLASH_ZEROS:
            FlashZeros();
                
        break;
        
    }//switch

}//countdown

void loop() 
{
    countdown();
    
}//loop

void FlashZeros( void )
{
    static bool
        bState = true;      //reflect that countdown left digits at 0000 to begin
    static unsigned long
        timeFlash = 0;

    if( millis() - timeFlash > 300 )
    {
        timeFlash = millis();
        if( bState )
        {
            display.setSegments(OFF);
            bState = false;
            
        }//if
        else
        {
            display.setSegments(ZEROS);
            bState = true;
            
        }//else
        
    }//if
    
}//FlashZeros

Blackfin:
It's easy to do: How long do you want it to show Play? 2-seconds? And then start the countdown and then flash indefinitely?

THANK YOU!!!!!!!!!
that is PERFECT!!! I can not thank you enough!!