Shop OBEX P1 Docs P2 Docs Learn Events
Shortening A Program — Parallax Forums

Shortening A Program

GuidoGuido Posts: 195
edited 2005-03-30 00:31 in BASIC Stamp
I am trying tricks to shorten my programs and have one I know can be done, but having problems getting it to work.
The Basic Format seems simple but the getting the results I can not seem to get..
Here we go! I have a Hex variable, 1 to 20. Depending on the number I want to run a certain subroutine equal to the number received.
Example if the Hex number is 15, I want to go to a subroutine and toggle an output 15 times....Is their an easy way to do this without listing every number with an "IF" and then? I am not sure if you could use a loop on this or not.
Thank You
Guido

Post Edited By Moderator (Jon Williams) : 3/28/2005 1:44:32 PM GMT

Comments

  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-03-28 15:45
    If these subroutines have to do more than just blink an LED the number of times specified, then

    '{$PBASIC 2.5}
    ON myVariable GOSUB sub0,sub1,sub2,sub3,.... etc

    sub0:
    ' stuff
    RETURN

    sub1:
    ' stuff
    RETURN

    etc.


    On the other hand, if each subroutine simply blinks the LED myVariable times, then you do not need separate subroutines to do that.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-03-28 15:46
    The "Lookup" and "LookDown" commands were made for this, as was the "ON...GOSUB".

    MAIN:

    MyHexVal = $F
    ON MyHexVal GOSUB Task_0, Task_1, Task_2, Task_3, Task_4, Task_5, Task_6,
    Task_7, Task_8, Task_9, Task_10, Task_11, Task_12, Task_13,
    Task_14, Task_15 ' 0 -> Task_0

    GOTO MAIN

    Note you could plug the SAME destination in each, and have that destination do:

    Task_X:
    · FOR I = 0 TO MyHexVal
    ··· HIGH LEDPin
    ··· PAUSE 500· ' 1/2 second on
    ··· LOW LEDPin
    ··· PAUSE 500· ' 1/2/ second off
    · NEXT
    · RETURN




    Post Edited (allanlane5) : 3/28/2005 3:50:18 PM GMT
  • GuidoGuido Posts: 195
    edited 2005-03-30 00:31
    Allan and Tracy,

    Thank You very much!!!! Allan was right on the money, I just thought I could not do a Hex variable loop. Well now I know I can. Tracy, The lookup table is fine the problem if your dealing with 20 different variables, then end requires to many goto statements....Thanks to you both for your help.

    Guido
Sign In or Register to comment.