Shop OBEX P1 Docs P2 Docs Learn Events
Displaying negative numbers — Parallax Forums

Displaying negative numbers

NewzedNewzed Posts: 2,503
edited 2005-07-11 21:31 in General Discussion
I am trying to get my LCD to display negative numbers indicating the position of the X or Y drive on my MF70 mill.

I have the origin set at 0, 0.· If I stay to the right of 0, the display is correct.· I can move .400 left - display = .400 - then move right .100 and display = .300.· All OK so far.

However, if I am at origin and I move the table right .100, then the display says 65436 instead of -.100.

Position is calculated by 0 -· dis, where dis is the travel after a go right command execution, or by 0 + dis after a go left command execution.
I am using SDEC modifiers.

Any suggestions?

Sid

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-11 17:58
    As you frequently suggest, Sid, perhaps you should post your code. SDEC will work in the right context. You can also do it yourself: look at BIT15 of your value; if it's "1" then the value is negative so you display "-". After that you take the ABS[noparse][[/noparse]solute] value you're working with and display it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • NewzedNewzed Posts: 2,503
    edited 2005-07-11 20:07
    The complete code for this program is huge so I'm reluctant to post the whole thing.· However, here afe snippets covering the matter under discussion.

    This is the routine that calculates travel.· It is shown for just 360 degrees:

    diffd = f
    IF f = 360 THEN
    dis = ABS diffd*/$0046 + 2
    ENDIF

    dis always = 1000

    to display it on the LCD, I write:

    SEROUT lcd, baud, 10, [noparse][[/noparse]"B", "Travel: ", SDEC dis/1000,".", DEC3 dis, " out", 13]

    To calculate the total - or net - travel (disy) from origin - 0, 0 - I write:

    disy = disy - dis

    To display, I write:

    SEROUT lcd, baud, 10, [noparse][[/noparse]"C", "Total travel: ", SDEC disy/1000,".",DEC3 disy, 13]

    The LCD displays 65.436.

    If I change the display instruction to:

    SEROUT lcd, baud, 10, [noparse][[/noparse]"C", "Total travel: ", SDEC disy, 13]

    the LCD displays -100.· I can put a dot in front of it and get .-100 but what I want the display to say is -.100.· I have no problems as long as disy is =>0.

    Is this enough of the code?

    Sid
    ·
  • NewzedNewzed Posts: 2,503
    edited 2005-07-11 20:49
    Small correction to the above post - dis = 100 not 1000.

    Sid
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-11 21:31
    You cannot do division on negative numbers in PBASIC.· Update your code like this:

    · temp = ABS dis
    · SEROUT Lcd, Baud, 10, [noparse][[/noparse]"B", "Travel: ", " " + (dis.BIT15 * 13), temp/1000, ".", DEC3 temp, " out", CR]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.