Stopwatch activation through digital output

Hello everyone

I'm developing a start system consisting of LEDs and a stopwatch. The operation is as follows: 5 LEDs light each one 1s after the other, then all go off simultaneously and another LED lights up (equal to a countdown) and then a stopwatch on an LCD display is activated.
My difficulty is to make the timer be activated only after 5 seconds of counting (when the last led is lit), it is already activated when I turn on the arduino. Here's the code:

#include <U8g2lib.h>
#include <U8x8lib.h>

U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, 6, 5, 4, 7);

// global for display
int min_val, sec_val, milli_val;

unsigned long currentRunStartMillis; // tempo de corrida
unsigned long savedMillis;

void setup(void) {
  //Define a porta do led como saida
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT);
  pinMode(35, OUTPUT);

  //Apaga os leds
  digitalWrite(30, LOW);
  digitalWrite(31, LOW);
  digitalWrite(32, LOW);
  digitalWrite(33, LOW);
  digitalWrite(34, LOW);  
  digitalWrite(35, LOW);

  //Aguarda o intervalo especificado
  delay(1000);
   
  //Acende o led
  digitalWrite(30, HIGH);
   
  //Aguarda o intervalo especificado
  delay(1000);

  //Acende o led
  digitalWrite(31, HIGH);

  //Aguarda o intervalo especificado
  delay(1000);

  //Acende o led
  digitalWrite(32, HIGH);

  //Aguarda o intervalo especificado
  delay(1000);

  //Acende o led
  digitalWrite(33, HIGH);

  //Aguarda o intervalo especificado
  delay(1000);

  //Acende o led
  digitalWrite(34, HIGH);
  
  //Aguarda o intervalo especificado
  delay(1000);

  //Apaga os 5 leds e liga o 6º led
  digitalWrite(30, LOW);
  digitalWrite(31, LOW);
  digitalWrite(32, LOW);
  digitalWrite(33, LOW);
  digitalWrite(34, LOW);
  digitalWrite(35, HIGH);

void loop(void) {
calcResultFromMillis(savedMillis - currentRunStartMillis - 6000, &min_val, &sec_val, &milli_val);
  
    
  
  // CURRENT RUN
  u8g2.setCursor(40, 10);
  if (min_val < 10)
  {
    u8g2.print("0");
  }
  u8g2.print(min_val);
  u8g2.print(":");
  if (sec_val < 10)
  {
    u8g2.print("0");
  }
  u8g2.print(sec_val);
  u8g2.print(".");
  u8g2.print(milli_val);
 
  u8g2.sendBuffer();
}

// calculate millis into 2 values, seconeds and millis for display
void calcResultFromMillis(unsigned long value, int *min_val, int *sec_val, int *milli_val) {
  *min_val = int(value / 60000);
  *sec_val = int(value / 1000) - (*min_val * 60);
  *milli_val = value - ( int(value/1000) * 1000);
  
}
calcResultFromMillis(savedMillis - currentRunStartMillis - 6000, &min_val, &sec_val, &milli_val);

I used this device (subtracts 6000 ms) to "force" the timer to start only after the countdown, but this is not the ideal way, as I intend to add more features before the countdown, ideally it would only trigger when the digital output ( pinMode 35) is HIGH.

If anyone can help, I appreciate it.

Why not take a timestamp just as setup() returns?

I have no idea how to implement a timestamp.

Or similar, as the very last thing setup() does before it returns.

Thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.