Laptimer Stop void loop and give best lap. (Help)

Hi! Well i will first start and say that im pretty new to this with coding. But here it goes

I have made a setup with a KY-008 laser and a KY-018 photoresistor. So everytime the RC car passes the (Light beam) i get a lap time it works like a charm. The problem is that it never stops and when i get to lap 30 and there is no more room on the display it starts overlapping the last lap

But i want to make it more advanced so it stops when i get to a certain lap let say Lap 30. When it gets there i want the loop to stop and it prints the best lap time.

So it more like this

Lap 27 : 9.58
Lap 28 : 10.84
Lap 29 : 10.74
Lap 30 : 10.54 Race Finished Best lap: Lap 27 : 9.58

Im using a arduino mega with a 3,2" TFT sheild screen (Not the touch screen)

If someone can guide me to this i would be happy!

This is the code now.

/*

Code for KY-008 Laser and Laser Detector
by eLab Peers (C) 2014.

Visit us at:
http://www.elabpeers.com

All rights reserved.

Wiring:
1.  VCC to 5V on Arduino board
2.  GND to GND on Arduino board
3.  S on KY-008 Laser to Pin 6 on Arduino board
4.  OUT on Laser Dector to Pin 7 on Arduino board

*/
// (c) Michael Schoeffler 2014, http://www.mschoeffler.de
#include <TFT_HX8357.h>
TFT_HX8357 tft = TFT_HX8357();       // Invoke custom library

#define TFT_GREY 0x5AEB // New colour

double i = 0;
double a = millis();
double c ;
int lap = 0;
bool    fHasLooped  = false;


int pinLaser = 6; // output signal pin of laser module/laser pointer
int pinReceiver = 7; // input signal pin of receiver/detector (the used module does only return a digital state)
void setup() {
  tft.init();
  tft.setRotation(2);
  tft.fillScreen(TFT_BLACK);
  tft.setCursor(0, 0, 2);
  
  pinMode(pinLaser, OUTPUT); // set the laser pin to output mode
  pinMode(pinReceiver, INPUT); // set the laser pin to output mode
  digitalWrite(pinLaser, HIGH); // emit red laser
  Serial.begin(9600); // Setup serial connection for print out to console
}
void loop() 

 {
     delay(10);
     if(digitalRead(pinReceiver) == HIGH)   
     {
       c = millis();
       i = (c - a) / 1000;
       if(i > 1)
        { 
          lap++;
          tft.setTextColor(TFT_GREEN,TFT_BLACK); tft.setTextFont(2);
          tft.print("Lap ");
          tft.print(lap);
          tft.print(":");
          tft.print(" ");
          tft.println(i);
          a = millis();
          Serial.print("Lap ");
          Serial.print(lap);
          Serial.print(":");
          Serial.print(" ");
          Serial.println(i);
        }
     }     
  }

Wrap the timing code in an if clause controlled by a boolean variable that is initially set to true. Set a variable to a value much larger than the expected best lap time, say 999 seconds

Then

if (timingActive)
  {
    //timing code goes here
  }

Each time you calculate the lap time test it against your bestLap variable. If the new lap time is better than the existing value of bestLap then set bestLap to the new lap time

When the lap count variable reaches your required limit set the boolean to false and print the value of bestLap

lap++;
if lap >= limit)
  {
    timingActive = false;
    //code to output bestLap goes here
  }

One possible way:

Step 1: Make a variable to hold the best lap time and initialize it to an impossibly high number.

douple bestLap = 99;

Step 2: Each time a lap completes check its time against the best time (assuming variable ‘i’ is the lap time, not sure).

      if (i > 1)
      {
        lap++;

        if ( i < bestLap ) bestLap = i;

Step 3: Check for 30 laps in loop(), print best time and stop if reached.

   if ( lap == 30 )
    {
      // print best time

      while (1); // stop everything

    } else {
      if (i > 1)
      {
        lap++;

BTW, variables to hold time should be unsigned long, not double, and I encourage you to come up with descriptive variable names.

Thanks for that fast reply UKHeliBob

Okey well i not sure how to put it in the code. I did like this and compiled the code, but i have a feeling that i am far off ::slight_smile:
Added boolean before void setup and timing active false in loop.

Here is the code now.

#define TFT_GREY 0x5AEB // New colour

double i = 0;
double a = millis();
double c ;
int lap = 0;

boolean (timingActive) = true;


int pinLaser = 6; // output signal pin of laser module/laser pointer
int pinReceiver = 7; // input signal pin of receiver/detector (the used module does only return a digital state)
void setup() {
  tft.init();
  tft.setRotation(2);
  tft.fillScreen(TFT_BLACK);
  tft.setCursor(0, 0, 2);
  
  pinMode(pinLaser, OUTPUT); // set the laser pin to output mode
  pinMode(pinReceiver, INPUT); // set the laser pin to output mode
  digitalWrite(pinLaser, HIGH); // emit red laser
  Serial.begin(9600); // Setup serial connection for print out to console
}
void loop() 

 {
     delay(10);
     if(digitalRead(pinReceiver) == HIGH)   
     {
       c = millis();
       i = (c - a) / 1000;
       if(i > 1)
        { 
          {
          lap++;
            if (lap >= 30)
          {
             timingActive = false;
          }
          tft.setTextColor(TFT_GREEN,TFT_BLACK); tft.setTextFont(2);
          tft.print("Lap ");
          tft.print(lap);
          tft.print(":");
          tft.print(" ");
          tft.println(i);
          a = millis();
          Serial.print("Lap ");
          Serial.print(lap);
          Serial.print(":");
          Serial.print(" ");
          Serial.println(i);

        }      
      }

You forgot to make the timing code dependent on the value of timingActive

Wrap the timing code in an if clause controlled by a boolean variable that is initially set to true.

Thanks for the help both of you. But the code Blue Eyes come up with worked for me. Now everything works thanks.

Now i will just add in a buzzer to it and it will be completed and ready to race!
:smiley:

One thing I’ll ask about.

What happens if the car passes the sensor during the delay(10) in your loop?

DangerToMyself:
One thing I'll ask about.

What happens if the car passes the sensor during the delay(10) in your loop?

One might ask, why is there a delay at all.