#include <LiquidCrystal_I2C.h>
int ledPinR1 =19;//red light if jumped line
int ledPinG1 =18;//green light if sucessful
int ledPinY3 =8;//indicator light 3
int ledPinY2 =9;//indicator light 2
int ledPinY1 =10;//indicator light 1
int ledPinStageY1 =17;//staging light 1
int ledPinStageY2 = 16;//staging light 2
int buttonPin = 7;//button used to play game
int reactionTime;//holds final reaction time
int stage = 0;//holds each stage of the game
unsigned long startTime;//gets start time
unsigned long endTime;//gets end time
unsigned long rollout=240;//a delay for a drag car to actual start rolling due to the time it takes the powertrain to react
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// put your setup code here, to run once:
pinMode(ledPinR1, OUTPUT);
pinMode(ledPinG1, OUTPUT);
pinMode(ledPinY3, OUTPUT);
pinMode(ledPinY2, OUTPUT);
pinMode(ledPinY1, OUTPUT);
pinMode(ledPinStageY1, OUTPUT);
pinMode(ledPinStageY2, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
lcd.begin(20, 2);
lcd.backlight();
lcd.print("Reaction Time:");
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0, 1);
//stops after each round and displays both staging lights to indicate ready
if (digitalRead(buttonPin) == HIGH && stage == 3)
{
digitalWrite(ledPinStageY1,HIGH);
digitalWrite(ledPinStageY2,HIGH);
delay(2000);
//sets stage to ready
stage = 0;
}
//user indicated ready and now timer starts
if (digitalRead(buttonPin) == LOW && stage == 0)
{
digitalWrite(ledPinStageY1,HIGH);
digitalWrite(ledPinStageY2,HIGH);
startTime = millis();
stage = 1;
delay(500);
}
//round finished and timer stopped
if (digitalRead(buttonPin) == HIGH && stage ==1)
{
endTime = millis();
digitalWrite(ledPinStageY2,LOW);
digitalWrite(ledPinStageY1,LOW);
digitalWrite(ledPinY1,LOW);
digitalWrite(ledPinY2,LOW);
digitalWrite(ledPinY3,LOW);
stage =2;
}
//results sent here to indicate green or red light
if (stage ==2)
{
//shows green if the user pressed button after yellow
if((endTime+rollout) - startTime >=2000)
{
displayreactiontime();
digitalWrite(ledPinG1,HIGH);
delay(3000);
digitalWrite(ledPinG1,LOW);
stage=3;
}
//shows red if user pushed button before yellow
if((endTime+rollout) - startTime <=2000)
{
//displays reaction time
displayreactiontime();
digitalWrite(ledPinR1,HIGH);
delay(3000);
digitalWrite(ledPinR1,LOW);
stage=3;
}
}
//used to go through 3 indicating lights
if (digitalRead(buttonPin) == LOW && stage ==1)
{
if(millis() - startTime >= 500)
{
digitalWrite(ledPinY1, HIGH); // Update the actual LED
}
if( millis() - startTime >= 1000)
{
digitalWrite(ledPinY1, LOW); // Update the actual LED
digitalWrite(ledPinY2, HIGH); // Update the actual LED
}
if(millis() - startTime >= 1500)
{
digitalWrite(ledPinY2, LOW); // Update the actual LED
digitalWrite(ledPinY3, HIGH); // Update the actual LED
}
if(millis() - startTime >= 2000)
{
digitalWrite(ledPinY3, LOW); // Update the actual LED
}
}
}
//method calculates time
void displayreactiontime()
{
reactionTime = ((endTime+rollout)-startTime)- 2000;
Serial.print("Reaction time = ");
Serial.println(reactionTime);
lcd.println(reactionTime);
}