View Full Version : Negative Help!

Commander_Bob

04-17-2007, 11:00 AM

I have been trying to get my 3 Axis accelerometer to work with positive and negative numbers and use commands like > or < but if I do something like 1 > -1 it says false and when I use debug it says -1 = 65535. How can I make the BS2 know it is a negative number and use this code?

IF··Gforce < -110 THEN

HIGH 3

ELSE

LOW 3

ENDIF

Thanks

Mike Green

04-17-2007, 11:11 AM

The Stamp's native arithmetic is unsigned 16-bit. Addition and subtraction work with signed or unsigned numbers, but greater and less than are backwards. For your example, you could try:

if (Gforce < 0) and (abs(Gforce) > 110) then

high 3

else

low 3

endif

metron9

04-17-2007, 12:29 PM

Some additional negative number information.

gforce VAR Word

FOR gforce= 32758 TO 32778

DEBUG DEC gforce," = "

IF gforce>32768 THEN DEBUG "-" ELSE DEBUG "+"

DEBUG DEC ABS(gforce),CR

NEXT

The numbers in a word variable range from 0 to 65535. When you want to think about negative numbers using a word variable you split the 65535 in half. You then have 32768 positive numbers and 32768 negative numbers. Remember zero is one of the positive numbers so really 65536/2 so the range is 0 thru 32767 positive and 32768 thru 65535 represent negative numbers.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Think Inside the box first and if that doesn't work..

Re-arrange what's inside the box then...

Think outside the BOX!

Commander_Bob

04-18-2007, 05:16 AM

Thanks. I tried this and it works too.

sign = zg.BIT15

IF (sign = 0) AND (ABS(zg) > 0) THEN

HIGH 3

ELSE

LOW 3

ENDIF

Mike Green

04-18-2007, 05:28 AM

Yes, your statement is true for numbers from 1 to 32767 and false for zero and negative numbers.