#include<Servo.h>//include servo library
#include <LiquidCrystal_I2C.h>
//servo tutorial - https://create.arduino.cc/projecthub/akshayjoseph666/servo-motor-interface-with-arduino-uno-9693ad?ref=search&ref_id=servo&offset=0
Servo Myservo; //create servo object to control a servo. variable name to the servo and referenced everywhere in code
int inipos=0;//initial position of servo arm set to Zero
int pos;//variable for servo arm position.
int openButton =7;//pin to which gate open button is connected
int closeButton=8;//pin to which gate close button is connected
int gateclosepos=15;//to return servo back. also to calculate race time
int gateopenpos=120;//open the start gate
long startTime;//store starting time of race
long stopTime;//store race finish time
float raceTime;//calculate laptime
float trackRecordTime;//store previous track record of session
LiquidCrystal_I2C lcd(0x27, 16, 2);//initialise the LCD
void setup() {
// put your setup code here, to run once:
//Servo related code
Myservo.attach(11); // attaching servo signal to pin 3
Myservo.write(gateclosepos);
//LCD setup
lcd.init();
lcd.backlight();
//Setup LCD Lines
lcd.setCursor(2,0);
lcd.print("Time to Race");
lcd.setCursor(2,1);
lcd.print("READY SET GO!!!");
//lcd.clear();
//led and button related code here
Serial.begin(9600);//enable serila communication with pc or device. printing text etc.
pinMode(13, OUTPUT);
pinMode(6, OUTPUT);
//pinMode(7, INPUT_PULLUP); //set to high when not pressed. when pressed connected to ground so Low (in scenario when button connected to pin and ground).
//good button tutorial on pullup and pulldown setup - https://create.arduino.cc/projecthub/muhammad-aqib/arduino-button-tutorial-using-arduino-digitalread-function-08adb5
pinMode(7, INPUT); //using a pull down resistor to make button state as low by default. so when pressed then on. wiring based on above tutorial
trackRecordTime=100.000;
}
void loop() { // put your main code here, to run repeatedly:
digitalWrite(6,HIGH);//light up leds at finish gate.
//servo operated by button
//code example from here - https://create.arduino.cc/projecthub/glowascii/servo-arduino-basics-cb9266?ref=search&ref_id=servo&offset=3
int opbuttonState = digitalRead(openButton);//button press status stored in variable
int clbuttonState = digitalRead(closeButton);//button press status stored in variable
//race start and stop and time calculation logic. using buttons for now. would need to be updated for IR leds.
if (opbuttonState == 1){//detect if openButton(race start button) is pressed
delay(10); //delay of for debounce
Myservo.write(gateopenpos); //open the start gate
lcd.clear();//lcd clear
lcd.setCursor(2,0);
lcd.print("GREEN FLAG");
lcd.setCursor(2,1);
lcd.print("GO! GO! GO!");
startTime = millis();//store the start time
}
else if (clbuttonState == 1 ){//detect if closeButton(race finish button) is pressed
delay(10); //delay of for debounce
Myservo.write(gateclosepos); //close the start gate
stopTime = millis(); //race stop time simulate with servo close button
raceTime = stopTime - startTime;//calculate the actual race time
raceTime = raceTime / 1000;//convert to seconds
//LCD Code to display race time of winner
lcd.clear();//lcd clear
lcd.setCursor(0,0);
lcd.print("WINNER T.RECORD");
lcd.setCursor(1,1);
lcd.print(raceTime , 3);//print current race time
lcd.setCursor(9,1); //set cursor to print track record
if (raceTime < trackRecordTime){ //compare new race time with track record in current session
trackRecordTime=raceTime; //if race time is faster then set this time as new track record time
}
lcd.print(trackRecordTime , 3);//print track record time
delay(500);
//code for IR Object Sensor with Leds only.- https://www.instructables.com/How-to-Make-an-IR-Object-Sensor-With-Arduino/
}
}