Shop OBEX P1 Docs P2 Docs Learn Events
Have code running in background in Pbasic? — Parallax Forums

Have code running in background in Pbasic?

BrockBrock Posts: 2
edited 2010-02-19 02:31 in BASIC Stamp
I'm relatively new to Basic(1 week) and all programming I've ever done has been in c++ so i'm still learning some things. What I need is a section of code to be run in the "background" while the rest of the code
is running.

To give you a better idea, the processor is monitoring a pressure and battery voltage. When that voltage goes below a certain specified level, I want to have this LED blink(LED is on pin 13 of mcu). How do I incorporate that loop to run while the rest of the code runs? I see there is a POLLIN/POLLOUT commands that poll a pin after every line, but I am not polling the status of the pin, I would need to check the value of a variable then have a loop like

high 13
pause 1000
low 13

Thanks,

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-02-18 16:28
    The Stamps are "single threaded" processors. They cannot do something in the "background".

    What you can do is to set up your main monitoring loop so that it takes roughly the same time to execute no matter what control path is taken and you use that as your "blink" timer instead of the PAUSE. Say that your main loop takes 100ms. You'd have a counter stored in a byte. If the byte is 255, you'd leave it alone otherwise you'd increment it with a wraparound at 20 back to zero. If the counter is less than 10, you'd turn the LED on. If the counter is 10 or higher, you'd turn the LED off.

    To start the LED blinking, you'd set the counter to zero. To turn off the LED, you'd set the counter to 255.
  • BrockBrock Posts: 2
    edited 2010-02-18 16:48
    Thanks for the response Mike.

    One question is why would I wraparound at 20?

    Another question I have is can I set a PollIN command to monitor an output that isn't being used(constant 0 value) and tell the pollin to look for 0 value and if it is the case

    if(bat voltage < x)
    hi 13
    pause 500
    lo 13

    etc, and itll execute this set of code after every instruction
  • Mike GreenMike Green Posts: 23,101
    edited 2010-02-18 17:07
    Regarding the counter ... values of 0-9 would cause the LED to go on, values of 10-19 would cause the LED to go off, and the wraparound at 20 would take the cycle back to zero.

    POLLIN doesn't work the way you want. It's not really an interrupt. Although POLLIN can cause code to be executed, there's no way to return to the "interrupted" code.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-02-19 02:31
    This kind of thing is often done with an external chip, such as a 555 timer, or even with one of those LEDs that has a built-in flash circuit. A pin on the Stamp would enable the autonomous flasher when it detects that the battery is low.

    POLLIN and POLLOUT could help in a minor way. In one scenario, the POLLIN pin would be set up with a voltage divider to detect the battery voltage threshold. It would be coupled to the POLLOUT pin in such a way as to enable the flasher when the battery is low. No further program intervention would be necessary.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.