Counter not in sync

Hi Folks -

maybe somebody can help me with my counter. It seems that the counter i have isnt in sync with the seconds. For example if i set my countdown timer to 60seconds and i do start on a phone a countdown timer also to 60seconds when i hit the 00:00 mark on my counter on the phone i still have 7-10seconds left
what am I doing wrong ?

void countdown(){

          unsigned const intervalTime = 1000;
          unsigned long previousTime;
          unsigned long currentTime = millis();
          
           if(currentTime - previousTime >= intervalTime){
                    if (countdowntime > 0) countdowntime--;
                    else countdowntime = 0;
                    displayevent();
                    previousTime += intervalTime;
           }
          
}

void displayevent(){

                  //while(countdowntime > 0){
                      int seconds = numberOfSeconds(countdowntime);
                      int minutes = numberOfMinutes(countdowntime);
                      int lminutes = minutes / 10;
                      int rminutes = minutes % 10;
                      int lseconds = seconds / 10;
                      int rseconds = seconds % 10; 

                      if(countdowntime == 0){   
                        digitalWrite(latchpin, LOW);
                        shiftOut(datapin, clockpin, MSBFIRST, 63); // Display hundreds tens
                        shiftOut(datapin, clockpin, MSBFIRST, 63); // Display hundreds unit
                        shiftOut(datapin, clockpin, MSBFIRST, 63); // Display seconds tens
                        shiftOut(datapin, clockpin, MSBFIRST, 63); // Display seconds unit
                        digitalWrite(latchpin, HIGH);
                        blinking();
 
                      } else{
                     
                      digitalWrite(latchpin, LOW);
                      shiftOut(datapin, clockpin, MSBFIRST, segdisp[lminutes]); // Display hundreds tens
                      shiftOut(datapin, clockpin, MSBFIRST, segdisp[rminutes]); // Display hundreds unit
                      shiftOut(datapin, clockpin, MSBFIRST, segdisp[lseconds]); // Display seconds tens
                      shiftOut(datapin, clockpin, MSBFIRST, segdisp[rseconds]); // Display seconds unit
                      digitalWrite(latchpin, HIGH);
                      }

Hi,
Can you post your complete code please?

Thanks.. Tom... :slight_smile:

previousTime needs to either be a global or static inside the function. It's defined locally which means it's value is lost between invocations of countdown(). And probably initialized to zero or whenever you want the countdown timer to start

Tom I have a menu system and this one is the countdown section which i am having trouble with for now. The entire code isnt relevant to solve this bit of problem :slight_smile:

i am thinking about this static inside the function solution gcjr, may you want to code out your solution if its not much to ask.

previousTime needs to be global scope, and initialized. Currently its thrown away as countdown() exits and is a random junk value as its not initialized.

[ BTW the compiler warnings tell you this - enable full warnings and always check them ]

          static unsigned long previousTime;
                unsigned long currentTime = millis();

thanks, but its still counting much faster than normal.
initialized previousTime = 0; ive set it to global, static all that with no difference.