rc lap timer

Hello, sorry for my english.

I am not a programmer, I am trying to learn by looking at codes.

I want to bulild a lap timer for rc car. This code I have adapted myself based on previous projects that supports 2 cars. Blynk side is simple; nodemcu sends lap times to blynk terminal widget thus i see every lap time on one screen.

Problem is; I would like to add a melody that will ring when one of the cars crosses the starting line… But as most tone libraries contain delays, if i use one of them, the lap times change. Is there a way to make a sound without using delay? Could you give me an idea to solve it.

Thanks

BLYNK_WRITE(3) {
  totalLap = param.asInt();
}


void setup() {
  Blynk.begin(auth, ssid, pass, server);
  Blynk.notify("LapTimer Online");
  irrecv.enableIRIn();
  delay(1000);

  lcd.begin();
  lcd.setCursor(0, 0);
  lcd.print("Ready!           ");
  lcd.setCursor(0, 2);
  lcd.print("Ready!           ");
  Serial.begin(115200);
  terminal1.println();
  terminal1.println("Ready");
  terminal1.flush();
  terminal2.println();
  terminal2.println("Ready");
  terminal2.flush();
}

BLYNK_CONNECTED() { 
  Blynk.syncAll();
}

void trigg1() {

  if ( car1Passed == false ) {
    //determine lap time in milliseconds
    lapMillisCar1 = millis() - startMillisCar1;
    startMillisCar1 = millis();

    int days = lapMillisCar1 / day ;                                //number of days
    int hours = (lapMillisCar1 % day) / hour;
    int minutes = ((lapMillisCar1 % day) % hour) / minute ;
    int seconds = (((lapMillisCar1 % day) % hour) % minute) / second;


    lapTimeCar1 = lapMillisCar1 / 1000.00;

    //if firs trigger print race started
    if (firstTriggerCar1 == true) {
      lcd.setCursor(0, 0);
      lcd.print("Car1 Started");
      terminal1.println("Car1 Started");
      terminal1.flush();
    }

    //print if not first trigger
    if (firstTriggerCar1 != true) {
      car1LapCount++;
      lcd.setCursor(18, 0);
      lcd.print(car1LapCount);
      //print lap time
      lcd.setCursor(0, 0);
      lcd.print("C1 Last: ");
      lcd.print(minutes);
      lcd.print(":");
      lcd.print(seconds);
      lcd.print("  ");
      terminal1.print("C1 Lap: ");
      terminal1.print(car1LapCount);
      terminal1.print(" / ");
      terminal1.print(minutes);
      terminal1.print(":");
      terminal1.println(seconds);
      terminal1.flush();


    }


    //if last lap is better than best lap
    if (lapMillisCar1 < bestMillisCar1 && firstTriggerCar1 != true)
    {

      bestMillisCar1 = lapMillisCar1;
      bestLapCar1 = lapTimeCar1;
      int daysBest = bestMillisCar1 / day ;                                //number of days
      int hoursBest = (bestMillisCar1 % day) / hour;
      int minutesBest = ((bestMillisCar1 % day) % hour) / minute ;
      int secondsBest = (((bestMillisCar1 % day) % hour) % minute) / second;
      //print best lap time
      lcd.setCursor(3, 1);
      lcd.print("Best: ");
      lcd.print(minutesBest);
      lcd.print(":");
      lcd.print(secondsBest);
      lcd.print("  ");
      newBestCar1 = true;

    }

    //beep piezo
    if (newBestCar1 == true)
    {
      // Best Lap Melody
      newBestCar1 = false;
    }
    else
    {
      // Normal Lap Melody
    }

    firstTriggerCar1 = false;
    car1Passed = true;
    car1PreviousMillis = millis();
  }

}


void trigg2() {

  if ( car2Passed == false ) {

    //determine lap time in milliseconds
    lapMillisCar2 = millis() - startMillisCar2;
    startMillisCar2 = millis();

    int days = lapMillisCar2 / day ;
    int hours = (lapMillisCar2 % day) / hour;
    int minutes = ((lapMillisCar2 % day) % hour) / minute ;
    int seconds = (((lapMillisCar2 % day) % hour) % minute) / second;


    lapTimeCar2 = lapMillisCar2 / 1000.00;

    //if firs trigger print race started
    if (firstTriggerCar2 == true) {
      lcd.setCursor(0, 2);
      lcd.print("Car2 Started");
      terminal2.println("Car1 Started");
      terminal2.flush();
    }

    //print if not first trigger
    if (firstTriggerCar2 != true) {
      car2LapCount++;
      lcd.setCursor(18, 2);
      lcd.print(car2LapCount);
      //print lap time
      lcd.setCursor(0, 2);
      lcd.print("C2 Last: ");
      lcd.print(minutes);
      lcd.print(":");
      lcd.print(seconds);
      lcd.print("  ");
      terminal2.print("C2 Lap: ");
      terminal2.print(car2LapCount);
      terminal2.print(" / ");
      terminal2.print(minutes);
      terminal2.print(":");
      terminal2.println(seconds);
      terminal2.flush();


    }


    //if last lap is better than best lap
    if (lapMillisCar2 < bestMillisCar2 && firstTriggerCar2 != true)
    {

      bestMillisCar2 = lapMillisCar2;
      bestLapCar2 = lapTimeCar2;
      int daysBest = bestMillisCar2 / day ;                                //number of days
      int hoursBest = (bestMillisCar2 % day) / hour;
      int minutesBest = ((bestMillisCar2 % day) % hour) / minute ;
      int secondsBest = (((bestMillisCar2 % day) % hour) % minute) / second;
      //print best lap time
      lcd.setCursor(3, 3);
      lcd.print("Best: ");
      lcd.print(minutesBest);
      lcd.print(":");
      lcd.print(secondsBest);
      lcd.print("  ");
      newBestCar2 = true;

    }

    //beep piezo
    if (newBestCar2 == true)
    {
      // Best Lap Melody
      newBestCar2 = false;
    }
    else
    {
      // Normal Lap Melody
    }

    firstTriggerCar2 = false;
    car2Passed = true;
    car2PreviousMillis = millis();

  }

}

void translateIR() {

  //Serial.print("Data=");
  // Serial.println(results.value, HEX);
  switch (results.value) {

    //    case 0xF60: Serial.println("RC5 ned");  trigg1(); break;
    //    case 0x760: Serial.println("RC5 ned");  trigg1(); break;
    //    case 0xF61:  Serial.println("RC5 opp");  trigg2(); break;
    //    case 0x761:  Serial.println("RC5 opp");  trigg2(); break;
    //4BIT RC6 CODE FROM CAR1. 4 is fastest.
    case 0x1:  Serial.println("Car 1 passed"); car1IrReadCount++; trigg1(); break;
    //4BIT RC6 CODE FROM CAR2. 4 is fastest.
    case 0x2:  Serial.println("Car 2 passed"); car2IrReadCount++; trigg2(); break;


  }
  Serial.print("IR count");
  Serial.print(car1IrReadCount);
  Serial.print("  ");
  Serial.println(car2IrReadCount);

}

void loop() {

  Blynk.run();

//to prevent more than one reading

  if ( car1Passed == true ) {
    unsigned long currentMillis = millis();
    if (currentMillis - car1PreviousMillis > interval) {
      car1PreviousMillis = currentMillis;
      car1Passed = false;
    }
  }

  if ( car2Passed == true ) {
    unsigned long currentMillis = millis();
    if (currentMillis - car2PreviousMillis > interval) {
      car1PreviousMillis = currentMillis;
      car2Passed = false;
    }
  }


  if (car1LapCount == totalLap || car2LapCount == totalLap) { //control if one of the cars compalete maximum lap count
    lcd.setCursor(17, 1);
    lcd.print("Fin");
    lcd.setCursor(17, 3);
    lcd.print("Fin");
  } else {
    if (irrecv.decode(&results)) // have we received an IR signal? if yes,
    {
      translateIR();
      Serial.println("received an IR signal");
      //tatt bort resume loop, for jeg vil ha alle koder
      //    for (int z=0; z<2; z++) // ignore 2nd and 3rd signal repeat
      //    {
      irrecv.resume(); // receive the next value
      //    }
    }
  }
  // Serial.println("NO IR signal");

}

I am trying to learn by looking at codes.

That is not a very good approach. Far better to make your fingers do the work. Type code; study the compiler messages; make corrections; run the code; see what happens.

But as most tone libraries contain delays, if i use one of them, the lap times change.

How would the lap time of the winner be affected by delay()s that happen AFTER the race is over? Or, are you referring to the lap time of the loser? Why does THAT lap time matter?

    int days = lapMillisCar1 / day ;                                //number of days

Days? Just how slow ARE these cars?

Any way, you can use millis(), as in the blink without delay example, to determine, if a melody should be playing, whether it is time to start the next note, or not. If it is, reset the time that the note started and start the note playing, and adjust the interval that the note should play.

That is not a very good approach. Far better to make your fingers do the work. Type code; study the compiler messages; make corrections; run the code; see what happens.

You are right, im working on it.

[/quote]

How would the lap time of the winner be affected by delay()s that happen AFTER the race is over? Or, are you referring to the lap time of the loser? Why does THAT lap time matter?

Maybe I misunderstood it. I want to hear a melody every time the finish line is over. So I'll see if the IR receiver can read the sended code by car.

[/quote]

Days? Just how slow ARE these cars?

this question leads us to the first line of suggestion. I think it's a code I got from copying. The cars are pretty fast. They can make speeds of 50 to 60 km / h

[/quote]

Any way, you can use millis(), as in the blink without delay example, to determine, if a melody should be playing, whether it is time to start the next note, or not. If it is, reset the time that the note started and start the note playing, and adjust the interval that the note should play.

Thank you.

just a note. you should edit and loose the [/quote] and replace with

it should look like

This is a Quote

Darn, you cannot see the cos it makes it a quote.! But you will see on your own text during edit.

[open] [/open] Like that but with quote, not open.

because if nobody says, nobody learns.

Good luck with cars.

[open] [/open] Like that but with quote, not open.

because if nobody says, nobody learns.

how it looks like :o

Thank you

how it looks like :o

Looks like a bought one.

How did you go with your cars, or are you still working.

I almost finished. I'm going to go and try it on the track next week. I design a box for the receiver.

If you are interested in this topic, I can share more details when it is finished.