Values of millis assigned to variables in while are not available outside loop

I am designing a timing gate that will record the times between a car breaking a laser beam pointed at a light dependent resistor as it goes round a track. I also would like to record the time it takes the car to pass so I can use it as a speed trap as well. I am using the millis to record the time elapsed between high and low inputs from the LDR (attached to a potential divider).

My problem is that the values I am getting from the while loops I am using are not available outside of the loops. I can display the time the input has been high or low straight after it's recorded, but it seems to 'forget' the values once I go onto the next stage of the sketch. I need to be able to add the high and low values (lap and speed) to get the full lap time after the two have been recorded.

Please could anyone tell me why I am losing the values and how I can retain them. Thanks. The code is below.

/*
FS Timing Gate sketch
sketch to calculate the lap time of a car based on breaking a laser 
beam shining onto an LDR as the car passes - sketch times the gap between the light pulses
and time takes car to pass
by Anita Crompton
version 1.0
written 18/11/13) 
updated 1/12/13  */

const int sensorPin = 2; // pin connect to centre of potential divider
float startTime; // value returned from millis when input received
float lapTime; // variable to store the duration
float speedTime; // variable to store the time of the car passing the gate
float timeInSec;  // to convert the time to seconds
float gateSpeed; // to store speedtrap speed
float speedInSec; // for speedtrap time

void setup()
{
  pinMode (sensorPin, INPUT); // set to read the input
  digitalWrite(sensorPin, HIGH); // turns on pull up resisitor
  Serial.begin (9600);
}

void loop ()
{
 if (digitalRead (sensorPin) == LOW)
 {
   startTime = millis();
 while (digitalRead (sensorPin) == LOW);
 float lapTime = millis()-startTime;
 float timeInSec = lapTime/1000;
 Serial.println (timeInSec);
 Serial.print("Lap time =");
 Serial.println (timeInSec); // duration between light pulses displayed on screen
 }
 if (digitalRead (sensorPin)==HIGH)
 {while (digitalRead (sensorPin)==HIGH);
 float speedTime = millis()-startTime;
 float speedInSec=speedTime/1000;
 float gateSpeed= 2.0 /speedInSec;
 Serial.print("Speed trap speed =");
 Serial.println(gateSpeed);
 }
 Serial.println(timeInSec);
}

When I run this I get a 0 for the timeInSec I ask it to print at the end.

Thanks for any assistance. I have tried searching for an answer, but I'm not sure how to phrase it to get the answer I need.

Anita :)

It’s because you have two different "timeinSec"s. Is called scope.

Mark

here, you have global variables :

float startTime; // value returned from millis when input received
float lapTime; // variable to store the duration
float speedTime; // variable to store the time of the car passing the gate
float timeInSec;  // to convert the time to seconds
float gateSpeed; // to store speedtrap speed
float speedInSec; // for speedtrap time

and here, you have local variables, with same names :

 while (digitalRead (sensorPin) == LOW);
 float lapTime = millis()-startTime;
 float timeInSec = lapTime/1000;
....
 float speedTime = millis()-startTime;
 float speedInSec=speedTime/1000;
 float gateSpeed= 2.0 /speedInSec;

remove the 'float' in the loop() function (they are already defined as float ;) ) , so that global variables are used everywhere in your sketch.

Thank you. I see now what I’ve done. Much appreciated.

Anita

I also would like to record the time it takes the car to pass so I can use it as a speed trap as well.

To do this you need to know the exact length of the car, and it will only work for cars of equal length and if the car passes the laser dead square and at the same height (unless it's leading and trailing edges are vertical).

Have you factored that into your plans?

A better method would be to use two lasers for the speed.


Rob