Shop OBEX P1 Docs P2 Docs Learn Events
why can I add VARs but not subtract? — Parallax Forums

why can I add VARs but not subtract?

epicjr77epicjr77 Posts: 29
edited 2009-10-30 01:45 in BASIC Stamp
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

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-10-30 01:45
    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
Sign In or Register to comment.