PDA

View Full Version : why can I add VARs but not subtract?



epicjr77
10-30-2009, 08:59 AM
Hello I am expecting to get a value of 0 (or close to it possibly a negative number) for my I and j vars. For some reason I get a value around 65000 i=x-xzero j=y-yzero but when I use addition i=x+xzero j=y+yzero I get reasonable numbers

any Idea why? OR how can I fix this?

Also I only want to go through the "calibrate" process once then do the loop portion. Is this the best way to do so?




' {$STAMP BS2}
' {$PBASIC 2.5}

x VAR Word
y VAR Word
xzero VAR Word
yzero VAR Word
pulsecount VAR Byte
i VAR Word
j VAR Word

'-------------------------calibrate---------------------------------------------------------
FOR pulsecount = 0 TO 1
PULSIN 6,1,x
PULSIN 7,1,y
xzero=x
yzero=y
DEBUG CLS,?xzero ,?yzero
PAUSE 2000
NEXT

'------------------------------------RUN-----------------------------------------
DO
PULSIN 6,1,x
PULSIN 7,1,y

i=x-xzero
j=y-yzero

DEBUG CLS,?x,?Y
PAUSE 1000
DEBUG CLS,?i,?j
PAUSE 1000
LOOP

Phil Pilgrim (PhiPi)
10-30-2009, 09:45 AM
All math in PBASIC unsigned. This means the a value like -1 = $ffff will print out as 65535. There's an out, however. Instead of using the ? prefix in your DEBUG statement, use SDEC instead. This will interpret the number as a signed, twos-complement value for output.

-Phil