`#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)