Hello, I'm very new to arduino and coding. It's a miracle I've gotten this project this far. I wanted to create a simple laser lap timer for my backyard pumptrack. I'm pretty sure I have the code doing what I want. Count laps, record current lap time in seconds and milliseconds, show last lap time, show best lap time. However, while testing I realized that if the laser beam is broken by walking through it, the first leg crossing it breaks the beam (LOW), then the space in between the legs allows the beam to hit the sensor (HIGH), then the second leg breaks the beam again (LOW). I need to program in some sort of delay in my coding, so that when the front bike wheel breaks the beam, the back bike wheel doesn't stop the timer. I'm assuming this is very easy, but I'm having trouble understanding what to put and where to put it in my code.
Here is my code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
// laps info
unsigned long currentRunStartMillis;
unsigned long lastRunInMillis;
unsigned long bestRunInMillis;
int currentLap;
unsigned long savedMillis;
int sec_val, milli_val; // this is for two variables that are used to show the time on the dispay in seconds and milliseconds
// laser gate
const int gateSensorPin = A0; // the number of the gate sensor pin
int gateSensorState; // the current reading from sensor
int lastgateSensorState = LOW; // the previous reading from sensor
unsigned long lastDebounceTime = 0; // the last time the sensor pin was toggled
int debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(gateSensorPin, INPUT);
delay(50); // this delay is to let the sensor and laser work, so we dont get the lap triggered.
lcd.init(); // initialize the lcd
lcd.backlight(); // turn on the LCD screen backlight
lcd.setCursor(0, 0);
lcd.print("Pumptrack");
lcd.setCursor(0, 1);
lcd.print("Laser Lap Timer ");
lcd.setCursor(0, 2);
lcd.print("1.0");
lcd.setCursor(0, 3);
lcd.print("Let's Ride");
delay(5000);
lcd.clear();
// Print text on LCD screen
lcd.setCursor(0, 0);
lcd.print("Lap ");
lcd.setCursor(0, 1);
lcd.print("Current Lap ");
lcd.setCursor(0, 2);
lcd.print("Last Lap ");
lcd.setCursor(0, 3);
lcd.print("Best Lap ");
//Serial.begin(9600);
delay(1500);
// reset parameters
currentRunStartMillis = 0;
lastRunInMillis = 0;
bestRunInMillis = 0;
currentLap = 0;
}
void loop() {
int reading = digitalRead(gateSensorPin);
if (reading != lastgateSensorState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != gateSensorState) {
gateSensorState = reading;
if (gateSensorState == LOW) {
savedMillis = millis();
if (currentLap > 0) {
lastRunInMillis = savedMillis - currentRunStartMillis;
if (lastRunInMillis < bestRunInMillis || bestRunInMillis == 0) {
bestRunInMillis = lastRunInMillis;
}
}
currentRunStartMillis = savedMillis;
currentLap++;
}
}
}
lastgateSensorState = reading;
// print laps
lcd.setCursor(4, 0);
lcd.print(currentLap);
// save current millis
savedMillis = millis();
// if we start the first lap
if (currentLap > 0) {
calcResultFromMillis(savedMillis - currentRunStartMillis, &sec_val, &milli_val);
} else {
calcResultFromMillis(0, &sec_val, &milli_val);
}
//code for writing the lap times on the lcd display.
// Current Lap Time
lcd.setCursor(12, 1);
lcd.print(sec_val);
lcd.print(":");
lcd.print(milli_val);
// Last Lap TIme
calcResultFromMillis(lastRunInMillis, &sec_val, &milli_val);
lcd.setCursor(9, 2);
lcd.print(sec_val);
lcd.print(":");
lcd.print(milli_val);
// Best Lap Time
calcResultFromMillis(bestRunInMillis, &sec_val, &milli_val);
lcd.setCursor(9, 3);
lcd.print(sec_val);
lcd.print(":");
lcd.print(milli_val);
}
// calculate millis into 2 values, seconds and millis for display
void calcResultFromMillis(unsigned long value, int *sec_val, int *milli_val) {
*sec_val = int(value / 1000);
*milli_val = value - *sec_val * 1000;
}