0

I am building a timing system that has a start, split and finish beams with this I am using a start gate that will drop when the race starts. when it drops it will trip the 1st sensor starting to count the millis which I am using to calculate the time. I am using IR sensors to that give me the 3 times based on the millis and then I do a calculation in excel that will give me the split and finish time.

Where I am running into issues is that after the person goes through the 3 timer sensors I have to close the start gate which trips the first sensor which messes up my times for the next person.

So I need to adjust my sketch so it takes this into consideration when it starts counting the millis. Basically I am thinking it needs a line of code to have the 1st sensor == LOW once and do nothing and then on the 2nd time that same sensor is == LOW it records the millis from that point.

Below is the sketch I am working on.

//timer
unsigned long start, reaction, push, elapsed; 


void setup()
{
  Serial.begin(115200);
  pinMode(2, INPUT); // start switch
  pinMode(3, INPUT); // split sensor
  pinMode(4, INPUT); // finish sensor
}


void loop(){
   
if (digitalRead(2) == LOW)
  {

    start = millis();
    
  }



  if (digitalRead(3) == HIGH)
  {

    reaction = millis();
 //   delay(200); // for debounce   
    
    
  }

     if (digitalRead(4)==HIGH)
      {

       push=millis();
       delay(500); // for debounce

    Serial.print(start);
    Serial.print(",");
    Serial.print(reaction);
    Serial.print(",");
    Serial.print(push);
    Serial.println();

  }

}
4
  • Are you sure it really messes up the timing of the next person? When you reset the start gates, you code sets start to the current time. But then you you actually start the next race, you code overwrites start with the correct value. As long as you reset the gate after the last sensor is triggers (which actually prints out the times), you're fine, as far as I can see.
    – Gerben
    Commented Sep 21, 2020 at 14:50
  • So what is happening is if when testing it out (just using my hand to break the beams) if I do it quickly it seems like I get a good result. but if I put the finish sensor far away from the split it gives me after calculations a number on the 1st time that is WAY too high when it should be the opposite.
    – chris
    Commented Sep 21, 2020 at 18:24
  • So for example I get a start milli of 27083 split of 28657 finish of 29214. when I do the math to get m/s it turns out to be 1.574 from start to the split and .557 from the split to the finish...... I am not sure if this is a problem with my code or if it is a problem with my math?
    – chris
    Commented Sep 21, 2020 at 18:28
  • Those millisecond values don't look odd to me, assuming the car is accelerating. I think you are calculating s/m not m/s. You should divide the distance (which I assume is 1m) by the number of seconds (which is 0.1574 and 0.557). Then you get an average of 0.64m/s in the first section. And an average of 1.8m/s in the second section.
    – Gerben
    Commented Sep 22, 2020 at 15:32

1 Answer 1

1

The start time will default to zero, so you could change it to:

if (digitalRead(2) == LOW && start == 0)
  {

    start = millis();
    
  }
3
  • 1
    You could also declare start as a static in the loop: static long start = millis();. That way it gets called only the first time, and will remember the var in the rest of the loop()
    – Ananas_hoi
    Commented Sep 21, 2020 at 16:28
  • 1
    with changing the sketch line to "if (digitalRead(2) == LOW && start == 0)" it seems that the first millis is always the same value as the first time it was tripped but the other sensors record what looks to be correct numbers.
    – chris
    Commented Sep 21, 2020 at 17:01
  • I am thinking it needs a line of code to have the 1st sensor == LOW once and do nothing - that's what my suggestion does. If you want it to "reset" then you need more code. I'm not quite sure what your parameters are. How do you know you are starting a new race?
    – Nick Gammon
    Commented Sep 23, 2020 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.