View Full Version : Negative Help!
04-17-2007, 12:00 PM
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
04-17-2007, 12:11 PM
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
04-17-2007, 01: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
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!
04-18-2007, 06:16 AM
Thanks. I tried this and it works too.
sign = zg.BIT15
IF (sign = 0) AND (ABS(zg) > 0) THEN
04-18-2007, 06:28 AM
Yes, your statement is true for numbers from 1 to 32767 and false for zero and negative numbers.