#define START_P 2
#define STOP_P 3
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
unsigned long start, koniec, uplynelo;
unsigned long lastTime1 = 0, lastTime2 = 0;
const unsigned long opoznienie = 50;
int STARTstate = LOW;
int STARTlast = LOW;
int STOPstate = LOW;
int STOPlast = LOW;
void DisplayResult(){
float h, m, s, ms;
unsigned long over;
uplynelo = koniec - start;
h = int(uplynelo / 3600000);
over = uplynelo % 3600000;
m = int(over / 60000);
over = over % 60000;
s = int(over / 1000);
ms = over % 1000;
Serial.print("Zmierzony czas (milisekundy): ");
Serial.println(uplynelo);
Serial.print("Zmierzony czas: ");
Serial.print(h, 0);
Serial.print(" h ");
Serial.print(m, 0);
Serial.print(" min ");
Serial.print(s, 0);
Serial.print(" s ");
Serial.print(ms, 0);
Serial.print(" ms ");
Serial.println();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Zmierzony czas:");
lcd.setCursor(0, 1);
lcd.print(h, 0);
lcd.print("h :");
lcd.print(m, 0);
lcd.print("m :");
lcd.print(s, 0);
lcd.print("s :");
lcd.print(ms, 0);
lcd.print("ms");
}
void setup() {
lcd.init();
lcd.setCursor(0, 0);
lcd.print("Start: P1 Stop: P2");
Serial.begin(9600);
Serial.println("TEST");
pinMode(START_P, INPUT);
pinMode(START_P, INPUT);
Serial.println("Przycisk START (2), STOP(3)");
}
void loop() {
int odczyt1 = digitalRead(START_P);
int odczyt2 = digitalRead(STOP_P);
if(odczyt1 != STARTlast){
lastTime1 = millis();
}
if((millis() - lastTime1) > opoznienie){
if(odczyt1 != STARTstate){
STARTstate = odczyt1;
if(STARTstate == HIGH){
start = millis();
delay(200);
Serial.println("Uruchomiono stoper...");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Uruchomiono stoper:");
}
}
}
if(odczyt2 != STOPlast){
lastTime2 = millis();
}
if((millis() - lastTime2) > opoznienie){
if(odczyt2 != STOPstate){
STOPstate = odczyt2;
if(STOPstate == HIGH){
koniec = millis();
delay(200);
DisplayResult();
}
}
}
STARTlast = odczyt1;
STOPlast = odczyt2;
}