How to properly count time - Uno R4 Wifi

`#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define BTN_RST 2
#define GATE 4
#define PP 7
#define LP 8

#define P_LED 12
#define L_LED 13

int mm = 0, ss = 0, ms = 0;
bool runTimer = false; // 1 = run, 0 = wait
int lmm, lss, lms;
int pmm, pss, pms;

void setup()
{
  // iputs
  pinMode(GATE, INPUT);
  pinMode(PP, INPUT_PULLUP);
  pinMode(LP, INPUT_PULLUP);
  pinMode(BTN_RST, INPUT);

  // outputs
  pinMode(P_LED, OUTPUT);
  pinMode(L_LED, OUTPUT);

	// initialize the LCD
	lcd.begin();

	// Turn on the blacklight and print a message.
	lcd.backlight();
	lcd.setCursor(4,0);
  lcd.print("GOOD LUCK");
  delay(5000);
  lcd.clear();
}

void loop()
{
  if(digitalRead(GATE) == 1)
  {
  lcd.setCursor(6,0);
  lcd.print("Ready");
  }
  else
  {
    lcd.clear();
    stopwatch();
  }
}

void stopwatch()
{
  runTimer = true;

  while(runTimer == true)
  {
    ms++;
    if(ms>27)
    {
      ms = 0;
      ss++;
    }
    
    if(ss>59)
    {
      ss = 0; 
      mm++;
    }

    if(mm>59)
    {
      runTimer = false;
      mm = 0, ss = 0, ms = 0;
      digitalWrite(P_LED, LOW);
      digitalWrite(L_LED, LOW);
      lcd.clear();
    }

    if(digitalRead(LP) == LOW)
    {
      digitalWrite(L_LED, HIGH);
    }

    if(digitalRead(LP) == HIGH)
    {
      lmm = mm;
      lss = ss;
      lms = ms;
    }

    if(digitalRead(PP) == LOW)
    {
      digitalWrite(P_LED, HIGH);
    }

    if(digitalRead(PP) == HIGH)
    {
      pmm = mm;
      pss = ss;
      pms = ms;
    }

    if(digitalRead(BTN_RST) == HIGH)
    {
      runTimer = false;
      mm = 0, ss = 0, ms = 0;
      digitalWrite(P_LED, LOW);
      digitalWrite(L_LED, LOW);
      lcd.clear();
    }

    
      lcd.setCursor(0,0);
      lcd.print("P ");
      lcd.print((pmm/10)%10);
      lcd.print(pmm%10);
      lcd.print(":");
      lcd.print((pss/10)%10);
      lcd.print(pss%10);
      lcd.print(":");
      lcd.print((pms/100)%10);
      lcd.print((pms/10)%10);
      lcd.print(pms%10);

      lcd.setCursor(0,1);
      lcd.print("L ");
      lcd.print((lmm/10)%10);
      lcd.print(lmm%10);
      lcd.print(":");
      lcd.print((lss/10)%10);
      lcd.print(lss%10);
      lcd.print(":");
      lcd.print((lms/100)%10);
      lcd.print((lms/10)%10);
      lcd.print(lms%10);
  }
}
`

The code will run one stopwatch and saving 2 diferent times. My problem is with counting milliseconds. (I know that 1s != 27ms, it was used only for approxymately value) How can i create stopwatch without using Timer_Compa_vect, please? Ms are very important in may project.

(pms,... stands for right time; lms,... stands for left time)

how can i implement it, please?

look this over

  0:03.863
  0:03.875
  0:03.888
  0:03.900
  0:03.913
  0:03.926
  0:03.938
  0:03.950
  0:03.962
  0:03.975
  0:03.988
  0:04.000

void loop (void)
{
    unsigned long msec = millis ();

    int secs  = msec / 1000;
    int mins  = secs / 60;
        secs %= 60;
        msec %= 1000;

    char s [20];
    sprintf (s, " %2d:%02d.%03lu", mins, secs, msec);
    Serial.println (s);
}

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

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