Go Down

Topic: Help changing code from a point-to-point timer to a lap timer (Read 35 times) previous topic - next topic


Hi there,

Im currently working on a basic project to set up a lap timer for a toy car. 

I currently have two IR lasers pointed at two LDRs, and it times how much time passes in between the first beam being broken and the second beam being broken (like timing point A to point B).

I wish to change to having a singular IR beam/LDR and for it to time how much time passes in between the singular beam/LDR being broken the first time and the second time (like laps around a track).

Could anyone help me change the code to do this? 

Code: [Select]
#define LEDPIN 13
#define ENDPIN A1
#define STARTPIN A0 
int endState = 0;
int startState = 0;
int timePassed = 0;
int timerStarted = false;
void setup() {
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);
  // initialize the end pin as an input:
  pinMode(ENDPIN, INPUT);     
  digitalWrite(ENDPIN, HIGH); // turn on the pullup
  // initialize the start pin as an input:
  pinMode(STARTPIN, INPUT);     
  digitalWrite(STARTPIN, HIGH); // turn on the pullup
void loop(){
  // read the state of the pushbutton value:
  endState = digitalRead(ENDPIN);
  startState = digitalRead(STARTPIN);
  // check if the start beam is broken
  // if it is, startState is LOW:
  if (startState == LOW) {     
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);  
  else {
    // turn LED off:
    digitalWrite(LEDPIN, LOW); 
  if (!endState && timerStarted) {
    float actualSeconds = timePassed;
    timerStarted = false;
  if (!startState && !timerStarted) {
    timePassed = 0;
    timerStarted = true;
  if (timerStarted) {
    timePassed += 1;


Record the value of millis() when the first beam is broken.
Record the value of millis() when the second beam is broken.
Subtract the first value from the second.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

Go Up