I am having problems with this code. I am sure someone much smarter than I am will figure this out quickly. I am using a reed relay as an indicator of PortB.6 = 1. Because of the loop the reed relay is buzzing because it is switching on and off. Obviously my code is not correct. I need the relay to be latched on. Can someone point me in the right direction. I have spent more than 36 hrs and now I need the forums help.

' 10-bit A/D conversion
' Connect analog input to channel-0 (RA0)
DEFINE LCD_DREG PORTB 'LCD data port
DEFINE LCD_DBIT 0 'LCD data starting bit 0 or 4
DEFINE LCD_RSREG PORTB 'LCD register select port
DEFINE LCD_RSBIT 4 'LCD register select bit
DEFINE LCD_EREG PORTB 'LCD enable port
DEFINE LCD_EBIT 5 'LCD enable bit
DEFINE LCD_BITS 4 'LCD bus size 4 or 8
DEFINE LCD_LINES 1 'Number lines on LCD
'DEFINE LCD_COMMANDUS 2000 'Command delay time in us
'DEFINE LCD_DATAUS 50 'Data delay time in us

adval var word 'Create adval to store result

TRISA = %11111111 ' Set PORTA to all input
TRISB = %00000000 ' Set PORTB to all output
ADCON1.7=1 ' RIGHT justify result
ADCON0 = %11000001 ' Configure and turn on A/D Module
Pause 500 ' Wait .5 second

' Define ADCIN parameters

DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 10 ' Set sampling time in microseconds


loop: ADCON0.2 = 1 'Start Conversion

notdone: pause 5
if ADCON0.2 = 1 Then notdone 'wait for low on bit-2 of ADCON0, conversion finished

adval.highbyte = ADRESH 'move HIGH byte of result to adval
adval.lowbyte = ADRESL 'move LOW byte of result to adval

Lcdout $fe, 1 'Clear screen
Lcdout "ADC Value: ", DEC adval 'Display the decimal value

if adval >500 and adval<600 then
PortB.6 = 1
endif
Pause 100 'Wait .1 second
Goto loop 'Do it forever

End