HO Slot Car lap counter miscounts

Ok I'm another bit of a newbe however I need some advice regarding how to deal with appears to "noise" from running the slot cars on the track and accurate lap counts. Im using an IR photo transistor to capture the lap count as it passes. Basically this is working (on the bench) but I keep getting erroneous counts at random times while the cars travel around the track. I have tried setting several delays in hope of preventing these counts but without success. My expectation is that this is noise in the power however running this counter on a battery still results in extra counts on either lane. Any thoughts, comment appreciated.

Note: Light value transition generally holds between 800 and 850 when idle. Drops below 500 when IR light beam is broken.
Note2: conventional photo resistor responded too slowely to be used.

Sketch below:

Apologies as its a bit messy.
#include <Arduino.h>
#include <TM1637Display.h>

#define CLK1 2
#define DIO1 3

#define CLK2 10
#define DIO2 11

TM1637Display display1(CLK1, DIO1);
TM1637Display display2(CLK2, DIO2);

/////////////////////////////////////////////////////

int lightPen1=A0;
int lightPen2=A1;

int lightVal1;
int lightVal2;

int dt1=20; // changes here have little effect beyond missing the count all together
int dt2=20; // changes here have little effect beyond missing the count all togethe
///////////////
int bluePin=9;
int grnPin=6;
////////////////////////////////////////////////////
int numb1=0;
int numb2=0;

///////////////////////////////////////////////
void setup() {
// put your setup code here, to run once:

pinMode (bluePin,OUTPUT);
pinMode (grnPin,OUTPUT);

pinMode (lightPen1,INPUT);
pinMode (lightPen2,INPUT);

Serial.begin (9600);
}

void loop()
{
lightVal1=analogRead(lightPen1);
Serial.println (lightVal1);

if (lightVal1>500) {
digitalWrite (bluePin,LOW);
}
if (lightVal1<500){

digitalWrite (bluePin,HIGH);
}
//delay (dt1);

//////////////////////////////////////////////////
lightVal2=analogRead(lightPen2);
Serial.println (lightVal2);

if (lightVal2>500) {

digitalWrite (grnPin,LOW);
}
if (lightVal2<500){

digitalWrite (grnPin,HIGH);
}
//delay (dt2);
//////////////////////////////////////////////
display1.setBrightness(0x0f);
display2.setBrightness(0x0f);

/////////////////////////////////////////////////
////////////////////////////////////////////////

{
display1.showNumberDec(numb1,false);
if(digitalRead(6)==1)
numb1++;
delay (dt1);
}
///////////////////////////////////////////////////////
{
display2.showNumberDec(numb2,false);
if(digitalRead(9)==1)
numb2++;
delay (dt2);
}
}

A data sheet for the photosensor and a simple schematic of how you have things connected would be nice.
Which Arduino?

1 Like

Thanks for the quick reply.
Im using a"LAFVIN Nano V3.0, Nano Board "

The IR setup is a "Gikfun 5mm 940nm LEDs Infrared Emitter and IR Receiver Diode for Arduino (Pack of 20pcs) EK8443" I found both on Amazon.

The IR receiver is flush mounted under the track surface. I sanded off part of the receiver to make it flat while ensuring I was not close to the diode itself. The IR diode is mounted directly above the receiver on an aluminum frame. A basic test setup was initially setup and worked fine.

Wires from the IR receiver are using a 4 wire ganged wire set ( same as used with LED strips) approx 8" from the track to the Nano.

Sharing the ground wire (3 wire set) was a problem... as both channels will count when sharing a ground. I know this does not make any sense as everything else shares the same ground.,

Hope that helps.

slot cars are sweet..
here's a stab at it for you..

#include <Arduino.h>
#include <TM1637Display.h>

#define CLK1 2
#define DIO1 3

#define CLK2 10
#define DIO2 11

TM1637Display display1(CLK1, DIO1);
TM1637Display display2(CLK2, DIO2);

/////////////////////////////////////////////////////

int lightPen1 = A0;
int lightPen2 = A1;

int lightVal1;
int lightVal2;

int dt1 = 20; // changes here have little effect beyond missing the count all together
int dt2 = 20; // changes here have little effect beyond missing the count all togethe
///////////////
int bluePin = 9;
int grnPin = 6;
////////////////////////////////////////////////////
int numb1 = 0;
int numb2 = 0;

///////////////////////////////////////////////

unsigned long lastRead = 0;
int readDelay = 50;
int lastVal1;
int lastVal2;


void setup() {
  // put your setup code here, to run once:

  pinMode (bluePin, OUTPUT);
  pinMode (grnPin, OUTPUT);

  pinMode (lightPen1, INPUT);
  pinMode (lightPen2, INPUT);
  display1.setBrightness(0x0f);
  display2.setBrightness(0x0f);


  Serial.begin (9600);
}

void loop()
{

  if (millis() - lastRead >= readDelay) {
    lastRead = millis();
    lightVal1 = analogRead(lightPen1);
    lightVal2 = analogRead(lightPen2);
  }

  if (lightVal1 != lastVal1) {
    lastVal1 = lightVal1;
    Serial.println (lightVal1);
    if (lightVal1 > 500) {
      digitalWrite (bluePin, LOW);
    }
    if (lightVal1 < 500) {
      digitalWrite (bluePin, HIGH);
      numb1++;
      display1.showNumberDec(numb1, false);
      Serial.print("Lap count 1: ");
      Serial.println(numb1);
    }
  }

  if (lightVal2 != lastVal2) {
    lastVal2 = lightVal2;
    Serial.println (lightVal2);
    if (lightVal2 > 500) {
      digitalWrite (grnPin, LOW);
    }
    if (lightVal2 < 500) {
      digitalWrite (grnPin, HIGH);
      numb2++;
      display2.showNumberDec(numb2, false);
      Serial.print("Lap count 2: ");
      Serial.println(numb2);
    }
  }

}

play it here..

have fun.. ~q

Read sideways instead of vertical. Use a cross track beam like NHRA does.
The upwards looking detector might be picking up ambient light. Use a modulated signal to filter out room light. Maybe put emitter in track and receiver above with shroud to cover stray light.
Put a drop of clear superglue on the end of the sanded LED to improve its clarity. The scratched surface may be reducing/dispersing the optical signal.
Compute fastest car transit. This gives minimum pulse width for valid detection; anything shorter is noise.
Use interrups and timers to trigger and measure events.
Double up sensors and combine with AND (or NAND) logic so both sensors need to hit for valid signal. This should eliminate most noise. Combine with min pulse width & your counts should be absolute.

1 Like

Thanks to everyone for you comments and thoughts! I'll try these and post an update when I get a few min.

I think I see it but no schematic to point to.

..so it turns out that the flat wire I was using and the fact that I initially chose to use an old power section of track (power to track completely disconnected and rerouted to sensors) was causing the problem. Good old inductance and motor noise was being picked up by these wires....mumble! what was I thinking!!. Scope showed this easily. The solution was to use a normal 9" section of track and using the same IR receiver types (sanded flat and mounted under the track as before) and using a section of old USB cable ( about 10" in length). LED lighting was used again from the original. No more extra counts in either lane.

I also filled the IR diode holes with super glue as suggested and fond that my signal readings increased a bit both track lanes. Thanks for the tip.

The suggested code worked also worked well. Thanks again!
I retried my original sketch modifying it to use only one delay at the end and it also worked as expected.

Thanks again to everyone for your help!


This is a pix of the setup using the flat wire and a power section of track. The track rails were disconnected from the power connecting rails. Replacing this track section with a standard section of track and connecting the IR receivers using an old USB cable wires resolved this issue.

So it sounds like an EMI pickup problem that was resolved by the twisted pair wiring of the USB cable?

Slot cars make for excellent spark gap transmitters.

Yes, that's correct. Works fine now with either the original code I listed or the one suggested.

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