Shortening A Program
Guido
Posts: 195
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
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
'{$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
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
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