Why is this code not working? (Weather Vane)
Franklin
Posts: 4,747
' {$STAMP BS2e} ' {$PBASIC 2.5} _INIT CON $FFAA 'INITIALIZE COPROCESSOR _ERRORS CON $FE 'Get Error Flags _LOW CON $B0 'SET PIN OUTPUT LOW _HIGH CON $B1 'SET PIN OUTPUT HIGH _INPUT CON $B2 'SET PIN TO INPUT _INPUTP CON $B6 'SET PIN TO INPUT WITH PULLUP _PUSH CON $D0 'PUSH ONE BYTE ON STACK at the current sram pointer and increment pointer _POP CON $D1 'decrement sram pointer and POP ONE BYTE OFF STACK _ZSTACK CON $D2 'ZERO THE STACK POINTER _CLRBL CON $B7 'CLEAR THE BLATCH REGISTER _CLRCL CON $B8 'CLEAR THE CLATCH REGISTER _RPINSA CON $C0 'READ PINS 0...5 _RPINSB CON $C1 'READ PINS 6..11 _RPINSC CON $C2 'READ PINS 12..18 _PINSBL CON $C3 'READ LATCHED LOW PINS 6..11 _PINSCL CON $C4 'READ LATCHED HIGH PINS 12..18 _SECONDS CON $C5 'Read seconds _MINUTES CON $C6 'Read Minutes _HOURS CON $C7 'Read Hours _TIME CON $C8 'Read seconds,minutes,hours _ADC0 CON $A0 'READ ADC0 _ADC1 CON $A1 'READ ADC1 _ADC2 CON $A2 'READ ADC2 _ADC3 CON $A3 'READ ADC3 _ADC4 CON $A4 'READ ADC4 _ADC5 CON $A5 _WRITEFLASH CON $D3 'write 5120 bytes from SRAM to FLASH memory _READFLASH CON $D4 'Read 5120 bytes from FLASH to SRAM memory _Readsram CON $D5 'Read a byte at the current sram pointer and increment pointer _setstack CON $D6 'Set the Sram pointer _EWRITE CON $D7 'wRITE A BYTE TO EEPROM _EREAD CON $D8 'Read a byte from EEPROM vane1 VAR Word vane2 VAR Word page VAR Byte TEMP VAR Word BUFFER VAR Byte Seconds VAR Byte Minutes VAR Byte Hours VAR Byte adcval0 VAR Word adcval1 VAR Word delta VAR Word bearing VAR Word ad1 VAR Byte ad0 VAR Byte x VAR Byte DEBUG "CO-PRO-M48 WeatherVane Test Program",CR PAUSE 1000 delta = 700 'delta is the overlap between wipers readvane: GOSUB adc DEBUG DEC adcval0,",",DEC adcval1,",",DEC bearing,CR ' [color=red]'CODE FROM FANTASTICELECTRONICS.com weather vane kit ' ' Decide IF we should update the delta value. IF adcval0 > 5120 - 2 * delta AND adcval0 < 4920 AND adcval1 < 2 * delta AND adcval1 > 200 THEN delta = (9 * delta + (adcval1 + 5120 - adcval0) / 2) / 10 ELSEIF adcval1 > 5120 - 2 * delta AND adcval1 < 4920 AND adcval0 < 2 * delta AND adcval0 > 200 THEN delta = (9 * delta + (adcval0 + 5120 - adcval1) / 2) / 10 ENDIF[/color] [color=red]' Decide IF we have a valid bearing off of one of the wipers. IF (adcval0 > delta AND adcval0 < 5120 - delta) AND (adcval1 > 5120 - delta OR adcval1 < delta) THEN bearing = 180 * (adcval0 - delta) / (5120 - 2 * delta) ELSEIF (adcval1 > delta AND adcval1 < 5120 - delta) AND (adcval0 > 5120 - delta OR adcval0 < delta) THEN bearing = 180 + 180 * (adcval1 - delta) / (5120 - 2 * delta) ENDIF ' 'CODE FROM FANTASTICELECTRONICS.com weather vane kit ' [/color]PAUSE 5000 GOTO readvane adc: adcval0 = 0 adcval1 = 0 SHIFTOUT 0,1,1,[noparse][[/noparse]_ADC0] SHIFTIN 0,1,0,[noparse][[/noparse]adcval0] adcval0 = adcval0 * 20 PAUSE 100 SHIFTOUT 0,1,1,[noparse][[/noparse]_ADC1] SHIFTIN 0,1,0,[noparse][[/noparse]adcval1] adcval1 = adcval1 * 20 RETURN
Can someone look at the highlighted code and tell me why it is not resolving bearing and why it never changes delta. the values for adcval range from about 20 to 5100.
If you need more info just ask, Thanks
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
Comments
IF adcval0 > 5120 - 2 * delta AND adcval0 < 4920 AND adcval1 < 2 * delta AND adcval1 > 200
needs some Parentheses worked in (as similar lines have.)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen