Shop OBEX P1 Docs P2 Docs Learn Events
Counting Negative Numbers — Parallax Forums

Counting Negative Numbers

DmageeDmagee Posts: 20
edited 2012-01-11 12:07 in BASIC Stamp
How can I make this code count negative? Once the count get to zero I want it to keep counting in the negative direction.







  ' {$STAMP BS2}' {$PBASIC 2.5}D_Set            VAR   Word                 ' Defalt SetpointFlag             VAR   Bit                  ' Flag that button was pressedFlag2            VAR   Bit                  ' Flag that button was pressedMoveTo           CON   2                    ' Curser positionPRESSED          CON   0                    ' Push Button statepb1              PIN   2                    ' Push Button 0nepb2              PIN   3                    ' Push Button twoD_SET = 10                                  ' Defalt SetingDODEBUG MoveTo, 0, 1DEBUG "Defalt Setpoint... "  DEBUG DEC2 (D_Set ), CRGOSUB PBLOOPPB:IF (pb2 = PRESSED) THEN                     ' pb2 pressedFlag2 = 1                                   ' Flag2 setENDIFIF (pb2 = 1) AND (Flag2 = 1) THEN           ' pb not pressedD_Set = D_Set - 1                           ' True -1Flag2 = 0                                   ' Reset Flag2ENDIFIF (pb1 = PRESSED) THEN                     ' pb1 pressedFlag = 1                                    ' Flag1 setENDIFIF (pb1 = 1) AND (Flag = 1) THEN            ' pb not pressedD_Set = D_Set + 1                           ' True +1Flag = 0                                    ' Reset Flag2ENDIFRETURN

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-01-10 18:24
    Your problem is that DEC deals with unsigned numbers. A -1 will display as 65535, -2 will display as 65534, etc. You can use SDEC instead which will include a minus sign if the number is between 32768 to 65535. If you want to used fixed formatting (like DEC2), you can do the same thing with SDECn, but be sure to allow for the minus sign.
  • DmageeDmagee Posts: 20
    edited 2012-01-10 19:42
    Ok, the SDEC worked in the Debug Terminal but how do I get the stamp to know that it's a negative number?
  • FranklinFranklin Posts: 4,747
    edited 2012-01-10 21:18
    but how do I get the stamp to know that it's a negative number?
    There are no negative numbers to the stamp, only to the human on the outside. a number above 32K is considered negative by the program (counting down from 64K as Mike stated. You can Google signed integers
  • Mike GreenMike Green Posts: 23,101
    edited 2012-01-10 21:55
    In 2's complement arithmetic (that all microcomputers use these days), addition and subtraction works the same regardless of whether you're using positive or negative numbers. The main difference is in comparisons where you use the carry out of the most significant bit to indicate unsigned less than while you use a combination of the sign bit and the carry for signed less than. Since the carry bit isn't directly accessible in the Stamps, you have to do signed comparisons with multiple IF statements separating out the various combinations of negative and positive numbers (positive < positive, negative < negative). Note that negative < positive is always true while positive < negative is always false.
  • DmageeDmagee Posts: 20
    edited 2012-01-11 12:07
    Thanks for all of the help, I've got every thing working the way I want it to now.
Sign In or Register to comment.