Help coding Lap Times (4 digits)

Hi guys,

I have been working on a Hot Wheels track timer project for a while but got stuck with a specific part of the coding, i.e. displaying Lap Times (4 digits, seconds.milliseconds e.g: "6.971"). I have been writing the code for some time now but couldn't find any display codes example that fits my application, nor do I understand existing examples thoroughly to be able to write the display codes in the context of the project.

I'm using 4 digits of Large 7 Segment Displays with Digit Drivers, both form Sparkfun (and chained just like suggested in their hookup guide) for each track. Is there by any chance someone who is familiar with this and willing to help with some guidance or specific codes for displaying the Lap Times part?

I have also attached the code (In Word document) I have so far.

If anything, please see link below for more details on the project (originally posted in another section):

Sparkfun Hookup example:
https://learn.sparkfun.com/tutorials/large-digit-driver-hookup-guide

Any help is very much appreciated!

Regards,

Kelvin

This time with the attachment.

Timersystem.doc (233 KB)

I have also attached the code (In Word document) I have so far.

You wrapped a few hundred bytes of text in a huge-assed word doc. Why? Post the text AS text.

My apologies, I have just found out about the code text function. Maybe this is better.

int MicroSwitch = 2; 
int MicroSwitch_State = HIGH;
int Gate_State = -1
long MicroSwitch_LastDebounceTime = 0;
long MicroSwitch_DebounceDelay = 50;
float GatePressDuration = 0
int MinimumGateClosedDuration = 3000

int Sensor_L = 3; 
int WinnerLight_L = 4;
int LAT_L = 5;
int CLK_L = 6;
int SER_L = 7;

int Sensor_L_State = HIGH;
long Sensor_L_LastDebounceTime = 0;
long Sensor_L_DebounceDelay = 50;

int Sensor_R = 8; 
int WinnerLight_R = 9;
int LAT_R = 10;
int CLK_R = 11;
int SER_R = 12;

int Sensor_R_State = HIGH;
long Sensor_R_LastDebounceTime = 0;
long Sensor_R_DebounceDelay = 50;

int StartTime = 0;
int LeftPassTime = 0;
int RightPassTime = 0;
int DifferenceInPassTime = 1000;
int MaxRaceDuration = 10000000;
int DisplayState = -1
int DisplayTime = 0
int MinimumDisplayDuration = 5000

void setup() {
pinMode(MicroSwitch, INPUT_PULLUP);
pinMode(Sensor_L, INPUT_PULLUP);
pinMode(Sensor_R, INPUT_PULLUP);

pinMode(WinnerLight_L, OUTPUT);
pinMode(LAT_L, OUTPUT);
pinMode(CLK_L, OUTPUT);
pinMode(SER_L, OUTPUT);

pinMode(WinnerLight_R, OUTPUT);
pinMode(LAT_R, OUTPUT);
pinMode(CLK_R, OUTPUT);
pinMode(SER_R, OUTPUT);

digitalWrite(WinnerLight_L, HIGH);
digitalWrite(WinnerLight_R, HIGH);
}

void loop() {
MicroSwitch_State = digitalRead(MicroSwitch);
Sensor_L_State = digitalRead(Sensor_L);
Sensor_R_State = digitalRead(Sensor_R);

if ( (millis() - MicroSwitch_LastDebounceTime) > MicroSwitch_DebounceDelay) {
    if ( (MicroSwitch_State == LOW) && (Gate_State < 0) ) {
        StartTime = micros();
        Gate_State = -Gate_State;
        MicroSwitch_LastDebounceTime = millis();
        GateClosedDuration = 0
    }
}

if (Gate_State > 0) {
    if ( (Sensor_L_State == LOW) && ( (millis() - Sensor_L_LastDebounceTime) > Sensor_L_DebounceDelay) {
        LeftPassTime = (micros() - StartTime);
        Sensor_L_LastDebounceTime = millis();
    }
    else if (((micros() - StartTime)) > MaxRaceDuration) && (RightPassTime > 0) {
              LeftPassTime = 9999000;
    }
}

if (Gate_State > 0) {
    if ( (Sensor_R_State == LOW) && ( (millis() - Sensor_R_LastDebounceTime) > Sensor_R_DebounceDelay) {
        RightPassTime = (micros() - StartTime);
        Sensor_R_LastDebounceTime = millis();
    }
    else if (((micros() - StartTime)) > MaxRaceDuration) && (LeftPassTime > 0) {
              RightPassTime = 9999000;
    }	
}

if ((LeftPassTime > 0) && (RightPassTime > 0)) {
   [ACTION OF DISPLAYING PASS (i.e. LAP) TIMES HERE]
   DisplayState = -DisplayState;
   DisplayTime = millis();
   if ((LeftPassTime - RightPassTime) > DifferenceInPassTime)) {
       digitalWrite(WinnerLight_L, LOW);
   }
   else if ((RightPassTime - LeftPassTime) > DifferenceInPassTime)) {
       digitalWrite(WinnerLight_R, LOW);
   }
   else if (abs(LeftPassTime - RightPassTime) < DifferenceInPassTime)) {
       digitalWrite(WinnerLight_L, LOW);
       digitalWrite(WinnerLight_R, LOW);
       delay(500)
       digitalWrite(WinnerLight_L, HIGH);
       digitalWrite(WinnerLight_R, HIGH);
       delay(500)
       digitalWrite(WinnerLight_L, LOW);
       digitalWrite(WinnerLight_R, LOW);
       delay(500)
       digitalWrite(WinnerLight_L, HIGH);
       digitalWrite(WinnerLight_R, HIGH);
       delay(500)
       digitalWrite(WinnerLight_L, LOW);
       digitalWrite(WinnerLight_R, LOW);
   }
}

if ( (millis() - MicroSwitch_LastDebounceTime) > MicroSwitch_DebounceDelay) && ((millis() - DisplayTime) > MinimumDisplayDuration) {
    while (MicroSwitch_State == HIGH) {
    delay(100);
    GateClosedDuration = GateClosedDuration + 100;
    }
 }
   
if (GateClosedDuration > MinimumGateClosedDuration) {
   digitalWrite(WinnerLight_L, HIGH);
   digitalWrite(WinnerLight_R, HIGH);
   [ACTION OF TURNING DISPLAYS PASS (i.e. LAP) TIMES OFF HERE];
   StartTime = 0;
   Gate_State = -Gate_State;
   MicroSwitch_LastDebounceTime = millis();
   DisplayTime = 0;
   DisplayState = -DisplayState;
}

}
int MinimumGateClosedDuration = 3000

Does that code even compile?

int MaxRaceDuration = 10000000;

Is that value valid for an int on your Arduino?

Those are meant as variables which can be tuned later on.

The first one is meant as 3 seconds minimum required time for gate to be closed (i.e. micro switch held in), before displays get reset/turned off and be ready for next race again.

The second one is the maximum race duration, in micros. Thus 10 seconds. (Lap times are measured initially in micros).

Those are meant as variables which can be tuned later on.

So, you think that means that you don't need semicolons on those lines? Guess again.

Which Arduino can store 10000000 in an int? The Arduino is programmed using C++, not makeUpYourOwnRules++.

Thank you. I will take care of that.

Do you by any chance also know how to write up the codes for displaying the lap times?

Do you by any chance also know how to write up the codes for displaying the lap times?

Do you have lap times? What do you want to display them on?

There are numerous example online for displaying the digits, but none is in the format I'm trying to display the lap times or using the digit drivers I have. I would like to display lap times in the following format (seconds and milliseconds):

For example: 6.971 (Lap times won't be greater that 9.999)

I'm actually getting the lap time with the following function (e.g. for the right lane):

RightPassTime = (micros() - StartTime);

But I think if I get help with the codes needed to display that example number, I would be able to incorporate it in the codes I have so far and refine it further to get it to work.

but none is in the format I'm trying to display the lap times or using the digit drivers I have.

Why does the format mean a thing? You are trying to display some digits. You know (or should) what digit you want to display in what position. Just do it.

As for "using the digit drivers you have", use the library that sparkfun provided/linked to.

Have you looked at seven segment libraries?