Shop OBEX P1 Docs P2 Docs Learn Events
Is a 1-line background program possible? — Parallax Forums

Is a 1-line background program possible?

JPLJPL Posts: 8
edited 2010-09-29 16:35 in BASIC Stamp
I would like to run a 1 line program in the background while everything else is running on my BSp40. The 1 line would be:

IF IN10 = 1 THEN GOSUB [subroutine]

The only thing I can think of now is to put that 1 line in every loop that I have written, but is there another way?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-28 22:06
    No

    There is no such thing as a background in the Stamps. They are single-threaded processors. They can do one thing at a time. Only

    There is something called "polling" in the BS2p Stamps, where the Stamp can indeed check the status of an I/O pin in between instructions, but there's no way to call a subroutine. The Stamp can be forced to effectively do a GOTO, but there's no way to remember where it came from, so it can't return to the place where the poll succeeded.

    It's pretty easy to add a little bit of external hardware to remember the change in state of an I/O pin (like a capacitor that's discharged when your input occurs and slowly gets recharged). If there's a delay of a sizable fraction of a second, you would only have to test for it occasionally, not in every loop.

    Depending on what your program does, you might be able to rewrite it using a different structure where it's relatively easy to embed tests for the state of an I/O pin.
  • JPLJPL Posts: 8
    edited 2010-09-28 22:14
    My programs run many FOR loops. I'll simply add the IF line to the beginning of each loop (1-2 times per program).

    Thank you for your quick reply.
  • CampeckCampeck Posts: 111
    edited 2010-09-29 05:39
    I like the polling instructions ability to call new programs. I just made a high power RGB fader circuit that has 5 different programs all with different effects called up with the polling command. Using the BS2pe

    I made a program that polled an input while an led flashed and the same program without polling. I couldn't see a decrease in led flash speed. Which is a nice thing. The stamps are already pretty slow.
  • ercoerco Posts: 20,256
    edited 2010-09-29 15:08
    This next suggestion to achieve a pseudo-interrupt may make some people cringe, but here goes:

    What if the Stamp was forced to reset as a result of some external stimulus, such as whatever external circuitry was originally planned to switch IN10 high (per the OP)?

    In this configuration, the OP's GOSUB-type routine would be relocated to the very beginning of the program, so it would execute first after each forced reset. Afterward, the program could jump to a location dictated by a variable previously written into EEPROM. Of course, all variables used would also need to be written to EEPROM regularly, and read from EEPROM into RAM after each reset.

    Uncurl your toes, it's just a thought. Although someone has probably done it already and Mike and Phil know the proper terminology for this type of routine.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-29 15:43
    erco,
    What you're suggesting is not particularly different from the poll statements although it would work with all Stamp models, not just the BS2p models. With all the "checkpoints" needed to remember what the program was doing when the reset occurred, you'll quickly burn out the EEPROM. If you had a small amount of RAM like that built into a DS1302 real time clock, it would be easy to save the program's state.
  • ercoerco Posts: 20,256
    edited 2010-09-29 16:35
    XLNT point, Mike. My EE friend also points out that "there would be blood" if the reset came in the middle of a write cycle. Bad Stamp Mojo.

    So much for my "poor man's interrupt" theory... :)

    But I'm still working on antigravity and perpetual motion!
Sign In or Register to comment.