Store Lap Times and Display After Time Trials are Over

Hi All,

I am using photo resistor and laser w/an Uno (rev. 3) as a start/finish line for local car races. I’ve been able to piece together a reasonably workable version from examples and a lot of searching. I need to store the lap times for display after x number of laps. I’ve found different possibilities but, haven’t been successful.

Current Operation:

  1. While the laser is hitting the ldr, the stopwatch scrolls/counts up with serial.println.
  2. When a car blocks the laser from hitting the ldr the stopwatch stops and resets to 0.
  3. Back to 1.

Thanks.
Jake

/*
----------------------------------------------------
           PhotoR     10K --> changed to 1K for 
                              better detection range
 +5    o---/\/\/--.--/\/\/---o GND
                  |
 Pin A0 o----------
----------------------------------------------------
*/

#include <StopWatch.h>

StopWatch SW;

int lightPin = A0;
unsigned long start = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(lightPin, INPUT); 
  SW.stop();
  SW.reset();
}

void loop()
{
  float lapTime = (SW.value() - start);

  if (analogRead(lightPin) < 799)
    {
    SW.start();
    Serial.println((lapTime)/1000, 3);
    }    
  if (analogRead(lightPin) > 800)
    {
    SW.stop();
    SW.reset();
    }     
}

/* todo: find way to store lap times and 
display after x number of laps are complete */

Why not use EEPROM to store the data?
Check the playground for the EEPROM library, think it even breaks up floats to store the data as 4 bytes.

Keep a running count of the number of laps and put each new lap time into an array of lap times indexed by the lap number.

Now you can display the data after a certain number of laps have passed. What exactly do you want to display ?
Each lap time ?
Average lap time ?
Best lap time ?
Something else ?

Once you have displayed what you want you could either clear the array and start again or overwrite the old lap times with new ones depending an what you want to do.

It's only for time trials, only one car on the track at a time. It would be nice if it marked the best lap but, it's not necessary. The array is what I was thinking but, couldn't get the code right. I only need the times stored long enough to display afterward, no need for any longer. The problem is the serial monitor very quickly scrolls passed the lap1's time while lap2 is being timed. Even after the car is finished, since the beam is hitting the ldr the clock continues to scroll. I have to manually break the beam and scroll back up through the serial monitor and visually find the times. There is no good way to suspend wires to connect and control the laser on and off where we race. Honestly, our laser is just a $3 cat toy with magnets glued to the bottom and duct tape keeping it lit.

It would be great if it would stop timing (and scrolling) after 4 or so laps and display the times. Or if possible, even some event like me quickly breaking the beam... returning an impossibly fast time as a trigger to stop and display times

If the problem that you are trying to solve is that the serial monitor continues to display lap times then only display the latest lap time recorded once.

Keeping a count of laps will enable you to do different things when the car first breaks the beam at the start (start timing for lap 1), breaks the beam on each lap (stop timing, calculate and display the lap time, increment the lap counter and start timing for the next lap) and breaks the beam at the end of the last lap (stop timing, calculate and display the lap time).

Bob, your suggestions make perfect sense and I have learned a lot slowly implementing each concept but, I am struggling a bit and hoping you or anyone would be willing to help. My for loop seems to increment just continuously, it does not wait for the second condition to be met before incrementing. I’ve included a snippet of the lines it displays for a better idea at the bottom.

/*
----------------------------------------------------
           PhotoR     10K --> changed to 1K for 
                              better detection range
 +5    o---/\/\/--.--/\/\/---o GND
                  |
 Pin A0 o----------
----------------------------------------------------
*/

#include <StopWatch.h>

StopWatch SW;

int lightPin = A0;

unsigned long start = 0;

int lap = 1;

unsigned long lapTime;
unsigned long lapArray[4];


void setup()
{
  Serial.begin(9600);
  pinMode(lightPin, INPUT); 
  SW.stop();
  SW.reset();
}

void loop()
{  
  float lapTime = (SW.value() - start);
  for (lap=1; lap<5; lap++)                //for loop doesn't stay on lap 1 until 
                                           //lightPin value drops,
     {
     if (analogRead(lightPin) > 500)
        {
           SW.start();
           delay(10);
           Serial.print("Lap ");                //lap value increments from 1-4
           Serial.print(lap);                   //with each print of time
           Serial.print(": ");
           Serial.println((lapTime)/1000, 3);
           }    
        if (analogRead(lightPin) < 499)
           {
           SW.stop();                      //if lap=1, lap-1 should start at
           lapArray[(lap-1)] = lapTime;    //lapArray[0], when for loop increments
           SW.reset();                     //lap, the calc lap-1 should be correct
           }                                                                   
     }
   Serial.print("Lap 1 ");                 //All array positions stay 0
   Serial.print(lapArray[0]);
   Serial.print(" ||||| Lap 2 ");
   Serial.print(lapArray[1]);
   Serial.print(" ||||| Lap 3 ");
   Serial.print(lapArray[2]);
   Serial.print(" ||||| Lap 4 ");
   Serial.println(lapArray[3]);

}
Lap 1 0 ||||| Lap 2 0 ||||| Lap 3 0 ||||| Lap 4 0
Lap 1: 0.779
Lap 2: 0.779
Lap 3: 0.779
Lap 4: 0.779
Lap 1 0 ||||| Lap 2 0 ||||| Lap 3 0 ||||| Lap 4 0
Lap 1: 0.890
Lap 2: 0.890
Lap 3: 0.890
Lap 4: 0.890
Lap 1 0 ||||| Lap 2 0 ||||| Lap 3 0 ||||| Lap 4 0
Lap 1: 1.002
Lap 2: 1.002
Lap 3: 1.002
Lap 4: 1.002
Lap 1 0 ||||| Lap 2 0 ||||| Lap 3 0 ||||| Lap 4 0

Why not build one of these -

It does everything you want -

Onscreen display of lap number, current lap time and best lap time. store and recall multiple sessions with best lap time shown for each and individual lap time review etc etc.

It also works in car or track side.

The instructions to build it are all on my blog and I can send you the code if you message me with your email address.

Duane B

rcarduino.blogspot.com

What I had in mind was something like this pseudo code

setup pins, variables etc
set lapNum = zero

start of loop
  if the car has broken the beam
    if lapNum == 0
      start timing
      increment lapNum
    end of if
  
    else if lapNum > 0 and lapNum < 5
      stop timing
      reset lap time to zero
      increment lapNum
      start timing
      calculate and display lap time
    end of else
  end of if
end of loop

The idea is that each time the car breaks the beam then appropriate actions are taken. This includes displaying the latest lap time only when the beam is broken. Once 4 laps are complete then no actions are taken so the display does not update. You can use an array to store each lap time if you want to but unless you need access to them later there is no need.

Was this code ever concluded?