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);
}
}