I am having a problem with getting a correct interrupt from IR sensor. Actually there are 2 problems:
- I am getting more than one interrupt at a time (meaning if I break the beam, the interrupts gets fired more than once)
- I am getting an interrupt everytime I place something infront of IR beam (which is correct) and everytime, I remove that abostacle (which should not happen).
The schematic looks as follows (just the receiver):
The code is written in C and compiled in GCC, I am using AVR Atmega644:
sei();
/* Enable interrupts. */
EIMSK &= ~((1 << INT1) | (1 << INT0));
EICRA |= ((1 << ISC11) | (1 << ISC10) | (1 << ISC01) | (1 << ISC00)); /* Trigger on rising edge */
EIFR |= ((1 << INTF1) | (1 << INTF0));
EIMSK |= ((1 << INT1) | (1 << INT0));
DDRD &= ~((1 << PD2) | (1 << PD3));
PORTD |= ((1 << PD2) | (1 << PD3));
For the ISR's I use: ISR(INT0_vect)
and ISR(INT1_vect)
The idea behind this should be as follows:
- This IR sensors output is HIGH when the IR beam is closed, and goes to LOW whem the beam gets broken.
- When the beam is closed, the capacitor should get discharged through R2.
- When the beam gets broken the capacitor starts charging through R1 (R1 for setting charging time), R6 and R2.
- When the voltage on capacitor passes ~3.4V, the output of the comparator goes HIGH, thus generating interrupt on AVR.
- Similary when the voltage on capacitor goes below ~3.4V the output of the comparator goes LOW.
Based on my calculations the time to charge a capacitor from 0V to 3.41V is from 1-10ms (depends on R1), so this is the time the beam should be broken to generate an interrupt. And the time to discarge a capacitor from 4.9V to 3.41 is ~ 1ms (the comparator output goes LOW), and to 0.2V ~7ms.
Thank you for your help!