Very simple 2 car stopwatch problem

Hello! I am new to Arduino and have a hopefully easy problem.

I am developing a simple timer that measures the time 2 cars take to go down a track. At this stage my sketch uses a switch to start both cars, and two switches time Car 1 and Car 2 through a certain track length. The sketch works fine and gives the correct elapsed times if Car 1 finishes first (i.e. digitalRead(3)==HIGH but not if Car 2 finishes first (i.e. digitalRead(2)==HIGH

I am sure it is my inexperience with programming Arduino that is the issue.

Can anyone find what I am doing wrong in the following sketch?

Thanks in advance, Paul.

//Simple 2 car stopwatch unsigned long start, fin1, elap1, fin2, elap2; int k, k1, k2; //for dply corr

void setup() { Serial.begin(9600); pinMode(10, INPUT); //P10 = start button pinMode(3, INPUT); //P3 = stop3 button pinMode(2, INPUT); //P3 = stop2 button Serial.println("Press 1 for start/reset, 2 for elapse time"); }

void displayResult() { float h1,m1,s1,ms1,h2,m2,s2,ms2; unsigned long over1, over2; //over = remainder (mod)

elap1 = fin1 - start; h1 = int(elap1 / 3600000); over1 = elap1 % 3600000; m1 = int(over1 / 60000); over1 = elap1 % 60000; s1 = int(over1 / 1000); ms1 = over1 % 1000;

elap2 = fin2 - start; h2 = int(elap2 / 3600000); over2 = elap2 % 3600000; m2 = int(over2 / 60000); over2 = elap2 % 60000; s2 = int(over2 / 1000); ms2 = over2 % 1000;

Serial.print("Raw elapsed time CAR#1: "); Serial.println(elap1); Serial.print("Time CAR#1: "); Serial.print(m1,0); Serial.print("m "); Serial.print(s1,0); Serial.print("s "); Serial.print(ms1,0); Serial.println("ms "); Serial.println();

Serial.print("Raw elapsed time CAR#2: "); Serial.println(elap2); Serial.print("Time CAR#2: "); Serial.print(m2,0); Serial.print("m "); Serial.print(s2,0); Serial.print("s "); Serial.print(ms2,0); Serial.println("ms ");

Serial.println(); Serial.println(); }

void loop()

{ if (digitalRead(10) == HIGH) { start = millis(); delay(300); Serial.println("Started..."); } // NOTHING HAPPENS UNTIL 10 GOES HIGH, THEN NEED 3 TO GO HIGH:

if (digitalRead(3) == HIGH) { fin1 = millis(); delay(200); }

if (digitalRead(2) == HIGH) { fin2 = millis(); delay(200);

displayResult();

}

}

What kind of car detection are you using?
Please post you code using the </> icon in the posting menu.
Show us a schematic of your project.

Edit:
Why use delay()s?
You should wait till both cars come in before showing results.

Welcome to the forum.

Can anyone find what I am doing wrong in the following sketch?

First, you did not post it inside of the code tags </> icon found at the top left of the toolbar. They will show the word “code” twice in brackets, and your code should be posted between the two words.

Second, your displayResult()function is inside of the conditional test for the fin2, so if it occurs first you wont see fin1.

if (digitalRead(2) == HIGH)
             {
                fin2 = millis();
                delay(200);
                        
                displayResult();
                
              }

I would recommend changing your program to define two boolean variables checkeredFlag1 and checkeredFlag2. Declare them FALSE in setup. Make each one TRUE in the digitalRead() block for that car’s finish.

if (digitalRead(3) == HIGH)
              {
                fin1 = millis();
                 //delay(200);
                checkeredFlag1 = TRUE;
              }
           
           
            if (digitalRead(2) == HIGH)
             {
                fin2 = millis();
                //delay(200);
                   checkeredFlag2 = TRUE;      
                 //displayResult();
                
              }

Then you will have code like this

if(checkeredFlag1==TRUE && checkeredFlag2 ==TRUE )//both cars have finished
{
displayResult(); 
checkeredFlag1=FALSE;//reset the flags so you don't keep printing out the result
checkeredFlag2 =FALSE;
}

Take all the delays out of your program. They should not be needed, and you could miss a close finish.

Paul123:
Hello! I am new to Arduino and have a hopefully easy problem.

Thanks in advance, Paul.

Paul,

I got bored and wrote a drag strip timer. It uses 3 active high switch on pins 2,3,10. It uses the Serial for messaging. if you have time try it.

/* RaceCars
 Chuck Todd <ctodd@cableone.net>
 */

#define start_pin 10
#define maxtime 25000 // maximum 'race' time in milliseconds. DNF (did not Finish)

unsigned long start_time;
unsigned long stop2_time; // tick count when switch stop2 detected
unsigned long stop3_time; // tick count when switch stop3 detected
uint8_t debounce;

void setup(){
 Serial.begin(9600);
 pinMode(start_pin,INPUT); // Active High, should have resistor tying it to ground (4.7k)
 
 pinMode(2,INPUT); // Active High, should have resistor tying it to ground (4.7k)
 
 pinMode(3,INPUT); // Active High, should have resistor tying it to ground (4.7k)
 
    stop2_time=0;
    stop3_time=0;
    start_time=0; // 0 means standby.  if nonzero, race underway
} 

void start_race(){
 start_time=millis();
 stop2_time=0;
    stop3_time=0;
    debounce = 0;
}

void race_over(){
if ((stop2_time!=0)&&(stop3_time!=0)){ // both cars finished
  if (stop2_time>stop3_time){ //car 3 won
     Serial.print(" Car 3 won by ");
 Serial.print((stop2_time-stop3_time),DEC);
 Serial.println(" milliseconds!");
  }
  else if(stop2_time<stop3_time){ //car 3 won
     Serial.print(" Car 2 won by ");
 Serial.print((stop3_time-stop2_time),DEC);
 Serial.println(" milliseconds!");
  } 
  else {
     Serial.println(" Tie Race! ");
  }
  Serial.println();
  Serial.print(" Car 2 elapsed time =");
  Serial.print((stop2_time-start_time),DEC);
  Serial.println(" milliseconds!");
  Serial.print(" Car 3 elapsed time =");
  Serial.print((stop3_time-start_time),DEC);
  Serial.println(" milliseconds!");
}
else { // one or both cars DNF
  if(stop2_time!=0){ // car 2 won
     Serial.print(" Car 2 elapsed time =");
     Serial.println((stop2_time-start_time),DEC);
     Serial.println(" Car 3 Did not Finish ");
  }
  else if(stop3_time!=0){  // car 3 won
     Serial.println(" Car 2 Did not Finish ");
     Serial.print(" Car 3 elapsed time =");
     Serial.println((stop3_time-start_time),DEC);
  }
  else{ // neither car finished
     Serial.println(" Car 2 Did not Finish ");
     Serial.println(" Car 3 Did not Finish ");
  }
}  
Serial.println(" Get ready for next race!");
delay(5000);  
}

static unsigned long delay_counter=0; 
static uint8_t staged=0;
void loop(){
 if(start_time!=0){ //race is running
   if (delay_counter<millis()){
   Serial.println("Race Running");
   delay_counter = millis()+1000; // next message in 1 second.
   }

   // debounce car 2
       // if switch has been high for 50ms then accept as good press
       // if switch held for less than 50ms then start debounce count over.
   
   if(digitalRead(2)==HIGH){
   if(stop2_time!=0){
      if(((debounce&1)==0)&&(millis()>(stop2_time+50))) {
  debounce = debounce |1;
  Serial.println("2 finished");
  }
   }else {
   stop2_time = millis();
   debounce = debounce & 0xfe;
   }
   }else{
   if((debounce&1)==0){
   stop2_time=0;
   }
   }
// debounce car 3
// if switch has been high for 50ms then accept as good press
// if switch held for less than 50ms then start debounce count over.

   if(digitalRead(3)==HIGH){
   if(stop3_time!=0){
      if(((debounce&2)==0)&&(millis()>(stop3_time+50))) {
  debounce = debounce |2;
                  Serial.println("3 finished");
  }
 }else {
   stop3_time = millis();
   debounce = debounce & 0xfd;
   }
   }else{
   if((debounce&2)==0){ stop3_time=0;
   }
   }
     
   if((debounce&0x3)==0x3){ // race over
         race_over();
         stop2_time =0
 stop3_time =0;
 start_time=0; // nolonger racing
 delay_counter = millis(); // message timeout
 
   else if(millis()>(start_time+maxtime)){ // race over, One or both cars DNF
         if((debounce&0x1)==0)stop2_time=0;  // stop time not valid it not debounced
 if((debounce&0x2)==0)stop3_time=0;  // stop time not valid if not debounced
 
     race_over();
 stop2_time=0;
 stop3_time=0;
 start_time=0; // nolonger racing
 delay_counter = millis(); // message timeout
   }
 }
 else {// look for start button
       delay_counter=millis(); // next message in 1ms
   staged =0; 
     while((staged&0x3)!=0x3){
  if(delay_counter<millis()){
 // check is cars are staged, (finish line switches are clear)
 staged =0;
 if(digitalRead(2)==LOW) staged = staged | 1;
 if(digitalRead(3)==LOW) staged = staged | 2;
 if((staged&0x03)!=0x03){ 
   Serial.println("Stage Cars, Clear Switches");
   if((staged&1)==0) Serial.println(" Please Stage Car 2");
   if((staged&2)==0) Serial.println(" Please Stage Car 3");
 }else
 Serial.println(" Cars Staged");
 
 delay_counter = millis() +1000; // next message in 1 second
  }
  if((digitalRead(2)==LOW)&&((staged&0x1)==0)) {//
    Serial.println("Car 2 Staged ");
 staged = staged | 0x1;   // set flag bit marking car 2 as staged
  }
  if((digitalRead(2)==HIGH)&&((staged&0x1)==1)) {//
    Serial.println("Car 2 UN Staged ");
 staged = staged & 0xFE;  // clear flag bit marking car 2 as staged
  }
  if((digitalRead(3)==LOW)&&((staged&0x2)==0)) {//
    Serial.println("Car 3 Staged ");
 staged = staged | 0x2;  // set flag bit marking car 3 as staged
  }
  if((digitalRead(2)==HIGH)&&((staged&0x2)==2)) {//
    Serial.println("Car 3 UN Staged ");
 staged = staged & 0xFD;  // clear flag bit marking car 3 as staged
  }
   }
   delay_counter = millis();
   while(((staged&0x3)==0x3)&&(start_time==0)){
   // if timeout print message
   if( delay_counter<millis()){
   Serial.println(" Press 10 to start ");
   delay_counter=millis()+10000; // next message in 10 seconds (10,000 milliseconds)
   }
   // verify finish line buttons are clear
     staged =0;
   if(digitalRead(2)==LOW) staged = staged | 1;
   if(digitalRead(3)==LOW) staged = staged | 2;
           // if finish line clear and start then begin the race.
   if(((staged&0x3)==0x3)&&(digitalRead(10)==HIGH)) start_race();
   
   }
   delay_counter=millis(); // 1ms delay to next Serial message
  }
 
}

Chuck.


Check out my Kickstarter Project Memory Panes an expansion RAM Shield for Mega2560’s. It adds 1MB of RAM for those projects where 8KB is not enough.

A sincere thank-you to all the kind people who provided helpful advice to a newbie.

Using this advice I now have a working sketch! All I have to do now is add some electronics to switch state when a car goes by.

Regards to all, Paul.

Check out the stopwatch class - http://playground.arduino.cc/Code/StopWatchClass -