Shop OBEX P1 Docs P2 Docs Learn Events
quick question about IF...THEN and negative values — Parallax Forums

quick question about IF...THEN and negative values

kingspudkingspud Posts: 128
edited 2007-06-13 18:19 in BASIC Stamp
Hello all,

In the following code I have an IF...THEN statement that

button_press:
· BUTTON 12,0,255,0,btn,1,press·· ' button [noparse][[/noparse] subtract 20 ]
GOTO button_press

press:············· 'Press button [noparse][[/noparse]1] on PIN 5 and goto Begin_301
··· score = score - 20
····· DEBUG SDEC score, CR···
··· IF score < 0 THEN
···· GOTO reset
··· ENDIF
··· GOSUB DecodeMode
··· Digit0 = score DIG 0 + $0100
··· Digit1 = score DIG 1 + $0200
··· Digit2 = score DIG 2 + $0300
··· GOSUB board_display:
··· GOTO button_press

DecodeMode:
· LOW Load
· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$090F\16]
· HIGH Load
· RETURN
board_display:
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit0\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit1\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit2\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit3\16]
··· HIGH Load
··· LOW Load
··· SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit4\16]
··· HIGH Load
··· PAUSE 250
RETURN

I have a button that when pressed it subtracts 20 from a score of 301 and shows it on the screen and three 7-segment displays
as you continue the press the button the score gets lower by 20 until it gets to 1 and the next time you press the button
the screen quickly shows -19 and then goes to a value of 65517!
I'm not sure what is wrong?
score is set to "word" size

Thanks for any help

Joe





▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“Intellectual growth should commence at birth and cease only at death”
Albert Einstein

Comments

  • FranklinFranklin Posts: 4,747
    edited 2007-06-13 01:52
    I don't see reset

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • kingspudkingspud Posts: 128
    edited 2007-06-13 03:32
    Here is the entire program

    ' {$STAMP BS2px}
    ' {$PBASIC 2.5}

    '
    [noparse][[/noparse] I/O Definitions ]
    '0 sets pin as input
    'button PIN 0 makes the word button = pin 0

    DIRS = $000E 'pins 1 2 3 output,all other pins outputs
    DOUT PIN 1 'pin 1 of stamp to pin 1 of 7219
    Load PIN 2 'pin 2 of stamp to pin 12 of 7219
    CLK PIN 3 'pin 3 of stamp to pin 13 of 7219

    '
    [noparse][[/noparse] Constants ]

    IsLow CON 0
    IsHigh CON 1



    '
    [noparse][[/noparse] Variables ]
    Digit0 VAR Word
    Digit1 VAR Word
    Digit2 VAR Word

    games VAR Byte
    Value VAR Nib
    score VAR Word
    player VAR Byte
    change VAR Byte

    '
    [noparse][[/noparse] Variables ]
    btn VAR Bit
    total VAR Byte
    temp VAR Byte

    '
    [noparse][[/noparse] Initialization ]
    Digit0 = $010F
    Digit1 = $020F
    Digit2 = $030F

    Value = 0
    total = 0
    games = 0
    change = 0
    score = 301

    '
    [noparse][[/noparse] Main Program ]
    main:
    GOSUB DecodeMode
    GOSUB ShutDownMode
    GOSUB ScanLimit
    GOSUB Intensity


    '----[noparse][[/noparse] stuff ]

    button_press:

    BUTTON 12,0,255,255,btn,1,press ' button [noparse][[/noparse] 20 ]

    DEBUG DEC score," ",CR
    GOTO button_press

    '
    [noparse][[/noparse] Subroutines ]

    press: 'Press button on PIN 5 and goto Begin_301
    score = score - 20
    DEBUG SDEC score, CR
    IF score < 0 THEN
    GOTO reset
    ENDIF
    'GOSUB DecodeMode
    Digit0 = score DIG 0 + $0100
    Digit1 = score DIG 1 + $0200
    Digit2 = score DIG 2 + $0300
    GOSUB board_display:
    GOTO button_press

    GOTO button_press

    '
    [noparse][[/noparse] GOSUB RESET ]
    reset:
    score = 0
    Digit0 = $010F
    Digit1 = $020F
    Digit2 = $030F
    GOSUB board_display:
    GOTO reset
    '
    [noparse][[/noparse] GOSUB BEGIN_401 ]





    '
    [noparse][[/noparse] Subroutines ]
    'Decode mode for no decoding to BCD
    'Least sig nibble:
    '0 = no decode
    '1 = decode Digitit 0
    'F = decode Digitits 0-3
    'FF = decode Digitits 0-7

    DecodeMode:
    LOW Load
    SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$090F\16]
    HIGH Load
    RETURN
    'Set shutdown mode so device is active
    ShutDownMode:
    LOW Load
    SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0C01\16]
    HIGH Load
    RETURN

    'Set scan limit mode for three Digitits
    ScanLimit:
    LOW Load
    SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0B02\16]
    HIGH Load
    RETURN
    'Set intensity of LEdDs
    'LSNibble 0 = min intensity and F = max intensity
    Intensity:
    LOW Load
    SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]$0A09\16]
    HIGH Load
    RETURN

    board_display:

    LOW Load
    SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit0\16]
    HIGH Load
    LOW Load
    SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit1\16]
    HIGH Load
    LOW Load
    SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]Digit2\16]
    HIGH Load

    PAUSE 250
    RETURN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    “Intellectual growth should commence at birth and cease only at death”
    Albert Einstein

  • kingspudkingspud Posts: 128
    edited 2007-06-13 04:00
    I found some information concerning negative numbers which states:

    Although -99 is obviously less than 100, the program will say it is greater. The problem is that -99 is internally represented as the two's complement value 65437, which (using unsigned math) is greater than 100. This phenomena will occur whether or not the negative value is a constant, variable or expression.

    Maybe its just me but this still doesn't help me and I'm not sure how to check for a number less than 0!

    Anyone?????

    Joe

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    “Intellectual growth should commence at birth and cease only at death”
    Albert Einstein
  • FranklinFranklin Posts: 4,747
    edited 2007-06-13 04:14
    BUTTON 12,0,255,255,btn,1,press ' button [noparse][[/noparse] 20 ]
    
    
    

    In your case button is on pin 12 (not 5) and btn needs to be a byte variable (not bit) and cleared before use.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-06-13 06:03
    Joe -

    In addition to what Stephen found, the following routine is an infinite loop:

    reset:
    score = 0
    Digit0 = $010F
    Digit1 = $020F
    Digit2 = $030F
    GOSUB board_display:
    GOTO reset

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • kingspudkingspud Posts: 128
    edited 2007-06-13 13:52
    Hello all,

    I know the loop is infinite but it is only for a test program and it gets hit once so it will stop the program.

    The main issue is the negative number and why I can't seem to see it in the IF statement.

    This is what I need help on!

    Can we focus on this issue please.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    “Intellectual growth should commence at birth and cease only at death”
    Albert Einstein
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-06-13 15:00
    Yes, the BS2 "comparison" math is not 'signed' math.

    Now, "2's complement" format (the one the BS2 uses) inverts all the bits and adds 1.

    So, for example, the value 10. In binary, this is 0000 1010. To get negative 10, we invert -- 1111 0101, then add 1 -- 1111 0110. And in hexadecimal, 1111 0110 is $F6, and in decimal 246.

    Now, you're doing "Word" quantities (16 bits) so -10 would be $FFF6.

    The bottom line of this example is -- positive numbers go from 0 to 65535. Negative numbers go from -32768 to 0 to 32767. And negative numbers always have the most significant bit set.

    So, if you know you're doing signed math, you can check if the most significant bit is set on a number -- that means it's negative.
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2007-06-13 15:57
    board_display:
      IF score.bit15 THEN  ' it is negative
        SHIFTOUT DOUT, CLK, MSBFIRST,[noparse][[/noparse]minus\16]   ' minus is the constant that puts "-" on the display
        score = ABS score  ' absolute value
      ENDIF
      ' ... continue with board_display routine as written
      RETURN
    
    



    If you want to display the negative numbers on the debug screen, just use
    DEBUG SDEC score ' signed decimal

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • FranklinFranklin Posts: 4,747
    edited 2007-06-13 17:17
    And as to the broken code. How do you expect us to test your code if it does not work? It's better if you post working code or at least tell us it works in a certain way if you want constructive responses.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • kingspudkingspud Posts: 128
    edited 2007-06-13 18:19
    Hello all,

    Forgive me for my brash response; I didn't realize that people would have a MAX7219 chip, the Professional Development board and a Basic Stamp 2 chip... I just thought people or administrators would be able to quickly answer the question without running the code.

    Sometimes I get caught up in the coding and I hit a wall that stops my momentum right in my tracks… At these times I just ask a quick question hoping for a fix or a sample code from someone. I forget that people may want to try the code out for themselves.

    I have to keep this in mind!!!!

    I agree you Stephen, I should have produced a complete and correct program before asking for help.

    This was my bad and I will not do it in the future.

    Thanks for everyone’s help and I do appreciate it!

    Joe

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    “Intellectual growth should commence at birth and cease only at death”
    Albert Einstein
Sign In or Register to comment.