I am new to Arduino but would like to state a few observations, and then get your thoughts regarding whether my explanations of the observations seem reasonable!
We are measuring the frequency of the TSL235R light to frequency converter. It puts out a nice, de-bounced square wave with frequency up to 1 MHz when it is very bright, all the way down to, say, 10 Hz when it is very dark.
I have measured this frequency three different ways:
1) with digitalRead combined with simple boolean logic. When the new digitalRead measurement changes from HIGH to LOW, the program can record an edge and calculate frequency.
OBSERVATION: This method seemed particularly unreliable, and yielded strange, random fluctuations in frequency even when light levels were stable.
2) with an interrupt routine like the one found here:https://playground.arduino.cc/Main/TSL235R/. This option was very appealing to me, as I understand the interrupt fires whenever the pin equipped with interrupt functionality catches an edge.
OBSERVATION: The method did indeed work very reliable, but only up to ~ 125 kHz. Beyond this, it seemed everything else in the code failed to execute. For example, the serial monitor would simply go blank, then when the light levels were reduced, it would yield a very high count (for just one data point), which I assume the interrupt had been catching for the entire time. I suspect the interrupt executed so frequently, that everything else in the code was essentially ignored.
3) with the FreqCount library described here: https://www.pjrc.com/teensy/td_libs_FreqCount.html. This library was like some amazing black magic! It has worked very well at the highest frequency range of the sensor -- that is, about 1 MHz. No issues. However, I would really like to know more about how it works. For example, why does it require the signal be wired to pin 47 on our Mega board? Anything special about that pin? The library is like a black box to me now.
Thanks so much for your time! If I can explain anything else better, let me know :)