I reformatted your code's intentation
Code:
void main (void)
{
//SET UP
ANSEL = 0; //turn off all analog inputs
ANSELH = 0;
ANSELbits.ANS1 = 0; //RA1 setting to Digital
ANSELbits.ANS2 = 0; //RA2 setting to Digital
TRISD = 0b00000000; //Port D bits 7:0 are all outputs
LATD = 0b00000000; //turns off PORTD outputs, good start position
TRISC = 0b00000000; //Port C bits 7:0 are all outputs
LATC = 0b00000000; //turns off PORTC outputs, good start position
TRISAbits.TRISA0 = 1; //TRISA0 Input (IR Sensor)
TRISAbits.TRISA1 = 1; //TRISA1 Input (PIR Sensor)
TRISAbits.TRISA2 = 1; //TRISA2 Input (Switch)
// this code configures the display
WriteCmd ( 0x02 ); // sets 4bit operation
WriteCmd ( CLEAR_SCREEN);
WriteCmd ( FOUR_BIT & LINES_5X7 ); // sets 5x7 and multiline operation.
WriteCmd ( CURSOR_BLINK ); // blinks cursor
WriteCmd ( CURSOR_RIGHT ); // moves cursor right
while(1) // Start of user program
{
//Init ADC
ADC_Init();
ADC_Convert();
//LATA = ADRESH;
Delay10KTCYx(25);
DistanceValue = ADRESH;
Delay1KTCYx(1);
READING = (DistanceValue);
if (READING > 0b100110) // Binary Number is 38 which is Number coming
// from 500mV which is around 30cm away from Sensor
{
INTRUDERALERT();
}
READING = (DistanceValue);
if (READING < 0b100110)
{
SENTRYMODE();
}
}
if (PORTAbits.RA2 == 0 && READING > 0b100110) //Checks if Button is Pressed
{
TURNOFF();
}
}
It is pretty clear that your "Checks if Button is Pressed" is out side of the main 'while()' loop, and will not get run.