Millis timer with start and pause button

Hi.

I have a stopwatch made with the Millis function and I want to add a start button and a pause button.

I would like you to help me with this problem, look everywhere for something that explains how to start the Millis chronometer when I want but I did not find
I leave here the code and I would appreciate if you can guide me with this

unsigned long previous;
int sec;
int min;
int hours;
void setup()
{
Serial.begin (9600);
}

void loop ()
{
 if (millis() >= (previous)) {
     previous = previous + 1000;
 sec++;
     if (sec== 60)
     {
        sec= 0;
        min++;
     }
     if (min== 60)
     {
        min= 0;
        hours++;
     }
     if (hours== 13)
     {
        hours= 1;
     }
  Serial.print (hours, DEC);
  Serial.print (":");
  Serial.print (min,DEC);
  Serial.print (":");
  Serial.println(sec,DEC);
  } 
}

The function millis() starts when the power gets turned on. You don't have to do anything. When you want to use it, simply find out what time it is by setting a long unsigned integer to it's current value and then calculation what the time will be later, when you want some event to occur, much the same as you would use the watch on your wrist.

DKWatson:
The function millis() starts when the power gets turned on. You don't have to do anything. When you want to use it, simply find out what time it is by setting a long unsigned integer to it's current value and then calculation what the time will be later, when you want some event to occur, much the same as you would use the watch on your wrist.

I still can not add a start button and a pause button :frowning: I need more help

You actually need more help than just the start and pause buttons, but one step at a time.

Without writing code for you, try thinking about it. If you had a clock on the wall that you couldn't touch/start/stop, how would you go about it? When you started you would make a note of the time and then normalize it, probably to zero (start_time = now, which is supplied by millis(), and normalized_time = now - start_time). Every time interval after that would then be in terms of elapsed time from the start, which translates to current_time - start_time.

By constantly updating current_time we can monitor elapsed time by checking current_time - start_time as being equal to or exceeding (>=) some preset interval. If this is true, execute some event. These tests can be one-time or scheduled. The start_time can be reset, perhaps at the start of a pause and then again at the end of the pause.

The physical buttons themselves would likely be wired to input_pullups with interrupts triggering on a falling signal.

So take all that techno-babble and wrap some code around it and let's see what you come up with.

You could just let millis() run just stop your counter from counting and shift the precision to milliseconds instead of seconds.

unsigned long previous;
int Millisecond;
int sec;
int min;
int hours;

int Go = 1 // Set to 0 (ZERO) to stop
         void setup()
{
  Serial.begin (9600);
}

void loop ()
{
  if (millis() >= (previous)) {
    previous = previous + 1;
    Millisecond += Go;
    if (Millisecond == 1000) {
      Millisecond = 0;
      sec++;
      if (sec == 60)
      {
        sec = 0;
        min++;
      }
      if (min == 60)
      {
        min = 0;
        hours++;
      }
      if (hours == 13)
      {
        hours = 1;
      }
      Serial.print (hours, DEC);
      Serial.print (":");
      Serial.print (min, DEC);
      Serial.print (":");
      Serial.println(sec, DEC);
    }
  }
}

Z

  if (millis() >= (previous)) {

previous = previous + 1;
    Millisecond += Go;
    if (Millisecond == 1000) {
    Millisecond = 0;

I see two problems in this snippet...

  1. The addition of time. This will stop working in 49 days due to the rollover. Then it will start working again after another 49 days.

  2. It assumes the millisecond clock always advances by 1 without skipping. If there's other code which takes more than a millisecond to run, that will obviously fail. Less-obviously, the Arduino millis() clock isn't guaranteed to always increment by 1. There are occasions where it will skip a number.

I would replace this by...

  unsigned long now = millis();
  if(Go) {
    Millisecond += now - previous;
    previous = now;
  }
  if(Millisecond >= 1000) {
    Millisecond -= 1000;