Making F1 lap timer

Please post the errors that you are getting.

Adafruit_LiquidCrystal lcd_1(0);

What does the 0 in this function call mean ?

    cm = 0.01723 * readUltrasonicDistance(7, 7);

The use of the same pin for trigger and echo looks unconventional. Are you sure that it is correct ?

Please post the schematic of your project here. I don't have a Tinkercad account and don't want one

I'm not as experienced as u, how do i put the schematic, I started this off an already made circuit, i think the 0 is the connection
The errors had to do with the numbers being displayed, but i think that had to do with the tinkercad processing speed. and the 5 secs between the when sensor senses object and when the 'while function starts. But i just added 5 secs to the time_taken

#include <Adafruit_LiquidCrystal.h>
  int seconds = 5;

  int cm = 0;
  int green = 3;
  int greenstat;
  int start;
  int end;
  int time_taken;
  Adafruit_LiquidCrystal lcd_1(0);

  long readUltrasonicDistance(int triggerPin, int echoPin)
  {

    pinMode(green, OUTPUT);
    pinMode(triggerPin, OUTPUT);  // Clear the trigger
    digitalWrite(triggerPin, LOW);
    delayMicroseconds(2);
    // Sets the trigger pin to HIGH state for 10 microseconds
    digitalWrite(triggerPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(triggerPin, LOW);
    pinMode(echoPin, INPUT);
    // Reads the echo pin, and returns the sound wave travel time in microseconds
    return pulseIn(echoPin, HIGH);
  }

  void setup()
  {
    Serial.begin(9600);
    lcd_1.begin(16, 2);
     lcd_1.print("hello world");
  }

  void loop()
  {
    
    lcd_1.setCursor(0, 1);
    lcd_1.print(seconds);
    greenstat = digitalRead(green);
    // measure the ping time in cm
    cm = 0.01723 * readUltrasonicDistance(7, 7);
    // convert to inches by dividing by 2.54

    if(cm < 50){ seconds = 5;digitalWrite(green, HIGH); delay(5000);start = seconds;}


                while(greenstat==HIGH){
                  cm = 0.01723 * readUltrasonicDistance(7, 7);
                  if(cm<50){digitalWrite(green, LOW); break;}
                                       delay(1000);
                                    seconds = seconds+1 ;
                                       lcd_1.setCursor(0, 1);
                                       lcd_1.print(seconds);
                 
                           
                						}
                end = seconds;
                time_taken = end-start+5;
                lcd_1.setCursor(5, 1);
                lcd_1.print("=");lcd_1.setCursor(6, 1);
                lcd_1.print(time_taken);
    




               }

Draw it with pencil and paper, take a photo of the drawing, copy the photo and paste it here

I have not come across an Adafruit LCD library that took such a parameter, hence my question

It is going to be difficult to provide help without knowing exactly what you are doing. For instance, I had assumed that you were using a physical system rather than Tinkercad


Here is an alternative way to time a lap

void loop()
{
    unsigned long currentTime = millis();
    unsigned long initialDelay = 5000;
    static unsigned long startTime;
    static unsigned long endTime;
    static unsigned long lapTime;

    switch (currentState)       //execute only the code for the current state 
    {
        case WAITING_TO_START:
            cm = 0.01723 * readUltrasonicDistance(7, 7);
            if (cm < 50)  //car detected
            {
                digitalWrite(green, HIGH);
                startTime = millis();  //save the start time
                currentState = TIMING_5_SECONDS;
            }
            break;
        case TIMING_5_SECONDS:
            if (currentTime - startTime >= initialDelay)
            {
                currentState = TIMING_LAP;  //settling time is over
            }
            break;
        case TIMING_LAP:
            cm = 0.01723 * readUltrasonicDistance(7, 7);
            if (cm < 50)  //car detected at end of lap
            {
                endTime = currentTime;
                lapTime = endTime - startTime;
                digitalWrite(green, LOW);
                //put code here to print the lap time
            }
            break;
    }
}

I cannot test or even compile the code because I don't have the hardware or libraries required

DO NOT expect it to compile and run without problems. I wrote it as an example of a different, more logical way to time a lap and there is still work for you to do and I may well have made mistakes in it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.