I have a Photo Interrupter which I am using to count the RPM of a motor.
I am incrementing an integer to show the amount of interrupts there have been.
The issue is that there can be several interrupts fired for each time the "beam" of the Photo Interrupter is broken.
I have a 5mm length of plastic which breaks the "beam". If I make the plastic shorter than that, the Photo Interrupter doesn't pick up the break.
I am using the following, very basic code:
int pin = 13;
volatile int tcnt = 0;
volatile int state = LOW;
void setup() {
Serial.begin(9600);
pinMode(pin, OUTPUT);
attachInterrupt(0, blink, CHANGE);
}
void loop() {
Serial.println(tcnt);
digitalWrite(pin, state);
}
void blink() {
tcnt = tcnt + 1;
state = !state;
}