The TCRT5000 IR sensor and interrupts
i'm working tcrt5000 ir sensor mounted on small circuit board. board includes led lights when sensor goes high.
i'm using sensor count things. counts can occur rate of 1 per second high 500 per second.
i've put code counting using interrupts. see below.
the problem i'm having count jumps rather incrementing sequentially. want increment count 1 each time sensor goes high. count jumps random amount typically 4 or 5.
here code:
suggestions please.
gary
i'm using sensor count things. counts can occur rate of 1 per second high 500 per second.
i've put code counting using interrupts. see below.
the problem i'm having count jumps rather incrementing sequentially. want increment count 1 each time sensor goes high. count jumps random amount typically 4 or 5.
here code:
code: [select]
#define sensorpin 2 // pin number sensors
unsigned long rawnumber = 0;
unsigned long prevmillis = 0;
volatile unsigned long interruptcounter = 0;
void setup ()
{
serial.begin (9600);
// initialize sensor pin input:
pinmode (sensorpin, input);
// set interrupt routine
attachinterrupt (0, sensorinterrupt, rising);
}
void loop()
{
// read state of sensor
sensorstate = digitalread (sensorpin);
if (millis () - prevmillis > 100)
{
rawnumber += interruptcounter;
interruptcounter = 0;
prevmillis = millis ();
}
serial.println (rawnumber);
}
void sensorinterrupt ()
{
interruptcounter++;
}
suggestions please.
gary
did check signal sensor? 1 edge if such count event occurs?
check scope. i'd expect multiple strikes per event might more practical not use interrupt polling input , debouncing in software. 500hz quite slow mcu 1 in arduino.
check scope. i'd expect multiple strikes per event might more practical not use interrupt polling input , debouncing in software. 500hz quite slow mcu 1 in arduino.
Arduino Forum > Using Arduino > Sensors > The TCRT5000 IR sensor and interrupts
arduino
Comments
Post a Comment