#include <LiquidCrystal.h>
#include "Button.h"
#define LANE_ONE_PIN A2 //Lane 1 sensor
#define LANE_TWO_PIN A3 //Lane 2 sensor
float startTime; //Reference time for the race starting
float gateOneTime; //Reference time for lane 1 finishing
float gateTwoTime; //Reference time for lane 2 finishing
bool raceStart=false; //Race has started.
bool laneOneFinish=false; //Car in lane 1 has finished
bool laneTwoFinish=false; //Car in lane 2 has finished
bool raceReset=true; //Gate is closed. Reset bools to allow new times.
const int GATE_BUTTON_PIN = A0;
//const int RESET_BUTTON_PIN = A1;
//Display
LiquidCrystal lcd(5, 7, 9, 10, 11, 12);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);// Debugging tool. Remove later.
digitalWrite(LANE_ONE_PIN, HIGH); // turn on the pullup
digitalWrite(LANE_TWO_PIN, HIGH);
pinMode(LANE_ONE_PIN, INPUT);
pinMode(LANE_TWO_PIN, INPUT);
pinMode(GATE_BUTTON_PIN, INPUT);
//pinMode(RESET_BUTTON_PIN, INPUT);
// set up the LCD's number of columns and rows:
//Lane 1 time:X.XX
//Lane 2 time:XX.X
//Time in three digits.
//Thousandths if < 10 sec.
//Tenths if >= 10 sec and < 100 sec.
lcd.begin(16, 2);
lcd.print("Lane 1 time:");
lcd.setCursor(0, 1);
lcd.print("Lane 2 time:");
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(GATE_BUTTON_PIN)==LOW) //Gate is opened
{
if (raceStart==false) //Should only run once per race.
{
lcd.clear();
lcd.print("Lane 1 time:");
lcd.setCursor(0, 1);
lcd.print("Lane 2 time:");
startTime=millis(); //time since start
raceStart=true; //Race is started
raceReset=false;
}
//Continue race untill both cars have finished
//Lane One finish line crossed
if(digitalRead(LANE_ONE_PIN)==HIGH && laneOneFinish==false)
{
gateOneTime=millis();
lcd.setCursor(12, 0);
lcd.print((gateOneTime-startTime)/1000); //Time in seconds
laneOneFinish=true;
}
//Lane Two finish line crossed
if(digitalRead(LANE_TWO_PIN)==HIGH && laneTwoFinish==false)
{
gateTwoTime=millis();
lcd.setCursor(12, 1);
lcd.print((gateTwoTime-startTime)/1000);
laneTwoFinish=true;
}
}
//Reset when closed
if(digitalRead(GATE_BUTTON_PIN)==HIGH && raceReset==false)
{
raceStart=false;
laneOneFinish=false;
laneTwoFinish=false;
raceReset=true;
}
}