Hi, is it possible to handle an interrupt by using an analog pin? I'm having issues with my CNY70 sensor, I would like to activate the interrupt when it surpases a certain value.
The digital inputs that trigger an interrupt do so when the input crosses a threshold. If you want to use interrupts, you just need to scale your signal so that the critical voltage on the external signal corresponds to the threshold voltage at the Arduino's input pin.
If you want to do more general processing of analog values you can always poll the analogue input to detect events, as long as you can afford to have a bit of latency in the detection.
While every didgital input pin will have a threshold, it's not a tightly controlled parameter. The ATmega328P datasheet shows that the highest voltage that the pins will sense as a digital low is between 0.2VCC and 0.3VCC, or between 1.0 and 1.5V at 5V, while the minimum voltage that will be sensed as high is between 0.6VCC and 0.7 VCC, or between 3 and 3.5V at 5V. In between, the pin will sense either a high or a low, but it doesn't promise which.
If you want better resolution, you'll have to use something else. One option is an external comparator with the output connected to any pin that can give you a digital input. Another option is to use the analog comparator interrupt. Either way, you'll have to supply the other voltage input. If your signal varies slowly relative to the Arduino's clock speed, you'll want to add some hysteresis to the input signal.
For more on the ATmega328P's analog comparator, see chapter 23 of the datasheet dated 07/2012, here: http://www.atmel.com/Images/doc8271.pdf. If you'd like to see some sample code that uses the comparator interrupt, look here: http://interface.khm.de/index.php/lab/experiments/frequency-measurement-library/. There's a description of a frequency measurement project, and a link to a library called "FreqPeriod." The example code is in the library.
This post shows converting an analog input to digital by using a comparator:
Then that could trigger an interrupt. The example code on that page detects the interrupt.
Thank you all!