#include #include // laps info unsigned long car20currentRunStartMillis; unsigned long car20lastRunInMillis; int car20currentLap; unsigned long car30currentRunStartMillis; unsigned long car30lastRunInMillis; int car30currentLap; unsigned long car40currentRunStartMillis; unsigned long car40lastRunInMillis; int car40currentLap; unsigned long car50currentRunStartMillis; unsigned long car50lastRunInMillis; int car50currentLap; // laser gate boolean car20lastgatesensorstate = LOW; // the previous reading from sensor unsigned long car20lastDebounceTime = 0; // the last time the sensor pin was toggled boolean car20reading = LOW; boolean car30lastgatesensorstate = LOW; // the previous reading from sensor unsigned long car30lastDebounceTime = 0; // the last time the sensor pin was toggled boolean car30reading = LOW; boolean car40lastgatesensorstate = LOW; // the previous reading from sensor unsigned long car40lastDebounceTime = 0; // the last time the sensor pin was toggled boolean car40reading = LOW; boolean car50lastgatesensorstate = LOW; // the previous reading from sensor unsigned long car50lastDebounceTime = 0; // the last time the sensor pin was toggled boolean car50reading = LOW; unsigned long bestRunInMillis; const int RECV_PIN = 7; IRrecv irrecv(RECV_PIN); decode_results results; LiquidCrystal lcd(9, 8, 5, 4, 3, 2); void setup() { Serial.begin(9600); // pin mode delay(50); // to late the sensor and laser work, so we wont get the lap triggered. lcd.begin(16, 4); delay(3000); lcd.setCursor(0, 0); lcd.print("C20:"); delay(50); lcd.setCursor(0, 1); lcd.print("C30:"); delay(50); lcd.setCursor(4, 2); lcd.print("C40:"); delay(50); lcd.setCursor(4, 3); lcd.print("C50:"); delay(50); lcd.setCursor(11, 0); lcd.print("|:"); lcd.setCursor(11, 1); lcd.print("|:"); lcd.setCursor(15, 2); lcd.print("|:"); lcd.setCursor(15, 3); lcd.print("|:"); irrecv.enableIRIn(); irrecv.blink13(true); // reset params car20currentRunStartMillis = 0; car20lastRunInMillis = 0; car20currentLap = 0; car30currentRunStartMillis = 0; car30lastRunInMillis = 0; car30currentLap = 0; car40currentRunStartMillis = 0; car40lastRunInMillis = 0; car40currentLap = 0; car50currentRunStartMillis = 0; car50lastRunInMillis = 0; car50currentLap = 0; } void loop() { if (irrecv.decode(&results)) { Serial.println(results.value, HEX); if ((results.value == 0x550BB350) && ((millis() - car20lastDebounceTime) >= (1000))) { car20reading = HIGH; Car20(); } else if ((results.value == 0xEA1500FF) && ((millis() - car30lastDebounceTime) >= (1000))) { car30reading = HIGH; Car30(); } else if ((results.value == 0xFD20DF) && ((millis() - car40lastDebounceTime) >= (1000))) { car40reading = HIGH; Car40(); } else if ((results.value == 0xFD20DF) && ((millis() - car50lastDebounceTime) >= (1000))) { car50reading = HIGH; Car50(); } irrecv.resume(); } //Serial.println(bestRunInMins); } void Car20() { unsigned long car20savedMillis; unsigned long car20fastestlap; // If the switch changed, due to noise or pressing: if (car20reading != car20lastgatesensorstate) { // reset the debouncing timer car20lastDebounceTime = millis(); } if (car20reading != car20lastgatesensorstate) { car20lastgatesensorstate = car20reading; // If we went High, this mean the beam was broken if (car20lastgatesensorstate == HIGH) { // save the millis so all the math on it will be done with the same value. car20savedMillis = millis(); // if its not the first lap if (car20currentLap > 0) { // save the last run car20lastRunInMillis = car20savedMillis - car20currentRunStartMillis; lcd.setCursor(4, 0); lcd.print(car20lastRunInMillis / 1000.0, 3); // if last run is faster then best run if (car20lastRunInMillis < car20fastestlap || car20fastestlap == 0) { car20fastestlap = car20lastRunInMillis; lcd.setCursor(14, 0); lcd.print(car20fastestlap / 1000.0, 3); } } car20currentRunStartMillis = car20savedMillis; car20currentLap++; if (car20fastestlap < bestRunInMillis || bestRunInMillis == 0) { bestRunInMillis = car20fastestlap; } car20lastgatesensorstate = LOW; } } } void Car30() { unsigned long car30savedMillis; unsigned long car30fastestlap; // If the switch changed, due to noise or pressing: if (car30reading != car30lastgatesensorstate) { // reset the debouncing timer car30lastDebounceTime = millis(); } if (car30reading != car30lastgatesensorstate) { car30lastgatesensorstate = car30reading; // If we went High, this mean the beam was broken if (car30lastgatesensorstate == HIGH) { // save the millis so all the math on it will be done with the same value. car30savedMillis = millis(); // if its not the first lap if (car30currentLap > 0) { // save the last run car30lastRunInMillis = car30savedMillis - car30currentRunStartMillis; lcd.setCursor(4, 1); lcd.print(car30lastRunInMillis / 1000.0, 3); // if last run is faster then best run if (car30lastRunInMillis < car30fastestlap || car30fastestlap == 0) { car30fastestlap = car30lastRunInMillis; lcd.setCursor(12, 1); lcd.print(car30fastestlap / 1000.0, 3); } } car30currentRunStartMillis = car30savedMillis; car30currentLap++; if (car30fastestlap < bestRunInMillis || bestRunInMillis == 0) { bestRunInMillis = car30fastestlap; } car30lastgatesensorstate = LOW; } } } void Car40() { unsigned long car40savedMillis; unsigned long car40fastestlap; // If the switch changed, due to noise or pressing: if (car40reading != car40lastgatesensorstate) { // reset the debouncing timer car40lastDebounceTime = millis(); } if (car40reading != car40lastgatesensorstate) { car40lastgatesensorstate = car40reading; // If we went High, this mean the beam was broken if (car40lastgatesensorstate == HIGH) { // save the millis so all the math on it will be done with the same value. car40savedMillis = millis(); // if its not the first lap if (car40currentLap > 0) { // save the last run car40lastRunInMillis = car40savedMillis - car40currentRunStartMillis; lcd.setCursor(7, 2); lcd.print(car40lastRunInMillis / 1000.0, 3); // if last run is faster then best run if (car40lastRunInMillis < car40fastestlap || car40fastestlap == 0) { car40fastestlap = car40lastRunInMillis; lcd.setCursor(12, 1); lcd.print(car40fastestlap / 1000.0, 3); } } car40currentRunStartMillis = car40savedMillis; car40currentLap++; if (car40fastestlap < bestRunInMillis || bestRunInMillis == 0) { bestRunInMillis = car40fastestlap; } car40lastgatesensorstate = LOW; } } } void Car50() { unsigned long car50savedMillis; unsigned long car50fastestlap; // If the switch changed, due to noise or pressing: if (car50reading != car50lastgatesensorstate) { // reset the debouncing timer car50lastDebounceTime = millis(); } if (car50reading != car50lastgatesensorstate) { car50lastgatesensorstate = car50reading; // If we went High, this mean the beam was broken if (car50lastgatesensorstate == HIGH) { // save the millis so all the math on it will be done with the same value. car50savedMillis = millis(); // if its not the first lap if (car50currentLap > 0) { // save the last run car50lastRunInMillis = car50savedMillis - car50currentRunStartMillis; lcd.setCursor(7, 3); lcd.print(car50lastRunInMillis / 1000.0, 3); // if last run is faster then best run if (car50lastRunInMillis < car50fastestlap || car50fastestlap == 0) { car50fastestlap = car50lastRunInMillis; lcd.setCursor(12, 1); lcd.print(car50fastestlap / 1000.0, 3); } } car50currentRunStartMillis = car50savedMillis; car50currentLap++; if (car50fastestlap < bestRunInMillis || bestRunInMillis == 0) { bestRunInMillis = car50fastestlap; } car50lastgatesensorstate = LOW; } } }