I finally got all my code to work my issue now is the sensor only read a car going by to maybe a fraction of a second and i have to cover the laser for at least 5 seconds for it to read it and it doesnt post the correct time on the serial monitor
// Pin values for Lights
const byte LEDWhite1 = 2;
const byte LEDWhite2 = 4;
const byte ARDUINOoutput = A0;
const byte ARDUINOInput = A1;
const byte LEDYellow1 = 6;
const byte LEDYellow2 = 8;
const byte LEDYellow3 = 10;
const byte LEDGreen = 11;
const byte LEDRed = 12;
const byte WinnerOut = 5;
const byte WinnerIn = 9;
// Pin values for Lasers
const byte StagingLinePin = A5;
const byte StartingLinePin = A4;
const byte EightMileLinePin = A2;
const byte FinishLinePin = A3;
//handicap timer for finish
unsigned long Handicapvalue = 0;
// Delay between the countdoown lights
const unsigned InitialDelay = 1000; // between Stage and first Yellow
const unsigned YellowDelay = 500; // between each Yellow and Green
const unsigned HandicapDelay = Handicapvalue; // Handicap
// sensitivity value to determine if sensor is blocked
const int LaserInterruptSensitivity = 0;
// State Flags
bool RaceHasStarted = false; // Set 'true' when Green light is turned on
bool RaceHasFinished = false; // Set 'true' when Finish Line is crossed
// Timers for Race
unsigned long RaceStartTime = 0; // Set to micros() when Green light is turned on
unsigned long RaceFinishTime = 0; // Set to micros() when Finish Line is crossed
unsigned long RaceEightMile = 0; // Set to micros() when 1/8 Finish Line is crossed
unsigned long ReactionTimer = 0; // This will contain the reaction time
unsigned long HandicapTimer = 0;
// Timer for Light Countdown
unsigned long CountdownStart = 0; // Note: Doubles as a state flag: if != 0, countdown is in progress; Set to millis() when Start Line is crossed
// Time since start of program
unsigned long now;
// Checks if sensor on analog input 'pin' is blocked
bool isSensorBlocked(byte pin)
{
int val = 0;
val = digitalReadFast(pin); // read input value
//Serial.print("Sensor Value: ");
//Serial.print(val);
//Serial.print("\n");
return digitalReadFast(pin) == LaserInterruptSensitivity;
}
// Turns on Light on pin 'lightPin'
void turnOnLight(byte lightPin) {
digitalWrite (lightPin, HIGH);
}
// Turns off Light on pin 'lightPin'
void turnOffLight(byte lightPin) {
digitalWrite (lightPin, LOW);
}
// turn on Red LED, turn off all others
void falseStart() {
// turn on Red LED
turnOnLight(LEDRed);
// turn off all other LEDs
turnOffLight(LEDWhite1);
turnOffLight(LEDWhite2);
turnOffLight(LEDYellow1);
turnOffLight(LEDYellow2);
turnOffLight(LEDYellow3);
turnOffLight(LEDGreen);
}
void checkStagingPin() {
if (isSensorBlocked(StagingLinePin)) {
//Serial.print("Staging Pin is blocked\n");
turnOnLight(LEDWhite1);
// delay(DebugDelay);
}
else {
//Serial.print("Staging Pin NOT blocked\n");
turnOffLight(LEDWhite1);
// delay(DebugDelay);
}
}
// if Starting Line sensor is blocked turn on white led 2 and check other arduino for car stage
void checkStartingPin() {
if (isSensorBlocked(StartingLinePin)) {
//Serial.print("Start Pin is blocked on THIS Arduino \n");
// Turn off the red light again. We are back at the start.
turnOffLight(LEDRed);
turnOnLight(LEDWhite2);
digitalWrite (ARDUINOoutput, HIGH);
// delay(DebugDelay);
} else { //
digitalWrite(ARDUINOoutput, LOW);
turnOffLight(LEDWhite2);
}
if ((digitalRead(ARDUINOInput) == HIGH) && isSensorBlocked(StartingLinePin)) {
//Serial.print("Start Pin is blocked on OTHER Arduino \n");
CountdownStart = millis(); // Start the countdown to GREEN
}
}
void RaceInPreStage() {
checkStagingPin();
checkStartingPin();
}
void RaceInCountdown() {
unsigned long elapsedCountdown = now - CountdownStart;
checkStagingPin();
// The car has left the Starting Line before the countdown ended!
if (!isSensorBlocked(StartingLinePin)) {
falseStart();
CountdownStart = 0; // DO NOT CONTINUE COUNTDOWN!
return;
}
if (elapsedCountdown >= (InitialDelay + HandicapDelay)) {
turnOnLight(LEDYellow1);
}
if (elapsedCountdown >= (InitialDelay + YellowDelay + HandicapDelay)) {
turnOnLight(LEDYellow2);
}
if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay + HandicapDelay)) {
turnOnLight(LEDYellow3);
}
if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay + YellowDelay + HandicapDelay)) {
// START!
turnOnLight(LEDGreen);
turnOffLight(LEDWhite1);
turnOffLight(LEDWhite2);
turnOffLight(LEDYellow1);
turnOffLight(LEDYellow2);
turnOffLight(LEDYellow3);
RaceHasStarted = true;
RaceStartTime = micros(); // Measure race in microseconds
CountdownStart = 0; // Countdown is done
}
}
void RaceInProgress() {
HandicapTimer = (Handicapvalue * 1000);
// When the car leaves the starting line after we turned green, capture the reaction time
if ( ReactionTimer == 0 && !isSensorBlocked(StartingLinePin)) {
ReactionTimer = micros() - RaceStartTime;
}
if (isSensorBlocked(FinishLinePin)) {
RaceFinishTime = micros() + HandicapTimer; // Time of Finish
RaceHasFinished = true;
Serial.print("Reaction Time (sec): ");
Serial.println(ReactionTimer / 1000000.0, 6);
Serial.print("Finishing time (sec): ");
Serial.println((RaceFinishTime - RaceStartTime ) / 1000000.0, 6);
Serial.print("Finishing time + Reaction time (sec): ");
Serial.println(((RaceFinishTime - RaceStartTime) + ReactionTimer) / 1000000.0, 6);
}
}
void RaceDone() {
digitalWrite(WinnerOut, HIGH);
if (digitalRead(WinnerIn) == HIGH) {
Serial.print("Done Second ");
//digitalWrite(WinnerOut, HIGH);
}
else if (digitalRead(WinnerIn) == LOW) {
Serial.print("Done First");
}
//digitalWrite(WinnerOut, HIGH);
while (1);
}
void setup() {
Serial.begin(9600);
// Initialize Lights On Drag Tree
pinMode(LEDWhite1, OUTPUT); // Pre-stage: Car is on Staging Line
pinMode(LEDWhite2, OUTPUT); // Staged: Car has reached Start Line
pinMode(LEDYellow1, OUTPUT); // Turns on 'InitialDelay' milliseconds after White2
pinMode(LEDYellow2, OUTPUT); // Turns on 'YellowDelay' milliseconds after Yellow1
pinMode(LEDYellow3, OUTPUT); // Turns on 'YellowDelay' milliseconds after Yellow2
pinMode(LEDGreen, OUTPUT); // Start: Turns on 'YellowDelay' milliseconds after Yellow3
pinMode(LEDRed, OUTPUT); // Fault: Leaving Staging Line between White2 and Green
pinMode(WinnerOut, OUTPUT);
pinMode(WinnerIn, OUTPUT);
// Initialize Input Lasers
pinMode(StagingLinePin, INPUT); //checks stage light laser
pinMode(StartingLinePin, INPUT); //checks stage light laser
pinMode(FinishLinePin, INPUT); //checks finish line light laser
pinMode(EightMileLinePin, INPUT); //checks 1/8 mile line pin
// Initialize communications between the two arduinos
pinMode(ARDUINOoutput, OUTPUT); // Arduino sends out signal to other arduino saying car is staged
pinMode(ARDUINOInput, INPUT); // Arduino recieves signal from other arduino saying car is staged
}
void loop()
{
now = millis(); // Get current time
if (RaceHasFinished) {
RaceDone();
}
else if (RaceHasStarted) {
RaceInProgress();
}
else if (CountdownStart != 0) {
RaceInCountdown();
}
else {
RaceInPreStage();
}
}