variables and gosubs???


Closed Thread
Results 1 to 39 of 39
  1. #1
    Join Date
    Oct 2006
    Posts
    14

    Default variables and gosubs???

    Hi guys,

    i've never used varibles in pbp before, so im trying to do 2 things here:


    what i want to do is this:

    Code:
    main:
    
    i = 0
    while(i < 10){
    
    Variable = i
    
    
    gosub ("subName" + Variable)
    
    i = i  +1
    
    }
    
    goto main
    
    
    subName1:
    
    return
    
    
    
    subName2:
    
    return
    
    
    subName3:
    
    return
    
    
    
    subName4:
    
    return

    its a bit of a mix of picbasic, and php, but im sure someone will get what im trying to do

    Is it possible?

    Thanks very much.

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi guest_05,

    You can use the BRANCH or BRANCHL command for that.

    Something like this...
    Code:
    i        VAR BYTE
    Variable VAR BYTE
    
    main:
        i = 0
        while(i < 5)
            Variable = i
            gosub SubSwitcher
            i = i  +1
        wend
    goto main
    
    
    SubSwitcher:
        BRANCHL  variable,[subName0, subName1, subName2, subName3, subName4]
    return
    
    
    subName0:
    
    return
    
    
    subName1:
    
    return
    
    
    subName2:
    
    return
    
    
    subName3:
    
    return
    
    
    subName4:
    
    return
    DT

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    DT, you are my teacher and I learn a lot from you here. With all my respect to you, please allow me to make a point;
    BRANCHL will cause the program to jump. Thus, "return" with a "subroutine" will not work.

    I made my point as below.


    Code:
    i   VAR BYTE
    
    main:
        FOR i = 0 to 4
            BRANCHL  i,[subName0, subName1, subName2, subName3, subName4]
            Sayzers_Point:  'continue from where we were.
        NEXT i
    GOTO main
    
    
    subName0:
    
    GOTO Sayzers_Point
    
    
    subName1:
    
    GOTO Sayzers_Point
    
    
    subName2:
    
    GOTO Sayzers_Point
    
    
    subName3:
    
    GOTO Sayzers_Point
    
    
    subName4:
    
    goto Sayzers_Point

    or another example could be as below.

    Code:
    i   VAR BYTE
    
    main:
    
        FOR i = 0 to 4
    
            SELECT CASE i
    
               CASE 0
                    'do something
               CASE 1
                    'do something else
               CASE 2
                    'do something more
               CASE 3
                    'do something interesting
               CASE 4
                    'do something bro
             END SELECT
    
        NEXT i
    
    GOTO main


    ---------------------------------
    Last edited by sayzer; - 18th November 2006 at 18:10.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Nope..it will perfectly work dude and what Darrel did it's really clever.

    When you'll meet the Return, it will automatically RETURN at the main... but not at SubSwitcher.

    It's a kind of On YourVar Gosub.

    Try it...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    That's why the GOSUB was placed inside the while loop.

    That gosub calls the SubSwitcher: which then redirects to the appropriate subroutine. Then the subroutine does the return.

    The return in the SubSwitcher: is only there in case the variable is larger than the number of available subroutines. Otherwise, it would run subName0 which would be in error.

    The subroutines don't return to the SubSwitcher, they return back to the while loop.

    HTH,
    DT

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    How dare you reply exactly at the same time
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    That seems to happen alot with us Steve.

    I should also note here, that Sayzer's examples will work just fine too.

    Just different ways to skin a dog.
    (sorry, I'm a cat person)
    <br>
    DT

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    here we say... every road leads to Roma.

    Why? i don't know... but why skin a cat as well
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  9. #9
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Since I am still learning,

    with the information above, will the "return" go back to FOR loop in the example below ?
    I guess it will, but I also do not like to guess.

    Code:
    i   VAR BYTE
    
    main:
    
        FOR i = 0 TO 4
            BRANCHL i,[subName0, subName1, subName2, subName3, subName4]
        NEXT i
    
    GOTO main
    
    
    subName0:
    
    RETURN
    
    
    subName1:
    
    RETURN
    
    
    subName2:
    
    RETURN
    
    
    subName3:
    
    RETURN
    
    
    subName4:
    
    RETURN


    mister_e, you are also my teacher as you know!
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    In your example the return won't work as Branch(l) use Goto. It's just a Select CAse goto.. or kind of

    I wonder why Branch(l) don't use gosub so far... easy to modify the macro but...
    Last edited by mister_e; - 18th November 2006 at 18:34.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e
    In your example the return won't work as Branch(l) use Goto. It's just a Select CAse goto.. or kind of

    I wonder why Branch(l) don't use gosub so far... easy to modify the macro but...

    Did you mean the example in post #9 ?


    -----------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  12. #12
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Yes indeed... post #9

    The various BRANCH?xxx and BRANCHL?xxx macros, mess around BIT?GOTO and L?GOTO

    EDIT: also ... TABLE?C
    Last edited by mister_e; - 18th November 2006 at 18:58.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  13. #13
    Join Date
    Oct 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Smile

    Ahhhhhh!!! what a cleaver way to do it!!! thanks so much Darrel.

    I would never have thought about using an array like that

    Cheers.

    Now i just have to find out if im using the code the right way lol

    (p.s based on the code you gave me, how much more code could a 16f628 allow?)

    and one more question, im trying to use this code to make a sort of clock.

    i'm using the pins LOW to make the circuit, but i dont really want to use HIGH to stop the circuit, is there anway to just turn the pin off, like when the program is run at the start?
    Thanks again

    Ta.
    Last edited by guest_05; - 18th November 2006 at 19:00.

  14. #14
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by guest_05
    ....

    (p.s based on the code you gave me, how much more code could a 16f628 allow?)

    Ta.


    DT's code is 58 words; you have about 2048 words in 628.


    mister_e, in this case, can I make a conclusion as

    "return" in a subroutine referred by a BRANCHL inside a WHILE will take the subroutine back to that WHILE statement.

    But, this is not true for a FOR loop.


    Am I right?


    -------------------------
    Last edited by sayzer; - 18th November 2006 at 19:01.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  15. #15
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Nope... a return will never work with Branch(L) anyway, as Branch(l) use goto.

    Branch index,[Label1, Label2,Label3] is something like ...
    Code:
    Select CASE index
        Case 1 : goto Label1
        Case 2 : goto Label2
        Case 3 : goto Label3
        End Select
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  16. #16
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor
    ...

    The subroutines don't return to the SubSwitcher, they return back to the while loop.

    ...

    If there is no typo here, did I misunderstand this statement then ?



    -----------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  17. #17
    Join Date
    Mar 2005
    Location
    Iowa, USA
    Posts
    216


    Did you find this post helpful? Yes | No

    Question

    I'm a little confused as well. I guess as a test, you can put a statement under the branch instruction to flash an LED or something to see what happens. The way I read it is it will return back to the while loop because there is nothing else in the SubSwitcher routine to execute after the branch statement, just another return back to the while loop.
    Wisdom is knowing what path to take next... Integrity is taking it.
    Ryan Miller

  18. #18
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Ok guys, sit back, take a breath, give me a little while to come up with a better answer.

    All will be revealed. Or further confused. We'll see.
    DT

  19. #19
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I'll try something...

    Code:
    StartPoint:
        Gosub Somewhere
    Label1:
        Pause 100
        Goto Start
    Somewhere:
        Pause 1
        Return
    Nothing hard here right? after the Return... you return to Label1 ... right?

    Now...
    Code:
    StartPoint:
        Gosub Somewhere
    Label1:
        Pause 100
        Goto Start
    Somewhere:
        Goto SomewhereElse
        z: goto z    
    
    SomewhereElse:
        Return
    Same thing happen here...

    This said...

    Code:
    Start:                         
          for routines=0 to 3
    StartPoint:
              gosub subselect
    ReturnPoint:
              pause 200
              next
        goto start
    
    SubSelect:
              branchl routines, [Routine0, Routine1, Routine2, Routine3]    
              
    Routine0:
             return
    Where the Return of Routine0 leads you?... to ReturnPoint

    I know i'm a pretty bad teacher...
    Last edited by mister_e; - 18th November 2006 at 20:02.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  20. #20
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    and I am a good student.


    I thought that once the program jumps, then it will forget where it was and will return to an unknown place. So that it will never be 100% precise.


    ---------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  21. #21
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Gosub and Return are good friends...

    As long as you have a GOSUB... you can insert 2789423564875634275624365 432956345634564326586324957243657 234596243652 43656243756243654 356437856 46582436587243657 26435726435 2436 (+/- 1) goto in-between. And when you'll use Return, it will return just under the GOSUB.
    Last edited by mister_e; - 18th November 2006 at 20:11.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  22. #22
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer
    I thought that once the program jumps, then it will forget where it was and will return to an unknown place. So that it will never be 100% precise.
    To be even more specific, the GOSUB will put a return address on the top of the "Stack" It will remain there forever (as long as the PIC is not reset) until a RETURN is executed. So, Steve's 2.7894235e+128 GOTOs do nothing to modify the address on the top of the stack. And, no matter when the RETURN is executed, the program will return to that address.

    HTH,
    Steve B
    Last edited by SteveB; - 18th November 2006 at 20:17.

  23. #23
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default That's a better explanation indeed !!!

    SteveB....SteveB....SteveB....
    SteveB....SteveB....SteveB....
    Whizzz... Woohoo...
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1198&stc=1&d=116388100 4">
    Attached Images Attached Images  
    Last edited by mister_e; - 18th November 2006 at 20:25.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  24. #24
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    So much for my fancy reply. Too slow.

    Got it Sayzer??
    DT

  25. #25
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor
    So much for my fancy reply. Too slow.
    Ahhhhhhh , I was really looking forward to it. My stab at it was just a down and dirty continuation of what Steve was getting at. We'll let Sayzer and Ryan weigh in on whether it worked for them.

  26. #26
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Steve,
    ROFL, Great picture!

  27. #27
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Hehe, Rock and roll, DEVIL!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  28. #28
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    I have a new teacher now!

    Thanks for this simple and clear explanation SteveB.

    I was expecting DT to come up with really complex explanations. heheh


    --------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  29. #29
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    mmm... not complex Sayzer, let's say detailled.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  30. #30
    Join Date
    Mar 2005
    Location
    Iowa, USA
    Posts
    216


    Did you find this post helpful? Yes | No

    Lightbulb

    Yep... makes sense. After all of your responses and looking at the manual for BranchL...
    Quote Originally Posted by TFM
    ...Index selects one of a list of Labels. Execution resumes at the
    indexed Label
    . For example, if Index is zero, the program jumps to the
    first Label specified in the list, if Index is one, the program jumps to the
    second Label, and so on. If Index is greater than or equal to the
    number of Labels, no action is taken and execution continues with the
    statement following the BRANCHL.
    ....(hence the RETURN)
    I guess seeing that return in Darrel's code was throwing me off, but now it I see why it's in there. It looked to me like a way of nesting subroutines. Both of the Steves explainations of how the return address in the stack works answers one of the questions I didn't even ask.... scary! Thankyou all.
    Wisdom is knowing what path to take next... Integrity is taking it.
    Ryan Miller

  31. #31
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I was going more for ... Cool!

    Well, since we don't need the explanation anymore, I'll leave it at this, since it's already done.

    DT

  32. #32
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1199&stc=1&d=116388436 9">
    Attached Images Attached Images  
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  33. #33
    Join Date
    Mar 2005
    Location
    Iowa, USA
    Posts
    216


    Did you find this post helpful? Yes | No

    Default

    Cool indeed!!! You're going to have to share with us your posting techniques!
    Wisdom is knowing what path to take next... Integrity is taking it.
    Ryan Miller

  34. #34
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Holy Gif!

    DT, I can't believe you made it within half an hour!
    What can I say? It is just as you said it was.




    rhino,

    I now see that you were in a different page then I was.

    But, you had your answer and I had mine.


    Thanks to all.

    I enjoyed watching posts coming in and out, too.

    Especially this last post by DT!

    ---------------------------


    Edit: I just realized that it has been two hrs....


    ------------------------------------
    Last edited by sayzer; - 18th November 2006 at 21:24.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  35. #35
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Darrel's turn

    Darrel...Darrel...Darrel...
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1198&d=1163881004">
    Darrel...Darrel...Darrel...
    Woohoo..Whizz..ROCK AND ROLL!!!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  36. #36
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Thanks guys,

    Quote Originally Posted by rhino
    You're going to have to share with us your posting techniques!
    Piece of cake (with the right software).
    Alt-Print Screen (windows XP, screen capture)
    MS Paint to import and crop
    Selteco Bannershop GIF animator. http://www.selteco.com/bannershop/
    and about an hour creating 58 animation frames.

    I'll have to try it again sometime.
    <br>
    DT

  37. #37
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Oh, Ta,
    I see you added another question to post#13. Almost missed it.

    and one more question, im trying to use this code to make a sort of clock.

    i'm using the pins LOW to make the circuit, but i dont really want to use HIGH to stop the circuit, is there anway to just turn the pin off, like when the program is run at the start?
    Thanks again
    Code:
    INPUT  PORTB.0
    
    '  --- OR ---
    
    TRISB.0 = 1
    Either of those will return the pin to High-Impedance state.

    HTH,
    DT

  38. #38
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Thumbs up

    Well, what d'ya know!? I Make a run for something to throw on the grill and the next thing I know, Darrel has raised the bar when in comes to posting code explainations!

    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1202&d=1163898624">
    Now that's what I was looking forward to!!!

    Darrel not only came up with a quite elegent solution for the origianl problem in a matter of minutes, but then went over the top in showing how it works.

    Way to Go!

    Steve B
    Attached Images Attached Images  
    Last edited by SteveB; - 19th November 2006 at 03:00.

  39. #39
    Join Date
    Mar 2005
    Location
    Iowa, USA
    Posts
    216


    Did you find this post helpful? Yes | No

    Thumbs up

    Very cool Darrel. I'm sure we're going to start seeing some interesting posts from now on. Thanks again, and keep raising the bar!
    Wisdom is knowing what path to take next... Integrity is taking it.
    Ryan Miller

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts