Electronic – arduino – photocell too sensitive

arduino

i use arduino with a photocell module to count the number of blinks of LED, arduino code :

attachInterrupt(cds, onPulse, FALLING);

sch of CdS module:
enter image description here
the problem is, for each blink of a LED, the counts is double or sometimes triple, first I suspect is the sensitivity of the CdS, I adjust the trim pot, but it doesn't resolve the problem. I also tried to use a torch, when I light up the torch the count is one, but when I tried to remove the torch away from CdS, the count increase to two. So I suspect is it the whole CdS module but not solely the problem of CdS sensitivity? I tried two new modules, both have same problem. Please advise.

Best Answer

I suspect your problem will be the interrupt routine can count pulses as short as a microsecond or so and you'll get a couple of transitions when it's right on the edge of detection. Probably the easiest way is to do something like the following to ignore pulses say within 10 milliseconds of each other:

volatile uint32_t last_count_ms = 0;

void isr()
{
   if (millis() - last_count_ms < 10)
       return;
   last_count_ms = millis();
   // Do the count here
}

Having too many interrupts per second can cause other problems with your system if the CPU ends up spending all its time servicing the interrupt, so the solution Transistor posted is probably a better long-term solution.