Shop OBEX P1 Docs P2 Docs Learn Events
Project question involving LOOKUP commands — Parallax Forums

Project question involving LOOKUP commands

AndyC1986AndyC1986 Posts: 9
edited 2011-12-02 12:06 in BASIC Stamp

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-11-30 16:20
    Your program is doing exactly what you've programmed it to do ... display the whole table no matter what you've entered. You've gone to all the trouble of entering a number and displaying stuff including the value of index which is really undefined at that point even though it'll display as zero since all variables are set to zero when the Stamp starts up. Then you go and set index to first 1, then 2, then 3, etc. to 26, look up the corresponding letter and display it. I don't know what you were thinking when you put the FOR / NEXT loop there. You might review the various statements you're using. The Basic Stamp Syntax and Reference Manual is a good place to look. Also, What's a Microcontroller? is a good introduction to many of the Stamp Basic statements.

    Try taking out the FOR statement and the NEXT statement and see what happens.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-11-30 16:43
    Hi Andy, I'm sure you are going to take a look at the manual and you will see that your code is not far off from being right. Anyway I'll pitch in with a couple of pointers,

    1 make your lookup text one continuous string eg: "a through to z"
    2 the letter is going to end up in the variable "value" , to display it you need :- DEBUG ASC? value and not DEBUG DEC value as you have it now


    Jeff T.
  • AndyC1986AndyC1986 Posts: 9
    edited 2011-11-30 20:52
    If i put the numbers [97-122] (ASCII, a-z) in the LOOKUP command how would i then convert the number i select to display the letter of the ASCII? I tried the DEBUG ASC? value and still giving me no value. I know its one little thing but i dont know what it is to get the damn letter out of the LOOKUP index.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-11-30 21:10
    There are no character values in Stamp Basic. They're numbers like any other value. When you write "z", the compiler just substitutes the number that corresponds (in ASCII) to the character "z". What determines what you see on the display is the formatter. If you don't use a formatter (like DEC or HEX or BIN), the number is just sent as a byte to the PC where it's displayed as a character. If you use DEC, the number is converted to a sequence of ASCII digits corresponding to the numeric value so you can read it on the PC.

    The LOOKUP statement in your first message just takes an index with values from 1 to 26 and produces a numeric value that's the ASCII code for the letters "a" through "z". If this value is sent as a byte to your debug window on the PC, you should see the character as such.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-12-01 05:58
    Hi, breaking the LOOKUP instruction down some :


    LOOKUP

    ok here is the instruction

    index

    this value holds the character position, it is zero indexed meaning if you place a value of zero in this variable it will point you to the first value in your lookup table which in this case is "a"

    ["a","b"]

    inside the brackets we have the values of the lookup table

    Value

    this variable is going to hold the value in the lookup table pointed to by "index" , so if I type in index=1 then "value" will be equal to "b"

    I think what may be tripping you up is the different formatting options. The variables "index", "value" and the contents of the lookup table are all numbers. In our programs we use DEC,HEX,ASC etc. to convert those byte sized values into ASCII characters. We are doing this because that is the way our display (DEBUG screen) receives and transmits its alpha numeric information and also how our programming IDE helps make our code a little more readable.

    For example when you press a 1 on you PC's keyboard the DEBUG app transmits the ASCII code value 49 to the Stamp. Obviously we dont want 49 we want our 1, so we use a formatter at the Stamp end to translate 49 into a value of 1, here it is, DEBUGIN DEC index (notice I need to place this input number into index).

    Ok so now our lookup table has an index value of 1 to reference an alphabetic character, we have an index value of 1 so that would be character "b" which is placed in the variable "value". But "value" does not really contain "b" it contains a number that represents "b", that number is a decimal value of 98.

    We are lucky because the DEBUG app is going to translate our number 98 into a character "b" before it displays it, because by default that is what DEBUG does, it takes ASCII character codes and displays them as alpha numeric characters.

    There are a couple of options for sending the 98 to DEBUG (remember the 98 is within the variable "value").

    1./ just send the number without any formatting :- DEBUG value
    2./ use the ASC? formatter to display a string :- DEBUG ASC? value

    Jeff T.
  • AndyC1986AndyC1986 Posts: 9
    edited 2011-12-02 10:29
    So I have gotten my program to work. However, it asks me to enter a number from 1-26. The index starts with 0. So really I am entering a number from 0-25. How do I change my index to read 1 = a, instead of 0 = a and 26=z instead of 25=z. In a nutshell can I make my index start at 1 instead of 0?
  • Mike GreenMike Green Posts: 23,101
    edited 2011-12-02 10:34
    Sorry for the mixup. Spin has a LOOKUP statement that starts its index at 1 while PBasic starts its index at 0. Just use LOOKUP index-1, ...
  • AndyC1986AndyC1986 Posts: 9
    edited 2011-12-02 12:06
    AHA! Mike, your help is greatly appreciated!
Sign In or Register to comment.