IR Obstacle Sensor responds to fast moving object

I have built a Lap timer for my track car (Tamiya Mini4wd in case you know what it is) , I used

  • Arduino Nano / Arduino Pro Mini
  • 128x64 OLED display
  • FC-51 IR Obstacle Sensor
  • Push Button for Reset

Attached the Code

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64);

#define button 9 //Button on Pin 9
#define IR 2 //IR sensor on Pin 2

int timerMode = 0;
long startTime;
double lapTime = 0;

void setup() {
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setRotation(0);
  display.setTextWrap(false);
  display.dim(0);
  
  pinMode(button, INPUT_PULLUP);
  pinMode(IR, INPUT_PULLUP);

  pinMode(2, INPUT);
  digitalWrite(2, HIGH);
  pinMode(9, INPUT);
  digitalWrite(9, HIGH);

}

void loop() {

  char string[10];

  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(0, 0);
  display.println("Lap Time");
  
  if (digitalRead(IR) == LOW){
    startTime = millis();
    timerMode++;
    delay(200);
  }
  if (timerMode == 1){
    display.setCursor(0, 20);
    lapTime =((millis() - startTime) / 1000.0);
    display.println(lapTime);
  }
  if (timerMode > 1){
    display.setCursor(0,40);
    display.println(lapTime);
  }
  if (digitalRead(button) == LOW){  //Reset Button trigger
    timerMode = 0;
  }
  display.display();
}

My build is working as design,

  1. Wait for the sensor to be triggered and start the timer count
  2. If the Sensor sense the second Trigger, the timer stop and print the time
  3. Reset Button reset the timer

My Issue is that when the car passes the sensor too quickly, it seems the sensor does not trigger Start or Stop of the timer. I can see the obstacle LED flash when the object pass the sensor, but sin many cases (over 70% of time) the Timer either NOT Start or Doesn’t Stop.

I have adjusted the range and the position is constant and stable, and as mentioned the sensor obstacle LED does flash. Any idea how I may improve the responsiveness between the Sensor and the Arduino??

Please post your code inline, as described in the forum guidelines (see the sticky on top). I for one am too lazy for download, open in IDE, etc. Others on mobile devices just can't open it at all.

But without even looking at the code, chances are it's such a typical case of "get rid of those delay() calls!".