I don't want to waste CPU cycles on 1000 interrupts if I can reduce it to 200 - the Atmega 328 will be working at 8MHz and it has other stuff to do.
Some options?
- don't use interrupts
- somehow turn off the pin2 interrupt only during bounce intervals
- use different sensor with debounced output
- modify the existing sensor (see below)
I'm not clear if your diagrams of the bouncing are to scale, and if so, what is the scale. The bouncing seems to take up a huge proportion of the cycle time.
Its to illustrate a few points. The scale of the bouncing doesn't matter very much if it occurs on each end of the pulse. If your pulses are much more sharp with only 10µs bounce at each end and you have 50% duty cycle, you'll still need a debounce interval > 50% of the signal's period (same as for the diagram). What if the duty is 60/40, 70/30 or higher?
I'm not sure how you've designed your optic sensor, but if you made the black/blocking portion or clear portion much greater than the other so you get (for example) 25% duty, then software debounce would be more practical and easier.
Also, if the signal is open-collector or open-drain type, then there should be an external pullup resistor somewhere. Adding a very physically small capacitor from the signal to GND might be all that's needed. In this case, FALLING mode is more appropriate.
It is too early for either of us to speculate about the possible causes of too many interrupts on my project. Your little diagram would require two extra components - I have very limited space so I want to avoid that if I can.
Why not use an analog input and run the Serial Plotter to get a picture of the signal?