Create timer using millis()

Hi, I have a program that includes a stopwatch and a timer. The stopwatch uses millis() to count, which makes it counting the exact time. But for the timer I can't think of a way to use millis(), so I have used a lot's of if's instead of millis(), but I can't get it counting in proper speed, I have tried calibrating it but it's hard and I can't use delays in the rest of the code.

So is there a way to program a timer using millis()?

You mean a countdown timer?

Simple. See if yo can follow this:

int countDown = 120;  // Countind down 2 minutes
unsigned long lastTick;

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

void loop(){
  
  unsigned long currentMillis = millis();
  
  if(currentMillis - lastTick >= 1000){
    countDown--;
    displayCountdownToSerial(); 
    lastTick += 1000;   
  }  
}

void displayCountdownToSerial(){
  
  int mins = countDown / 60;
  int secs = countDown % 60;
  
  Serial.print(" Time Remain = ");
  Serial.print(mins);
  Serial.print(" : ");
  Serial.println(secs);
}

Delta_G

in your code is it somehow possible to reset your Counter after it is finished ?

is there a way to program a timer using millis()?

Please explain what you mean by “a timer” and what starts it

PALAVRAS:
Delta_G
in your code is it somehow possible to reset your Counter after it is finished ?

At any time you can set 'countDown' to a new value and set 'lastTick' to millis() to start a new countdown.
You will probably want to do something to stop the counter when it hits zero. Right now it goes into negative seconds. That is as easy as changing:

    countDown--;

to

  if (countDown > 0)
    countDown--;