Shop OBEX P1 Docs P2 Docs Learn Events
Gosub to a variable ? — Parallax Forums

Gosub to a variable ?

Don MooreDon Moore Posts: 4
edited 2008-11-09 07:28 in BASIC Stamp
I'm very new to programming in for the BS2 and have run into a problem. I'm trying to use the GOSUB command but instead of an "address label" I'm trying to get it to read the value of a variable as the address label, and I'm getting an error. "Expected a label"

I'm trying to use it to look up a value to draw with a 7 LED display POV wheel.

I know there's an easier way to do it but the concept is below:

If I can't do it this way, is there a way to pipe my LOOKUP output into the GOSUB command directly instead of a variable (like is done with the OUTH after the lookup for a 7-seg LED)?

Thank you in advance.

~Don



displaychar        VAR      Byte                ' Letter to be drawn 

DO

FOR counter = 0 TO 6
    LOOKUP counter, [noparse][[/noparse] "A", "B", "C", "D", "E", "F", "G" ], displaychar       ' I'm just using this as test data.  Later it will be READ from a DATA string, which I'll want to be able to edit for various messages.
    GOSUB [color=#990000][b]displaychar[/b][/color]
    PAUSE 10
NEXT

LOOP

A:
OUTH = %0011111
OUTH = %0100100
OUTH = %1000100
OUTH = %0100100
OUTH = %0011111
RETURN
.
.
.
G:
OUTH = %0111110
OUTH = %1000001
OUTH = %1001001
OUTH = %1001001
OUTH = %0111010
RETURN

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,563
    edited 2008-11-09 05:40
    Don Moore,

    Check out the ON... GOSUB function...

    Syntax: ON Offset GOTO Address1, Address2, ...AddressN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-11-09 05:52
    No; you can't do that. A variable is not executable code. Whatever you GOSUB to must be executable code. It's as if you were trying to follow a recipe for Beef Stroganoff, and when you got to page 2, which should contain instructions, instead of paper with additional instructions on it you found a little pan of tangerine Jelly Bellies. Your cuisine would suffer.

    Whatever you GOSUB to should be a section of code that eventually executes a RETURN instruction to continue just after your original GOSUB.

    GOSUB is like a little side trip for your program. Your program uses GOSUB to take a little side trip, then returns to where it left off, to continue the main journey.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i
  • Beau SchwabeBeau Schwabe Posts: 6,563
    edited 2008-11-09 05:59
    Don Moore,

    Change your FOR/NEXT loop to look like this....

    FOR counter = 0 TO 6
           ON counter GOSUB A, B, C, D, E, F, G
           PAUSE 10
    NEXT
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Don MooreDon Moore Posts: 4
    edited 2008-11-09 06:10
    CARL:

    I understand that.. but what I'm trying to do is something to the sort of...

    MyMessage DATA "This message will be displayed 0" <<--- I want to be able to change this without reworking the entire program

    index VAR byte

    DO UNTIL displaychar = 0

    READ MyMessage + index, displaychar
    Gosub displaychar
    index = index + 1
    loop

    And I'm using GOSUB because I may have to jump to the letter E for example several times for my message...


    I didn't want to have to hardcode a bunch of GOSUB <address> manually every time I changed the displayed message.


    BEAU:
    Using the earlier suggestion, I could do a LOOKDOWN on the read "displaychar" to get an offset value (0-25 for A-Z) to use, then specify that on the {ON offset GOTO address1, address2....}. I just thought there might be a more direct approach.

    I'll give it a shot to see what I can come up with.


    Your new approach will work but it won't allow me to do content checking .. for example .. if the character is a "space" pause for 20ms instead of a GOSUB command. The ABCDE text is just something for me to get started with then build off of

    I think I'll have to do your earlier suggestion. The second suggestion you made however, is interesting, and gives me a few ideas for later.

    Thanks again.

    Post Edited (Don Moore) : 11/9/2008 6:16:10 AM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-11-09 07:28
    Why not parameterize your code entirely, so you don't need separate subroutines? viz:

    Ltr_A DATA %0011111, %0100100, %1000100, %0100100, %0011111
    ...
    Ltr_G DATA %0111110, %1000001, %1001001. %1001001, %0111010
    
    ...
    
    FOR counter = 0 to 6
      ptr = counter * 5 + Ltr_A
      FOR i = ptr TO ptr + 4
        READ i, char
        OUTH = char
      NEXT
      PAUSE 10
    NEXT
    
    
    


    Your program will be more compact this way, as well.

    -Phil
Sign In or Register to comment.