Hello,
I am far from being expert in Arduino, I am a beginner. That's why I need your help.
I have to code a game in which I have to fire five shots at a target.
-
Before each shot, I have to reset the starting time (TimeInitTir)
-
When the target is hit, the time is stopped and the time measured with this formula: TimeTir1 - millis()-TimeInitTir.
-
Then I convert this time into minutes, seconds, and milliseconds, and I display it on the LCD screen.
Steps 1, 2, 3 are repeated for the 5 shots.
At the end of the five shots, I have to:
a. Calculate the total time (TimeTotal + TimeTir1 + TimeTir2 + TimeTir3 + TimeTir4 + TimeTir5) and I also display it on the LCD screen.
b. Determine the index of the shot that has the smallest time and display it with the time (Example: "Best shot: shot 1" and "Time: 00:07:45")
Here is my code:
void Tir1() {
PonctuationTime1();
lcd.setCursor(18,1);
lcd.print(" ");
lcd.setCursor(18,2);
lcd.write(4);
TimeInitTir1 = millis();
while (1) {
if (digitalRead(BoutonSELECT) == HIGH){
Serial.println('Z');
delay(wait500);
finChrono5tirs(); }
if (Serial.available()>0) {
received = Serial.read();
if(received ='A'||'B'||'C'||'D'||'E'||'F')
{tone(Buzzer, freq500, dur100);
Zone1++;
TimeTir1 = (millis() - TimeInitTir1 );
// convert time to minutes, seconds, milliseconds
// conversion temps en minutes, secondes, millisecondes
TimeSec1 = (TimeTir1 /1000)%60;
TimeMil1 = (TimeTir1 %1000)/10;
TimeMin1 = (TimeTir1 /1000)/60;
// display time min, sec, mill on the LCD
// affichage du temps min, sec, mill sur l'écran LCD
lcd.setCursor(0,1);
lcd.print("Tps1:");
lcd.setCursor(13,1);
lcd.print(" sec");
lcd.setCursor(7,1);
lcd.print(":");
lcd.setCursor(10,1);
lcd.print(".");
if (TimeMin1 >= 10 ) {
lcd.setCursor(5,1);
lcd.print(TimeMin1, DEC); }
if (TimeSec1 >= 10 ) {
lcd.setCursor(8,1);
lcd.print(TimeSec1], DEC); }
if (TimeMil1 >= 10 ) {
lcd.setCursor(11,1);
lcd.print(TimeMil1, DEC); }
if (TimeMin1 < 10 ) {
lcd.setCursor(5,1);
lcd.print("0");
lcd.setCursor(6,1);
lcd.print(TimeMin1, DEC); }
if (TimeSec1 < 10 ) {
lcd.setCursor(8,1);
lcd.print("0");
lcd.setCursor(9,1);
lcd.print(TimeSec1, DEC); }
if (TimeMil1 < 10 ) {
lcd.setCursor(11,1);
lcd.print("0");
lcd.setCursor(12,1);
lcd.print(TimeMil1, DEC); }
//=====================================
lcd.setCursor(18,1);
lcd.print(" ");
lcd.setCursor(18,2);
lcd.write(4);
TimeTotal= TimeTir1 + TimeTir2 + TimeTir3 + TimeTir4 + TimeTir1;
}
} // fin if(Serial...
} // fin while
}