Interrupts using analog pins

I am using a GP2D120 (optical range sensor). I have verified that the sensor work using some basic code and polling to read the values from the device. Now i want to do this using software interrupts.I have read through some Arduino documentation and feel i am close to figuring it out. Below is what i have so far;

#include <StandardCplusplus.h>
volatile int left_IR = 0;

void setup() {
Serial.begin(9600);
pinMode(A0, INPUT);

cli(); // disable interrupts
PCICR |= (1 << PCIE1); // enable bit for on register for interrupt
PCMSK1 |= (1 << PCINT8); // mask to enable interrupt for analog pin (A0)
MCUCR = (1 << ISC00) | (1 << ISC01); // shift in a 0 1 for checking pin on any change
sei(); // enable interrupts
}

void loop(void) {
cout << "left analog value is: " << left_IR << endl;

}

ISR(PCINT1_vect) {
left_IR = analogRead(A0); // set variable to analog voltage
}

Explain concept pls........... GP2D120 is Distance Measurement Sensor(DMS).

Is it possible to get a reliable interrupt from an analog signal? What value does the signal have to reach in order to fire the interrupt?

What value do you expect to get with analogRead() an instant after the same signal triggers an interrupt.

Have you a link to the device you are using? Ar you sure you need to use analogRead at all?

…R

If you are expecting to detect interrupts from changes in an analog signal, then I think that your expectation is misconceived.

Probably a better idea to use a timer interrupt that fires every x milliseconds and read/store the analogue pin value for main code to pickup and react on. Another way that is probably not suitable for your needs is to use the build in analogue comparator (Good tutorial here) that would trigger an interrupt when a certain voltage level (distance) is reached.

You need to use the much neglected comparator in the arduino chip.

http://www.gammon.com.au/forum/?id=11916

Grumpy_Mike: You need to use the much neglected comparator in the arduino chip.

http://www.gammon.com.au/forum/?id=11916

I tried this technique and got lots of spurios interrupts from a slightly noisy source .. to use you must filter and clean the voltage in .. I went for the read every x milli secs interrupt appproach coupled with a first order filter in the software to smooth the resultand a/d convert counts...

to use you must filter and clean the voltage in ..

Only if it is hovering around the switching point. A much better approach would be to use an external comparator and give it a bit of positive feedback to introduce some hysteresis into the trigger point.

vsathish: Explain concept pls........... GP2D120 is Distance Measurement Sensor(DMS).

I plan to use this DMS on a Lego robot as a means of wall detection.

Robin2: Is it possible to get a reliable interrupt from an analog signal? What value does the signal have to reach in order to fire the interrupt?

What value do you expect to get with analogRead() an instant after the same signal triggers an interrupt.

Have you a link to the device you are using? Ar you sure you need to use analogRead at all?

...R

There are different modes of triggering; any low level, any change, rising edge, and falling edge. I plan to trigger on any change. After some research, to my understanding, interrupts are most stable on single bytes of data, seeing as the values that analogRead() returns is 10 bit resolution (0-1023), i would wonder if this can even be done,

Riva: Probably a better idea to use a timer interrupt that fires every x milliseconds and read/store the analogue pin value for main code to pickup and react on.

This would probably be the best idea for my project. How would i go about setting up a timer interrupt.

RAID_ZERO:

Riva:
Probably a better idea to use a timer interrupt that fires every x milliseconds and read/store the analogue pin value for main code to pickup and react on.

This would probably be the best idea for my project. How would i go about setting up a timer interrupt.

I’ve had good luck using mstimer2 library for things like that:

http://playground.arduino.cc/Main/MsTimer2

RAID_ZERO: I plan to trigger on any change. After some research, to my understanding, interrupts are most stable on single bytes of data, seeing as the values that analogRead() returns is 10 bit resolution (0-1023), i would wonder if this can even be done,

I think you have two confusions here. There needs to be a substantial change to trigger an interrupt - the change from digital HIGH to digital LOW or vice versa. Interrupts are digital things.

The value returned by the ADC is not used to trigger an interrupt. That value is only produced when your code calls "analogRead()".

You still haven't provided a link to the specifications for your device so I have no idea what is the proper way to process its output.

...R

After some research, to my understanding, interrupts are most stable on single bytes of data, seeing as the values that analogRead() returns is 10 bit resolution (0-1023), i would wonder if this can even be done,

You can not trigger an interrupt if an analogue input changes. You can only trigger it when it crosses a threshold. If I understand what you are trying to do then no it is not possible.

RAID_ZERO: Now i want to do this using software interrupts.

This doesn't strike me as a sensible thing to want.

If you are determined, I believe it is possible to configure the ADC so that it will perform repeated conversions and generate an interrupt each time one completes; I suppose it would be possible to implement an interrupt handler to do whatever processing you wanted to the result, and call additional handler functions when thresholds were crossed etc. I highly doubt that this is a sensible thing to do in your project, though.