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() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  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.setCursor(0, 0);
  display.println("Lap Time");
  if (digitalRead(IR) == LOW){
    startTime = millis();
  if (timerMode == 1){
    display.setCursor(0, 20);
    lapTime =((millis() - startTime) / 1000.0);
  if (timerMode > 1){
  if (digitalRead(button) == LOW){  //Reset Button trigger
    timerMode = 0;

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!".

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.