you have the right idea, but your code will work intermitently
the reason is the IR sensors see a pulse for either 1 or 2mS and the SW loops every 26mS - so there is a high probablility that the SW will be polling the input pin when the pulse is not there, even though the sensor is seeing the beacon
there is a very easy way to solve the problem and you DONT have to use interrrupts!!!
to fix it, rc_dig_in01 and 2 are both external interrupt pins, and they have logic that looks for the signal to change state, from a 0 to a 1, and when this happens a flag bit is set - so even though the pulse is too short for SW to always catch it by polling the pin, the flag will remain set until your code clears it. interrupts do NOT have to be enabled for this logic to do its thing with the flagbit
heres what we did - dont let the intr names scare you off - this code is not using interrupts - only looking at those flag bits:
if (INTCON3bits.INT2IF) /*578 interrupt flag polling */
{
INTCON3bits.INT2IF=0; /*clear the intr flag*/
if (DirFilterL==2) DiSeeL=1; /*IR seen when intr2 3 times in a row*/
else DirFilterL = DirFilterL+1;
}
else
{
DiSeeL=0; /*turn it off when no intr flagbit is seen*/
DirFilterL=0;
}
INTCON3bits.INT2IF is the flag bit that gets set when pin rc_dig_in01 sees a change from a 0 to 1 - which means the sensor saw something
(INTR 2 is on rc_dig_in01 and INTR 3 is on rc_dig_in02 - dont ask why - thats the way the RC is wired :c)
the only problem we noticed is sometimes the IR sensors trigger when no IR is present - its a very short pulse, around 100uS - so we put a filter in the code which requires the flag to get set three times in a row - you see the flag, you clear it, then 26mS later you see it again, and clear it again, then 26mS you see it again and you say OK this is real - set the DiSEEL bit to one (thats the output from this code - we have two on our bot, one L and one R)
it works very well - our auton code uses fixed sensors to the left and right so we can tell when we are passing the beacon - then we stop and back up, and turn towards the ball (we use a yaw rate sensor to measure our turns)