Shop OBEX P1 Docs P2 Docs Learn Events
Running Subroutines simultaneously — Parallax Forums

Running Subroutines simultaneously

guitarists10guitarists10 Posts: 1
edited 2009-09-22 04:32 in BASIC Stamp
I am trying to find out how to run subroutines simultaneously but if does not seem to work. If someone could help me out I would greatly appreciate it. Thanks a bunch

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-09-21 18:33
    You can't. There's only one processor in a Stamp. When it's doing one statement, it can't do anything else. If you really want to do two or more things simultaneously, you need something like a Propeller.

    If you would describe what you're trying to accomplish, someone may be able to suggest other ways of doing it that don't require actual simultaneous execution of more than subroutine at a time.

    Some simultaneous tasks can be handled by using special purpose external co-processors like a ServoPAL or SoundPAL or any of several 3rd party external co-processors that can do some specific task for the Stamp and be controlled by the Stamp, yet function independently.
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-09-21 18:34
    Sorry to disappoint you, but you cannot. Running pBasic·is just like putting on pair of pants - one leg at a time. Post you code to see if we can find a solution elsewhere.
  • hmlittle59hmlittle59 Posts: 404
    edited 2009-09-22 00:04
    In my code when I'm in a "Sub-Routine" monitoring a Certain INPUT Pin01, I am also checking another INPUT Pin02 ,Pin03,Pin04 and if any of these PINs changes state(HIGH) I set a FLAG for that Pin, so when I'm done with THAT "Sub-Routine" of (Pin01) I check that FLAG(s) for Pin02,Pin03,and Pin04 and if HIGH by-pass some code(TIME DELAY routines) and go to the next step within that Sub-Routine(2,3,4). That was the closes I could get to doing my 3 "Sub-Routines" in a near real-time/multi-tasking fashion.

    I have a "CONTROLLING" Sub-Routine that LOOPs thru itself(DO/LOOP). Each time thru it will GOSUB to each routine below (1,2,3,4), at the start of each Sub-routine I check for a PIN(HIGH/LOW), If all is OK then I bounce back to the "CONTROLLING" Sub-routine for the next one to check. Once all Sub-routines are checked then I just start the "CONTROLLING" sub-routine all over again(I have time delays...DS1302 and there must be and error) so it will not exit from this "CONTROLLING" Sub-Routine. Once the ERROR has Cleared or I press a certain button(then setting a FLAG) the "CONTROLLING" Sub-Routine returns me back to the MainMenu or the MainMonitoring routine(ALL Clear). I found that I had to set FLAGs to know what to do when I got back to the "CONTROLLING" and check those FLAGs.

    1) Do Sub-routine(1)...Monitor Pins 2,3,4,5..etc. also. If the state of the other pins change set a Flag(s) while still within Sub-Routine(1)
    2) Do Sub-routine(2)...Monitor Pins 1,3,4,5..etc. also. If the state of the other pins change set a Flag(s) while still within Sub-Routine(2)
    3) Do Sub-routine(3)...Monitor Pins 1,2,4,5..etc. also. If the state of the other pins change set a Flag(s) while still within Sub-Routine(3)
    4) Do Sub-routine(4)...Monitor Pins 1,2,3,5..etc. also. If the state of the other pins change set a Flag(s) while still within Sub-Routine(4)

    For what I'm looking to do, this Process works very very well, the BS2/e is more then fast enough. But I'm using timers(3) to the minute though.

    1)Start of Program
    2)Main Body of program(to do initial check for errors)
    3)Gosub to "CONTROLLING" Sub-routine...That will monitor the 2,3, or 4 other Sub-routines
    4)Once either one of these Sub-routine(s) are done you always return to the "CONTROLLING" sub-routine.
    5)Within the "CONTROLLING" sub-routine check for the same errors or event to if they have cleared and if you can return to STEP 2)
    6)If you still have an error stay within the "CONTROLLING" sub-routine

    I only monitor the change of state of pins(three of them) and time delays(three of them) and Button Pins so it was easy to do...after starring at the ceiling every night for 2 - 3 hours a night for a week(Ha Ha...laugh now but its true) to get the Logic/ Steps/Process down.

    hope this BAD semi-Pseudo code comes close to helping...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I'M STILL LEARNING SO MUCH...BUT STILL KNOW SO LITTLE!!!

    hmlittle59
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-09-22 02:26
    If I understood you correctly you are monitoring three inputs and perform some task depending on how they are set. You are not necessary looking for when they are set.
    So what is preventing you to run your process depending on that?
    In other words – you have 8 possible states so you should have 8 possible processes / subroutines. It should be possible, but may be messy, to have each one of these processes continue to monitor the inputs. Sounds like a good candidate for implementation of “state machine”.
    What did I miss?
  • hmlittle59hmlittle59 Posts: 404
    edited 2009-09-22 04:32
    I'm only looking to see if the Pins(3 of them) go from HIGH to LOW. If either one changes then I GOSUB and check the time and from there to the "CONTROLLING" Sub-routines. From there I just check 3 separate Sub-routines to see which one gave the error. If the error has cleared by then(less then a 1 second) then I return to the "CONTROLLING" Sub-routine and it will check each of the other sub-routines. If all is clear then the "CONTROLLING" sub-routine return control back to the Monitoring Routine. If an error was found then the Sub-routine(1,2, or 3) sets some FLAGs and starts the count down timer. This routine I wrote can handle 3 separate timers(count down) and react when they reach Zero(0). When any one routine is done, control is return to the "CONTROLLING" sub-routine to recheck the other sub-routines for any errors. If they are in error then the timer is started for that one. There is a couple of routines I wrote that helps me keep control of whats going on. The only problem is that I'm completely out of variable space that I can use/re-use/ and re-re-use. These modules are the same but with some different FLAGs and Variables. I've already used some of the DS1302 space for variable storage and I need to Debug one more and that's it. But I don't feel like cramming any more up their. The timer routines were fun to write (I'm PROUD of them) and all testing seems to go as plan. Each Count down timer is the same amount of time (10,20,30...etc.) but they can start at different times and keep track of the different stop time(s). That's one of the reason I felt that I needed an "CONTROLLING" Sub-routine.

    I'm still testing for all different kind of situation that would cause it to fail but so far so good.

    Remember I'm only monitoring down to the MINUTE. Not seconds or split seconds. That would makes it harder to trap certain time events.
    I also monitor for exit BUTTON push...

    Oh well...Back to testing...hope this helps someone.
    PS: MY Miami Dolphins are not 0 and 2...NOT GOOD!!!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I'M STILL LEARNING SO MUCH...BUT STILL KNOW SO LITTLE!!!

    hmlittle59
Sign In or Register to comment.