Shop OBEX P1 Docs P2 Docs Learn Events
Conversion Commands — Parallax Forums

Conversion Commands

BuStOsAvIcHBuStOsAvIcH Posts: 1
edited 2008-07-24 15:26 in BASIC Stamp
Hello, I am currently working on my senior design project with some colleagues and we have hit a stopping point in regards to programming. We need to convert from ASCII to decimal do a math formula and then convert from decimal back to ASCII in order to output it to our LCD screen. We're using the Super Carrier Board and obviously PBASIC.
We've spent the past few days trying to find this out so any help would be very helpful. Thanks.

Edit: The information is coming from an ultrasonic sensor (Maxbotix LV-MaxSonar-EZ1 Sonar Range Finder). We need to take the ASCII code, which is going to be a distance in inches, convert it to a decimal, do an equation to get the distance in feet and then convert that back to ASCII so we can display it on our LCD (Parallax 2x16 Serial LCD (Backlit)).

Post Edited (BuStOsAvIcH) : 7/24/2008 3:35:51 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-24 12:56
    The "formatters" that are part of the I/O statements like SERIN and SEROUT are the usual way to do this. Look up DEC in the PBasic manual index.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-07-24 15:26
    The formatters are also part of the LCDOUT command that is available on the BS2p, 'pe and 'px. But not on the other Stamps. What Stamp do you have plugged into the super carrier board? The approach to data output will also depend on whether you have a parallel or serial LCD module, because the serial modules usually have some special formatting commands. In PBASIC in general, the DIG operator is the one you need to read about in the Stamp manual.

    x VAR Word
    idx VAR Nib
    x = 12345   ' a variable
    FOR idx=4 TO 0
      DEBUG x DIG idx + 48  ' extracts digits MSB first as ascii codes
    NEXT
    



    If your data comes in through SERIN, then the format modifiers like DEC are the way to transform ascii on the fly into a number suitable for math operations. On the other hand, if you have a string like "12345" in memory, a subroutine like the following will convert it to a plain number:

    mystring DATA "12345"
    x VAR Word
    idx VAR Nib
    char VAR byte
    x = 0  ' initially
    FOR idx=0  TO 4
      READ mystring+idx,char
      x = x * 10 + (char - 48)
    NEXT
    DEBUG ? x
    



    Of course, the exact form depends on the details of where the ascii data is coming from.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.