help with drag race timing code

could i get some help the cone will run but i have some kind of problem in case 1 my time all ways shows as 0 0r dosnt thake a reading amd them my et time shows but its just a random number and just keeps adding up as the code runs my over all time and all lights work fine

// Dual stop watch with state machine
// output on serial monitor
// blinking led while running
#include <LiquidCrystal.h>
#include <IRremote.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

const byte startPin     = 26;
const byte stopRightPin = 11;
const byte stopLeftPin  = 10;
const byte ledPin       = 13;
int irstartPin = 23;
const int RECV_PIN = 22;
const byte prestagelPin = 8;
int prestageledlPin = 48;
const byte prestagerPin = 9;
int prestageledrPin = 46;
int stagestartPin = 24;
int stagelPin = 49;
int stagerPin = 45;
int greenlPin = 52;
int greenrPin = 43;
int redlPin = 53;
int redrPin = 42;
int startinPin = 27;

IRrecv irrecv(RECV_PIN);
decode_results results;

boolean stageLeft              = HIGH;
boolean stageRight             = HIGH;
boolean timerStartState        = HIGH;
boolean timerStopLeftState     = HIGH;
boolean timerStopRightState    = HIGH;
boolean leftFinish             = false;
boolean rightFinish            = false;
boolean leftJumpStart          = false;
boolean rightJumpStart         = false;
boolean leftReactionTimer      = false;
boolean rightReactionTimer     = false;


byte state = 0;     // state machine states: 0 ... 3

unsigned long startTime;
unsigned long stopLeft;
unsigned long stopRight;
unsigned long stopLeftReaction;
unsigned long stopRightReaction;
unsigned long RightReactionTime;
unsigned long LeftReactionTime;
unsigned long LeftET;
unsigned long RightET;
unsigned long raceTimeLeft;
unsigned long raceTimeRight;

void setup()
{
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Left");
  lcd.setCursor(11, 0);
  lcd.print("Right");

  pinMode(prestageledlPin, OUTPUT);
  pinMode(prestageledrPin, OUTPUT);
  pinMode(stagelPin, OUTPUT);
  pinMode(stagerPin, OUTPUT);
  pinMode(greenlPin, OUTPUT);
  pinMode(greenrPin, OUTPUT);
  pinMode(redlPin, OUTPUT);
  pinMode(redrPin, OUTPUT);
  pinMode(prestagelPin, INPUT_PULLUP);    //left stage sensor
  pinMode(prestagerPin, INPUT_PULLUP);    //right stage sensor
  pinMode(stagestartPin, INPUT_PULLUP);   //input singal from ir to start stage lights/tree
  pinMode(startPin, INPUT);               //timer start signal input
  pinMode(startinPin, OUTPUT);            //timer start signal output
  pinMode(stopLeftPin, INPUT_PULLUP);     //left finish sensor
  pinMode(stopRightPin, INPUT_PULLUP);    //right finish sensor
  pinMode(ledPin, OUTPUT);                //timer running led blinks
  pinMode(irstartPin, OUTPUT);            //ir output signal

  Serial.begin (9600);
  Serial.println("press start");
  irrecv.enableIRIn();
}

sketch_sep25b.ino (8.5 KB)

void loop()
{
  { // read input pins:
    stageLeft          = digitalRead(prestagelPin);   //left stage/start sensor
    stageRight         = digitalRead(prestagerPin);   //right stage/start sensor

    digitalWrite(irstartPin, LOW);        //ir rest for next run
    digitalWrite(startinPin, HIGH);       //reset timer start signal output
  }
  if  (irrecv.decode(&results))
  {
    if (results.value == 0xFF22DD)        // ir play button
    {
      digitalWrite(irstartPin, HIGH);     // signal to stage start pin
    }
    irrecv.resume();
  }
  if (stageLeft == LOW)                    //left pre stage light on if car in sensor of if not
    digitalWrite(prestageledlPin, LOW);    //on.
  else
    digitalWrite(prestageledlPin, HIGH);   //off

  if (stageRight == LOW)                   //right pre stage light on if car in sensor of if not
    digitalWrite(prestageledrPin, LOW);    //on
  else
    digitalWrite(prestageledrPin, HIGH);   //off

  if (digitalRead(stagestartPin) == HIGH)  // start tree signal from ir start pin
  {
    digitalWrite(stagelPin, LOW);          //left stage light on by ir
    digitalWrite(stagerPin, LOW);          //right stage light on by ir
    delay(1000);
    digitalWrite(startinPin, LOW);         //timer start signal output

    if (digitalRead(prestagelPin) == LOW)  //car must be in stage sensor till green if moves before red ligh will come on insted
    {
      digitalWrite(greenlPin, LOW);        // green light on
      Serial.println("Go left!");

    }
    else
    {
      digitalWrite(redlPin, LOW);          //red light on car moved befor green
      leftJumpStart == true;
      Serial.println("Left Too early!");
    }
    if (digitalRead(prestagerPin) == LOW)  //car must be in stage sensor till green if moves before red ligh will come on insted
    {
      digitalWrite(greenrPin, LOW);        // green light on
      Serial.println("Go right!");
    }
    else
    {
      digitalWrite(redrPin, LOW);         //red light on car moved befor green light
      rightJumpStart == true;
      Serial.println("Right Too early!");
    }
  }
  {
    // read input pins:
    timerStartState     = digitalRead(startPin);      //timer start pin signal input
    timerStopLeftState  = digitalRead(stopLeftPin);   //left finish line sensor
    timerStopRightState = digitalRead(stopRightPin);  //right finish line sensor


    switch (state)
    {
      case 0:                                  // waiting for start button press

        if (timerStartState == LOW)            //timer start

        {
          Serial.println("START TIMER");
          startTime = millis();
          state++;
        }
        break;

      case 1:                                      // running - waiting for left and right car to move from staging sensor
        digitalWrite(ledPin, millis() / 128 % 2); // blink the led till
        if ( ( stageLeft == HIGH) && (leftReactionTimer == false) )
        {
          stopLeftReaction = millis();
          LeftReactionTime = (stopLeftReaction - startTime);
          Serial.print("Left Reaction  ");
          Serial.println(LeftReactionTime / 1000.0, 2);
          leftReactionTimer = true;
        }

        if ( (stageRight == HIGH) && (rightReactionTimer == false) )
        {
          stopRightReaction = millis();
          RightReactionTime = (stopRightReaction - startTime);
          Serial.print("Right Reaction  ");
          Serial.println(RightReactionTime / 1000.0, 2);
          rightReactionTimer = true;
        }

        state++;

        break;

      case 2:                                 // running - waiting for left and right car to cross finist sensor and print time

        if ( (timerStopLeftState == LOW) && (leftFinish == false) )   //input from left finish line sensor
        {
          stopLeft = millis();
          LeftET = (stopLeft - LeftReactionTime);
          raceTimeLeft = (stopLeft - startTime);
          Serial.print("Left Finished:  ");
          Serial.println(LeftReactionTime / 1000.0, 2);
          Serial.println(LeftET / 1000.0, 2);
          Serial.println(raceTimeLeft / 1000.0, 2);
          lcd.setCursor(0, 1);
          lcd.print(LeftET / 1000.0, 2);
          leftFinish = true;
        }

        if ( (timerStopRightState == LOW) && (rightFinish == false) )   //input fron right finish line sensor
        {
          stopRight = millis();
          RightET = (stopRight - RightReactionTime);
          raceTimeRight = (stopRight - startTime);
          Serial.print("Right Finished: ");
          Serial.println(RightReactionTime / 1000.0, 2);
          Serial.println(RightET / 1000.0, 2);
          Serial.println(raceTimeRight / 1000.0, 2);
          lcd.setCursor(12, 1);
          lcd.print(RightET / 1000.0, 2);
          rightFinish = true;
        }
        if ( (leftFinish == true) && (rightFinish == true) )
        {
          digitalWrite(ledPin, LOW);
          leftReactionTimer = false;
          rightReactionTimer = false;
          leftFinish  = false;
          rightFinish = false;

          state++;
        }
        break;


      case 3:                                 // print the winner and reset tree
        if ( (raceTimeLeft < raceTimeRight) && (leftJumpStart == false))
        {
          Serial.println("LEFT WIN");
          lcd.setCursor(6, 2);
          lcd.print("<---");
          delay(9000);
          digitalWrite(greenlPin, HIGH);
          digitalWrite(greenrPin, HIGH);
          digitalWrite(redlPin, HIGH);
          digitalWrite(redrPin, HIGH);
          digitalWrite(stagelPin, HIGH);
          digitalWrite(stagerPin, HIGH);
        }
        if ( (raceTimeRight < raceTimeLeft) && (rightJumpStart == false))
        {
          Serial.println("RIGHT WIN");
          lcd.setCursor(6, 2);
          lcd.print("--->");
          delay(9000);
          digitalWrite(greenlPin, HIGH);
          digitalWrite(greenrPin, HIGH);
          digitalWrite(redlPin, HIGH);
          digitalWrite(redrPin, HIGH);
          digitalWrite(stagelPin, HIGH);
          digitalWrite(stagerPin, HIGH);
        }
        state = 0;

        break;



    }
  }
}

What are these? Snippets or two different programs, or what? What is the name you are using for what you say is "time" that is wrong?

Paul

No it's all the same it wouldnt let me post it all in one post my leftReactionTime, RightReactionTime, LeftReactionTime, LeftET, and RightET won't do right everything that case 1 control everything else works

Please post the complete program in one piece. If it is too long then post your .ino file as an attachment.

...R

added in attachment on first post

could i get some help in kinda new and i know the program is a mess but everything works but the timing that case 1 controls im not getting a et time for left or right or reaction times for left and right i do get a total time for both lanes and all lights work program in attachment

sketch_sep25b.ino (8.5 KB)

Mikeh23:
could i get some help the cone will run but i have some kind of problem in case 1 my time all ways shows as 0 0r dosnt thake a reading amd them my et time shows but its just a random number and just keeps adding up as the code runs my over all time and all lights work fine

// Dual stop watch with state machine

// output on serial monitor
// blinking led while running
#include <LiquidCrystal.h>
#include <IRremote.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

const byte startPin    = 26;
const byte stopRightPin = 11;
const byte stopLeftPin  = 10;
const byte ledPin      = 13;
int irstartPin = 23;
const int RECV_PIN = 22;
const byte prestagelPin = 8;
int prestageledlPin = 48;
const byte prestagerPin = 9;
int prestageledrPin = 46;
int stagestartPin = 24;
int stagelPin = 49;
int stagerPin = 45;
int greenlPin = 52;
int greenrPin = 43;
int redlPin = 53;
int redrPin = 42;
int startinPin = 27;

IRrecv irrecv(RECV_PIN);
decode_results results;

boolean stageLeft              = HIGH;
boolean stageRight            = HIGH;
boolean timerStartState        = HIGH;
boolean timerStopLeftState    = HIGH;
boolean timerStopRightState    = HIGH;
boolean leftFinish            = false;
boolean rightFinish            = false;
boolean leftJumpStart          = false;
boolean rightJumpStart        = false;
boolean leftReactionTimer      = false;
boolean rightReactionTimer    = false;

byte state = 0;    // state machine states: 0 … 3

unsigned long startTime;
unsigned long stopLeft;
unsigned long stopRight;
unsigned long stopLeftReaction;
unsigned long stopRightReaction;
unsigned long RightReactionTime;
unsigned long LeftReactionTime;
unsigned long LeftET;
unsigned long RightET;
unsigned long raceTimeLeft;
unsigned long raceTimeRight;

void setup()
{
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(“Left”);
  lcd.setCursor(11, 0);
  lcd.print(“Right”);

pinMode(prestageledlPin, OUTPUT);
  pinMode(prestageledrPin, OUTPUT);
  pinMode(stagelPin, OUTPUT);
  pinMode(stagerPin, OUTPUT);
  pinMode(greenlPin, OUTPUT);
  pinMode(greenrPin, OUTPUT);
  pinMode(redlPin, OUTPUT);
  pinMode(redrPin, OUTPUT);
  pinMode(prestagelPin, INPUT_PULLUP);    //left stage sensor
  pinMode(prestagerPin, INPUT_PULLUP);    //right stage sensor
  pinMode(stagestartPin, INPUT_PULLUP);  //input singal from ir to start stage lights/tree
  pinMode(startPin, INPUT);              //timer start signal input
  pinMode(startinPin, OUTPUT);            //timer start signal output
  pinMode(stopLeftPin, INPUT_PULLUP);    //left finish sensor
  pinMode(stopRightPin, INPUT_PULLUP);    //right finish sensor
  pinMode(ledPin, OUTPUT);                //timer running led blinks
  pinMode(irstartPin, OUTPUT);            //ir output signal

Serial.begin (9600);
  Serial.println(“press start”);
  irrecv.enableIRIn();
}

Then it’s time to begin debugging the variables being used in the “if” and other statements.

Paul

i dont really see a problem in that section of the code could my issue be that im tring to use the same input to control several things at once

Please write (in English, not code) a brief description of what the different parts of the code are intended to do. The names of the variables are probably meaningful for you, but not for me.

Also I don't understand what the different states are intended for. For example I am wondering if state 0 is intended to be the situation where timerStartState == LOW

...R

the first part is just lights all that works there is 2 sensors at the start that control light that i am also tring to use to control my timing yes

case o is just my first timing read that start when the green light comes on the race (start time)

case 1 is using 2 senors that run 2 lights from the beginning of the program the sensor send a low signal to turn the lights on but i want it to record a time when the sensor goes back high my reaction time reading (reaction time )

case 2 stops the timer at finish line when signal goes low
then i am using the finish time - start time to give me a total time this part of the code is working

then using the reaction time reading - start time to give me the time it took the car to move after the green light this reading is the one from case 1 when i run the program sometimes it will read but gives me a 0
and some times it just wont read

and then the finish time - reaction time reading to give me et time (elapse time) this time in the program never stops for some reason it will print a time but never stops

Case 3 is just displaying the winner and this is working

Why did you start another thread about your code?

i seen this one was more about programming than the other

Do not cross post. Threads merged.

ok thanks

Mikeh23:
case 2 stops the timer at finish line when signal goes low
then i am using the finish time - start time to give me a total time this part of the code is working

With your explanation it makes a lot more sense but I won't pretend that I understand it completely.

I see a difference between CASE 0 and CASE1. In CASE 0 the system only moves on to CASE 1 when the condition is met. In CASE 1 it moves on to CASE 2 immediately even if neither of the IF clauses is satisfied.

...R

So your saying I need to put if both reactiontimes are true state ++
Would this stop the program and not allow a car to stop the finish time if the other car never moves

Mikeh23:
So your saying I need to put if both reactiontimes are true state ++

I deliberately did NOT say that because I do not know what you want to happen or what the overall impact of doing that might be.

Perhaps you don't need state 1 at all and the code in state 1 could be combined with state 2.

To be honest I don't understand what the present code in state 1 is intended for.

...R

I have two car sitting in laser sensors creating a low signal when the light turns green my timer starts to signal the race to start the cars till move and my laser signal will go high I want to mesur the time from the green light/timer start till when the car moves and laser signal goes high then the cars will go through another laser to stop the race timer then I will use the three times to calculate reaction time ( time it took car to move after green) et time ( time car moved till finished) and total time (time from green/start till finish)

From the description in Reply #18 it seems to me that the final time will ALWAYS be considerably later than the time to be recorded in CASE 1.

If that is correct then I would put the line state++; in BOTH of the IF clauses in CASE 1 so that whichever one is triggered first will cause the system to move to CASE 2, but it will not move to CASE 2 until one of those IF clauses is triggered.

And, obviously, delete the existing line state++;

...R