Drag Race Timing System With Sensors

Good day, I Need help I am busy building a small scale Drag Timing System for a hobby and I am struggling with coding I will share my code and image of my system.

What It should do is

Detect a Car Pre-stage(Photosensor) light up an led
Detect a Car Stage(PhotoSensor) Light up an led

When a start button is pressed initiate a countdown and light up an led every 500 mill

Yellow
Yellow
Yellow
Green

If the vehicle moves before green red light and nothing should happen after that
if the vehicle moves on or after green start a timer and stop it when the vehicle reaches the finish line

a reaction time from when vehicle left to when the green led was lit up if he left before green a negative reaction time should be available.

From when the vehicle left the beam to the finish the running time should be displayed on a lcd and when the vehicle reaches the finish line the ET (Elapsed time and reaction time should be displayed.) (Later on I would like to add distance to calculate the speed the vehicle was traveling) This is how far I came but I can not program for the life of me I can build the system but I am not a programer and need help.

#include <LiquidCrystal.h>

const int RS = 8;
const int EN = 9;
const int D4 = 10;
const int D5 = 11;
const int D6 = 12;
const int D7 = 13;
const int BAUD_RATE = 9600;
const int LED_Prestage = 2;
const int LED_Stage = 3;
const int LED_Y1 = 4;
const int LED_Y2 = 6;
const int LED_Y3 = 7;
const int LED_Start = A2;
const int LED_RED_Light =A3;
const int Pre_Stage_Sensor = A0;
const int Stage_Sensor = A1;
const int Finish_Sensor = A4;
const int Start_Button = 5;


const String (MESSAGE) = "System Ready.";

LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);

void setup() {
  pinMode(LED_Prestage,OUTPUT);
  pinMode(LED_Stage,OUTPUT);
  pinMode(LED_Y1,OUTPUT);
  pinMode(LED_Y2,OUTPUT);
  pinMode(LED_Y3,OUTPUT);
  pinMode(LED_Start,OUTPUT);
  pinMode(LED_RED_Light,OUTPUT);
  pinMode(Pre_Stage_Sensor,INPUT);
  pinMode(Stage_Sensor,INPUT);
  pinMode(Finish_Sensor,INPUT);
  pinMode(Start_Button,INPUT_PULLUP);
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0, 1);
  lcd.print(MESSAGE);
  

  delay(2000);
  lcd.clear();
}

void loop() {
  int Pre_Stage_Sensor_Value = analogRead(Pre_Stage_Sensor);
  int Stage_Sensor_Value = analogRead(Stage_Sensor);
  boolean StartFlag = true;

  if (Pre_Stage_Sensor_Value < 500) {
    digitalWrite(LED_Prestage, HIGH);
  } else {
    digitalWrite(LED_Prestage, LOW);
  }

  if (Stage_Sensor_Value < 500) {
    digitalWrite(LED_Stage, HIGH);
  } else {
    digitalWrite(LED_Stage, LOW);
  }

  if ((Pre_Stage_Sensor_Value <500) && (Stage_Sensor_Value <500)) {
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("Vehicle Staged");
    delay(500);
  } else {
    lcd.clear();
    lcd.begin(16,2);
    lcd.setCursor(0,1);
    lcd.print("Please Stage");
    delay(500);
  }

  if ((Stage_Sensor_Value < 500) && (digitalRead(Start_Button) == LOW)) {
    delay (500);
    digitalWrite (LED_Y1, HIGH);
    delay (500);
    digitalWrite (LED_Y1, LOW);
    digitalWrite (LED_Y2, HIGH);
    delay (500);
    digitalWrite (LED_Y2, LOW);
    digitalWrite (LED_Y3, HIGH);
    delay (500);
    digitalWrite (LED_Y3, LOW);
    digitalWrite (LED_Start, HIGH);
    StartFlag = true; // set StartFlag to true
  }
  else if (Stage_Sensor_Value > 500) { // check if the car has left the stage
    digitalWrite(LED_Start, LOW);
    StartFlag = false;
  }

  if (StartFlag == false) {
    digitalWrite(LED_RED_Light, HIGH);
  } else {
    digitalWrite(LED_RED_Light, LOW);
  }
}

1 Like

First post ... and you managed to post your code... and give a pretty clear description of what you're trying to do... +1 for that... not many make it this far!

So what doesn't do what you want it to?

Thank you yeah I am a little dumb when it comes to this stuff but trying it and hell my passion for drag racing is crazy i got it to where the light light up and sensors sense when the car is is pre stage and stage and then the start button initiates the start and countdown starts I have an issue with the red light and then the rest of the code I have been getting one error after anathor when implementing the if a car leaves and the timer not only that the timer does not stop when it senses the car at the end of the track it just keeps running and then the LCD keeps running all the print displays constantly I have changed a few things to get it slower and that seems to solve the issue but from the race start to the finish parts I am struggling over a month now and no success that's why I turned to this form to see if someone can assist me with some guidance or extra parts of code

If it were my project... I'd think about it this way.

You have a number of "states"... for example...

  • 0 Waiting for pre-stage
  • 1 pre-stage
  • 2 stage (waiting for go button)
  • 3 started (count down button pressed ... sequence has begun)
  • 4 racing (green light)
  • 5 finished (crossed the finish line).

... then it goes back to 0... Waiting for pre-stage.

So... in you loop you need manage these 6 states. An easy way to do this is with a "case" statement.

Create a global variable called state

int state = 0;

Then in loop...

switch (state)
{
  case 0:
    if (Pre_Stage_Sensor_Value < 500)
     {
       digitalWrite(LED_Prestage, HIGH);   // Turn on the pre-stage LED.
       state ++;                           // Move to the next state
     }
     break;
  case 1:
    if (Stage_Sensor_Value < 500)
     {
       digitalWrite(LED_Stage, HIGH);   // Turn on the stage LED.
       state ++;                        // Move to the next state
     }
     break;
  case 2:
    if (digitalRead(Start_Button) == LOW)
    {
      // Do the light thing
      state++;                      // Move to the next state
    } 
    break;

  ... etc
}


You wanna try the next couple of states ?

EDITED... had the break statements is the wrong place.

Thank you very much that makes a lot of sense I will give it a try and see what it does

@red_car Thank you for the advise I have done the following an implemented the state I think correctly and first thing of the bat is the LCD is sorted out no more random glitches or scrolling through a bunch of text it is stable beyond belief the timing still works but I have a new issue the red light does not switch on if the vehicle leaves the stage light before green here is the code that I have edited and reimplemented to see if you can see any issues.

#include <LiquidCrystal.h>

const int RS = 8;
const int EN = 9;
const int D4 = 10;
const int D5 = 11;
const int D6 = 12;
const int D7 = 13;
const int BAUD_RATE = 9600;
const int LED_Prestage = 2;
const int LED_Stage = 3;
const int LED_Y1 = 4;
const int LED_Y2 = 6;
const int LED_Y3 = 7;
const int LED_Start = A2;
const int LED_RED_Light =A3;
const int Pre_Stage_Sensor = A0;
const int Stage_Sensor = A1;
const int Finish_Sensor = A4;
const int Start_Button = 5;
int state = 0;
bool StartFlag = false;

const String MESSAGE = "System Ready.";

LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);

void setup() {
  pinMode(LED_Prestage,OUTPUT);
  pinMode(LED_Stage,OUTPUT);
  pinMode(LED_Y1,OUTPUT);
  pinMode(LED_Y2,OUTPUT);
  pinMode(LED_Y3,OUTPUT);
  pinMode(LED_Start,OUTPUT);
  pinMode(LED_RED_Light,OUTPUT);
  pinMode(Pre_Stage_Sensor,INPUT);
  pinMode(Stage_Sensor,INPUT);
  pinMode(Finish_Sensor,INPUT);
  pinMode(Start_Button,INPUT_PULLUP);
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0, 1);
  lcd.print(MESSAGE);
  delay(2000);
  lcd.clear();
}

void loop() {
  int Pre_Stage_Sensor_Value = analogRead(Pre_Stage_Sensor);
  int Stage_Sensor_Value = analogRead(Stage_Sensor);

  switch (state) {
    case 0:// Standby Mode
    
    if (Pre_Stage_Sensor_Value> 500){
      digitalWrite(LED_Prestage, LOW);
    }
    if (Stage_Sensor_Value> 500){
      digitalWrite(LED_Stage, LOW);
      state++;
    }
    lcd.clear();
    lcd.print("Please Stage");
    delay (500);
      break;
  
    
   case 1:
      if (Pre_Stage_Sensor_Value < 500) {
        digitalWrite(LED_Prestage, HIGH);   // Turn on the pre-stage LED.
        state ++;                           // Move to the next state
      }
      break;
    
    case 2:
      if (Stage_Sensor_Value < 500) {
        digitalWrite(LED_Stage, HIGH);   // Turn on the stage LED.
        state ++;                        // Move to the next state
      }
      break;

    case 3:
      if (digitalRead(Start_Button) == LOW) {
        delay (500);
        digitalWrite(LED_Y1, HIGH);
        delay(500);
        digitalWrite(LED_Y1, LOW);
        digitalWrite(LED_Y2, HIGH);
        delay(500);
        digitalWrite(LED_Y2, LOW);
        digitalWrite(LED_Y3, HIGH);
        delay(500);
        digitalWrite(LED_Y3, LOW);
        digitalWrite(LED_Start, HIGH);
        StartFlag = true; // set StartFlag to true
        state++;                      // Move to the next state
      } 
      break;
    
    case 4:
      if ((analogRead(Stage_Sensor) > 500) && (LED_Start, LOW)) { // check if the car has left the stage
        StartFlag = false;
        state++;
      }
break;
    
    case 5:
      if (StartFlag == false) {
        digitalWrite(LED_RED_Light, HIGH);
      } 
      if (StartFlag == true)
    {
        digitalWrite(LED_RED_Light, LOW);
      state++;
      }
      break;
  }
}

This is what I wrote last time someone needed a single-lane drag race tree. Maybe it will help.

// Pin values for Lights
const byte LEDWhite1 = 2;
const byte LEDWhite2 = 4;
const byte LEDYellow1 = 6;
const byte LEDYellow2 = 8;
const byte LEDYellow3 = 10;
const byte LEDGreen = 11;
const byte LEDRed = 12;


// Pin values for Lasers
const byte StagingLinePin = A0;
const byte StartingLinePin = A1;
const byte FinishLinePin = A3;


//  Delay between the countdoown lights
const unsigned InitialDelay = 7; // between Start Line and first Yellow
const unsigned YellowDelay = 500;  // between each Yellow and Green


const int LaserInterruptSensitivity = 10; // sensitivity value to determine if sensor is blocked


// 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


// Timer for Light Countdown
// Note: Doubles as a state flag: if != 0, countdown is in progress
unsigned long  CountdownStart = 0;  // Set to millis() when Start Line is crossed


// Checks if sensor on analog input 'pin' is blocked
bool isSensorBlocked(byte pin)
{
  return analogRead(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 setup()
{
  Serial.begin(9600);


  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
}


void loop()
{
  // Get current time
  unsigned long now = millis();


  /*
     Everything below this line is AFTER race finished
  */
  if (RaceHasFinished)
  {
    // do nothing?
  }


  /*
      Everything below this line is for AFTER race starts
  */
  else if (RaceHasStarted)
  {
    // The race has not finished but it has started to the
    // race is in progress.  Waiting for car to cross the finish line
    if (isSensorBlocked(FinishLinePin))
    {
      // race has just finished
      RaceFinishTime = micros();  // Time of Finish
      RaceHasFinished = true;
      Serial.print("Finishing time (sec): ");
      Serial.println((RaceFinishTime - RaceStartTime) / 1000000.0, 6);
    }
    else
    {
      // race is still in progress
      Serial.print("Elapsed time (sec): ");
      Serial.println((micros() - RaceStartTime) / 1000000.0, 6);
    }
  }


  /*
      Everything below this line is BEFORE the RACE starts
  */


  else if (CountdownStart != 0) // The contdown to the start is in progress
  {
    // The race has not started or finished but the countdown to start
    // is in progress so we light up the lights in sequence
    
    unsigned long elapsedCountdown = now - CountdownStart;


    // The car has left the Staging Line before the countdown ended!
    if (!isSensorBlocked(StagingLinePin))
    {
      falseStart();


      // DO NOT CONTINUE COUNTDOWN!
      CountdownStart = 0;
      return;
    }


    if (elapsedCountdown >= (InitialDelay))
      turnOnLight(LEDYellow1);


    if (elapsedCountdown >= (InitialDelay + YellowDelay))
      turnOnLight(LEDYellow2);


    if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay))
      turnOnLight(LEDYellow3);


    if (elapsedCountdown >= (InitialDelay + YellowDelay + YellowDelay + YellowDelay))
    {
      // START!
      turnOnLight(LEDGreen);
      RaceHasStarted = true;
      RaceStartTime = micros(); // Measure race in microseconds
      CountdownStart = 0; // Countdown is done
    }
  }
  /*
     Everything below this line is Pre-Stage or Stage
  */
  else
  {
    // The race is not finished or running and the countdown isn't in 
    // progress so we are waiting for pre-staging and staging.


    // White led 1 is ON when Staging Line sensor is blocked
    if (isSensorBlocked(StagingLinePin))
    {
      turnOnLight(LEDWhite1);
    }
    else
    {
      turnOffLight(LEDWhite1);
    }


    // if Starting Line sensor is blocked turn on white led 2 and start countdown
    if (isSensorBlocked(StartingLinePin))
    {
      turnOnLight(LEDWhite2);
      CountdownStart = millis();  // Start the countdown to GREEN
    }
  }
} 

Hello,
I am working on a very similar project. I have all of the hardware designed, built, & tested and am running around in circles trying to get the 500 mS timings to work properly !! Mine has two modes of operation - PRO tree and SPORTSMAN tree. The PRO tree will flash the 3 yellows then green unless one lane rolls the beam, then it goes red. The SPORTSMAN tree will countdown the 3 yellows then the green again unless the lane rolls the beam !! Tom

@johnwasser thank you very much for your input I will definitely have a look at this and see how it works on the Arduino it is highly appriciated.

Hi, @62Tom may we have a look at your code maybe even with my novice experience at leaste someone can assist with a proper designed code at the end of this thread because for some reason there is no fully written code out there and everyone is so secretive when it comes down to what I think a very simple system that we can use for everyones enjoyment of the Drag Racing Sport I know I will take everyones input and help and design a system for everyones use that anyone even the Kid in school can use with his hot-wheels because that is our future of Drag Racing at least for us in South Africa. I love this sport lets make it better

Couple of things/questions...

This code probably isn't doing what you want. Did you mean && digitalRead(LED_Start) == LOW ?

The check for an early start needs to happen in the state where you are counting down. The problem currently is that you are using delay statements to control the timing. These statements are known as "blocking" because while they are running no other code can execute... so you can't check if the car has left early. The alternative to using delay is to use the millis() statement to track time... this function returns the number of milliseconds since the Arduino started. By taking a millis snapshot, at the point you press the Start button, then checking against the current value of millis, you can determine long much time has passed.

The benefit of this approach it that you can do other things while you are waiting for the time to pass. If you look at John's example thats exactly what he does.

Have a look at the code below. I've split state 3, into 3 & 4. 3 is waiting for the start button, 4 is the countdown has begun. There are no delay statements... so the check for the car leaving early can be done continuously. I've added 1 new variable countdownStart - this keeps track of when we started counting down, and we keep comparing to the current time to work out when the lights get turned on/off.

    case 3:                                       // Waiting for the start button
      if (digitalRead(Start_Button) == LOW)       // Start button pressed?
      {
        countdownStart = millis();                // Take a snapshot of when we started the countdown.
        state++;                                  // Move to the next state
      }
      break;

    case 4:                                       // Counting down
      if (millis() - countdownStart > 2000)       // 2 seconds elapsed?
      {
        digitalWrite(LED_Y3, LOW);                // Turn off yellow LED 3
        digitalWrite(LED_Start, HIGH);            // Turn on green LED
        StartFlag = true;                         // set StartFlag to true
        state++;                                  // Move to the next state
      }
      else if (millis() - countdownStart > 1500)  // 1.5 seconds elapsed?
      {
        digitalWrite(LED_Y2, LOW);                // Turn off yellow LED 2
        digitalWrite(LED_Y3, HIGH);               // Turn on yellow LED 3
      }
      else if (millis() - countdownStart > 1000)  // 1.0 seconds elapsed?
      {
        digitalWrite(LED_Y1, LOW);                // Turn off yellow LED 1
        digitalWrite(LED_Y2, HIGH);               // Turn off yellow LED 2
      }
      else if (millis() - countdownStart > 500)   // 0.5 seconds elapsed?
      {
        digitalWrite(LED_Y1, HIGH);               // Turn on yellow LED 1
      }
      else                                        // Within the first 0.5 seconds.
      {
                                                  // Do nothing.
      }

      // Here is where you check if the car leaves early...
      if (analogRead(Stage_Sensor) > 500 && !StartFlag)
      {
        digitalWrite(LED_RED_Light, HIGH);
        // and do whatever.
      }
      
      break;

    case 5:
      break;

    case 6:
      break;

Hi abel973,
Thanks for responding. I am a hardware guy teaching myself programming
!! Right now, I have several small sketches to test the different
pieces of hardware so I will have to assemble them into a single piece
of code. They are all fairly well documented so I can remember what I
was doing & thinking at that time. I am in the process of moving over
to the latest Arduino IDE and installing it on a WIN-10 computer !!
Tedious process but I will post the code in a couple of days.
Tom

@red_car

I have seen a few issue that you showed out and fixed that but decided what you are discriibing is actually a very well explained reason to why and how mine does not work the

&& (LED_Start, LOW) 

should have actually been

&& digitalRead(LED_Start) ==

I will have a look at my code today and try and implement what you are suggesting and once again I can't thank every one of you enough for assisting in this code it is highly appreciated.

Cool... shout out if you get stuck. Have a look at @johnwasser's example too... his code is usually very good, and may give you some ideas about how to solve certain problems that you might face.

Ok so far so good lights are sorted out it has a very weird delay and not the 500mil in between it is actually set to I think it might me something with the case state's that causes this i have now written some timing code with the board as well but here is where the difficult part comes in I have spent most of the day trying to sort out the time delays and the LCD Displaying what I want when I want again

#include <LiquidCrystal.h>

const int RS = 8;
const int EN = 9;
const int D4 = 10;
const int D5 = 11;
const int D6 = 12;
const int D7 = 13;
const int BAUD_RATE = 9600;
const int LED_Prestage = 2;
const int LED_Stage = 3;
const int LED_Y1 = 4;
const int LED_Y2 = 6;
const int LED_Y3 = 7;
const int LED_Start = A2;
const int LED_RED_Light = A3;
const int Pre_Stage_Sensor = A0;
const int Stage_Sensor = A1;
const int Finish_Sensor = A4;
const int Start_Button = 5;
int state = 0;
bool StartFlag = false;
const String MESSAGE = "System Ready.";
double countdownStart;
double raceStart;
bool FinishFlag;
double FinishET;

LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);

void setup() {
  pinMode(LED_Prestage, OUTPUT);
  pinMode(LED_Stage, OUTPUT);
  pinMode(LED_Y1, OUTPUT);
  pinMode(LED_Y2, OUTPUT);
  pinMode(LED_Y3, OUTPUT);
  pinMode(LED_Start, OUTPUT);
  pinMode(LED_RED_Light, OUTPUT);
  pinMode(Pre_Stage_Sensor, INPUT);
  pinMode(Stage_Sensor, INPUT);
  pinMode(Finish_Sensor, INPUT);
  pinMode(Start_Button, INPUT_PULLUP);
  Serial.begin(BAUD_RATE);
  lcd.begin(16, 2);
  lcd.setCursor(0, 1);
  lcd.print(MESSAGE);
  delay(2000);
  lcd.clear();
}

void loop() 
{
  int Pre_Stage_Sensor_Value = analogRead(Pre_Stage_Sensor);
  int Stage_Sensor_Value = analogRead(Stage_Sensor);
  int Finish_Sensor_Value = analogRead (Finish_Sensor);
  
  switch (state) {
    case 0: // Standby State
      if (Pre_Stage_Sensor_Value > 500) {
        digitalWrite(LED_Prestage, LOW);}
        else{
          digitalWrite(LED_Prestage, HIGH);
                    
      }
       
    case 1:// Vehicle Staging State
    if (Stage_Sensor_Value < 500) {
      digitalWrite(LED_Stage, HIGH);
    }
    else{
      digitalWrite(LED_Stage, LOW);
       
      }
    
    if (Stage_Sensor_Value < 500){
      lcd.clear();
      lcd.print("Vehicle Ready");
      delay(500);
    }
      else { 
      lcd.clear();
      lcd.print("Please Stage");
      delay(500);
      
      }
     
  
 case 2:                                      
      if (digitalRead(Start_Button) == LOW)       
      {
        countdownStart = millis();
        state++;
      }
      break;

 case 3:                                       
      
       if (millis() - countdownStart > 2000)       
      {
        digitalWrite(LED_Y3, LOW);                
        digitalWrite(LED_Start, HIGH);            
        StartFlag = true;
        
      }
      else if (millis() - countdownStart > 1500)  
      {
        digitalWrite(LED_Y2, LOW);                
        digitalWrite(LED_Y3, HIGH);               
      }
      else if (millis() - countdownStart > 1000)  
      {
        digitalWrite(LED_Y1, LOW);                
        digitalWrite(LED_Y2, HIGH);               
      }
      else if (millis() - countdownStart > 500)   
      {
        digitalWrite(LED_Y1, HIGH);               
      }
   
                                                                                     
case 4:
      if (analogRead(Stage_Sensor) > 500 && !StartFlag)
      {
        digitalWrite(LED_RED_Light, HIGH);
      }
      
    
case 5:    
    
    if ((millis() - countdownStart > 2000))
        {
      lcd.clear();
      lcd.print("GO");
      raceStart= millis() - countdownStart;
      delay (500);
      lcd.clear();
      lcd.print(raceStart);
      
    }
    if (analogRead(Finish_Sensor) <500)
    { FinishFlag= true; 
      FinishET = millis() - raceStart;
      lcd.clear();
      lcd.print("Finish ET:");
      lcd.setCursor(0, 1);
      lcd.print(FinishET / 100, 3);

    }






















}}// END LOOP BRACKETS
        

What do yo think can cause the delay slow reaction time to the lights and what do you suggest when the lcd is not doing what I want im not to clued up with the state and case statements but this seems to me where the issues are. The green light also does not switch of when red light comes on how can I make that go of when red light is on I have attached a file to show you what it does (PLEASE HELP) thanx.
Screen Recording 2023-02-21 at 13.45.30.mov.zip (6.8 MB)

Each case needs a break; at the end

If I insert breaks my button does not want to initiate the start sequence what can that be?

Without a break at the end of each case you will execute the next case.

Once case 0 is satisfied and you are ready to move to case 1 you need to include state++; inside the if statement that says we are good to move on. This applies to each case except the last one. The last case resets state = 0;

I have added breaks but there is one break in particular that will not let me initiate the start button what can this be?

                                  
#include <LiquidCrystal.h>

const int RS = 8;
const int EN = 9;
const int D4 = 10;
const int D5 = 11;
const int D6 = 12;
const int D7 = 13;
const int BAUD_RATE = 9600;
const int LED_Prestage = 2;
const int LED_Stage = 3;
const int LED_Y1 = 4;
const int LED_Y2 = 6;
const int LED_Y3 = 7;
const int LED_Start = A2;
const int LED_RED_Light = A3;
const int Pre_Stage_Sensor = A0;
const int Stage_Sensor = A1;
const int Finish_Sensor = A4;
const int Start_Button = 5;
int state = 0;
bool StartFlag = false;
const String MESSAGE = "System Ready.";
double countdownStart;
double raceStart;
bool FinishFlag;
double FinishET;

LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);

void setup() {
  pinMode(LED_Prestage, OUTPUT);
  pinMode(LED_Stage, OUTPUT);
  pinMode(LED_Y1, OUTPUT);
  pinMode(LED_Y2, OUTPUT);
  pinMode(LED_Y3, OUTPUT);
  pinMode(LED_Start, OUTPUT);
  pinMode(LED_RED_Light, OUTPUT);
  pinMode(Pre_Stage_Sensor, INPUT);
  pinMode(Stage_Sensor, INPUT);
  pinMode(Finish_Sensor, INPUT);
  pinMode(Start_Button, INPUT_PULLUP);
  Serial.begin(BAUD_RATE);
  lcd.begin(16, 2);
  lcd.setCursor(0, 1);
  lcd.print(MESSAGE);
  delay(2000);
  lcd.clear();
}

void loop() 
{
  int Pre_Stage_Sensor_Value = analogRead(Pre_Stage_Sensor);
  int Stage_Sensor_Value = analogRead(Stage_Sensor);
  int Finish_Sensor_Value = analogRead (Finish_Sensor);
  
  switch (state) {
    case 0: // Standby State
      if (Pre_Stage_Sensor_Value > 500) {
        digitalWrite(LED_Prestage, LOW);}
        else{
          digitalWrite(LED_Prestage, HIGH);
                    
      }
      
    // Vehicle Staging State
    if (Stage_Sensor_Value < 500) {
      digitalWrite(LED_Stage, HIGH);
    }
    else{
      digitalWrite(LED_Stage, LOW);
       
      }
    
    if (Stage_Sensor_Value < 500){
      lcd.clear();
      lcd.print("Vehicle Ready");
      delay(500);
    }
      else { 
      lcd.clear();
      lcd.print("Please Stage");
      delay(500);
      
      }
    
    break;// if this break is removed greenligt comes on and stays on but the start button does not want to innitiate. 
    
   case 1:  
    
      if (digitalRead(Start_Button) == LOW)       
      {
        countdownStart = millis();
        
      }                               
      
       if (millis() - countdownStart > 2000)       
      {
        digitalWrite(LED_Y3, LOW);                
        digitalWrite(LED_Start, HIGH);            
        StartFlag = true;
        
      }
      else if (millis() - countdownStart > 1500)  
      {
        digitalWrite(LED_Y2, LOW);                
        digitalWrite(LED_Y3, HIGH);               
      }
      else if (millis() - countdownStart > 1000)  
      {
        digitalWrite(LED_Y1, LOW);                
        digitalWrite(LED_Y2, HIGH);               
      }
      else if (millis() - countdownStart > 500)   
      {
        digitalWrite(LED_Y1, HIGH);               
      }
   
       break;                                                                              
case 2:
      if (analogRead(Stage_Sensor) > 500 && !StartFlag)
      {
        digitalWrite(LED_RED_Light, HIGH);
      }
      
      break;
    
case 3:    
    
    if ((millis() - countdownStart > 2000))
        {
      lcd.clear();
      lcd.print("GO");
      raceStart= millis() - countdownStart;
      delay (500);
      lcd.clear();
      lcd.print(raceStart);
      
    }
    if (analogRead(Finish_Sensor) <500)
    { FinishFlag= true; 
      FinishET = millis() - raceStart;
      lcd.clear();
      lcd.print("Finish ET:");
      lcd.setCursor(0, 1);
      lcd.print(FinishET / 100, 3);

    }

break;

You read the start button on case 1. state needs to equal 1 to get there. You need state++; somewhere in case 0 to move you to state 1.

Go back and look at red_car's code in post 4. Notice every case has a state++; somewhere in it and every case has a break; at the end.
The state++; basically says you satisfied the intent of this case and you're ready to move to the next case. The break; says ignore all the code below that point in this switch statement.