Shop OBEX P1 Docs P2 Docs Learn Events
Pausing and polling — Parallax Forums

Pausing and polling

ArchiverArchiver Posts: 46,084
edited 2003-06-02 14:41 in General Discussion
This may seem elementary to all of you, but I need advice. I've done plenty
of programming in Java, C, C++, and Pascal, but embedded applications are
fairly new to me.

What is the best way, on a BS2, to pause execution while polling for a
button press? The best way I've come up with if using a FOR loop that does
absolutely nothing except run the BUTTON command. My program pauses
frequently for various lengths of time (in which the timing isn't all that
critical, although "about a second" or "about .25 seconds" is good enough.

This seems like a kludge, though. Is there a better way?

Steve

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-06-01 00:37
    Assuming the button press (or whatever) is not very brief, within your
    program loop you poll all your inputs one right after the other and set bits
    in a variable according to their state. When you need to, check the status
    of these bits and branch accordingly. This is how I process multiple sensors
    on some of my robots.

    Obviously the more statements you have in your loop, the less often you poll
    your inputs, but you can tweak things and write your code to make things
    work fairly fast.

    Original Message

    > This may seem elementary to all of you, but I need advice. I've done
    plenty
    > of programming in Java, C, C++, and Pascal, but embedded applications are
    > fairly new to me.
    >
    > What is the best way, on a BS2, to pause execution while polling for a
    > button press? The best way I've come up with if using a FOR loop that does
    > absolutely nothing except run the BUTTON command. My program pauses
    > frequently for various lengths of time (in which the timing isn't all that
    > critical, although "about a second" or "about .25 seconds" is good enough.
    >
    > This seems like a kludge, though. Is there a better way?
  • ArchiverArchiver Posts: 46,084
    edited 2003-06-01 01:13
    That would work fine if my code executed in a nice tight loop, but I have
    multiple Pause statements, where I want nothing else to happen except
    checking for the button press. So, it looks like I'll have to have subloops
    that simply check for the press, and do nothing otherwise. I just don't like
    a loop that simply increments a variable until it reaches a certain point.
    As my code will always run on the same stamp, the timing of the loop is
    predictable, but I guess I have something against this since I'm used to
    programming for regular computer systems when the execution time can vary
    widely (a FOR loop counting from 0 to 999 may take a second, or a
    millisecond, or less, on a PC).

    Steve

    Original Message
    From: "Rodent" <daweasel@s...>
    > Assuming the button press (or whatever) is not very brief, within your
    > program loop you poll all your inputs one right after the other and set
    bits
    > in a variable according to their state. When you need to, check the status
    > of these bits and branch accordingly. This is how I process multiple
    sensors
    > on some of my robots.
    >
    > Obviously the more statements you have in your loop, the less often you
    poll
    > your inputs, but you can tweak things and write your code to make things
    > work fairly fast.
    >
    >
    Original Message
    >
    > > What is the best way, on a BS2, to pause execution while polling for a
    > > button press? The best way I've come up with if using a FOR loop that
    does
    > > absolutely nothing except run the BUTTON command. My program pauses
    > > frequently for various lengths of time (in which the timing isn't all
    that
    > > critical, although "about a second" or "about .25 seconds" is good
    enough.
  • ArchiverArchiver Posts: 46,084
    edited 2003-06-01 01:20
    >This may seem elementary to all of you, but I need advice. I've done plenty
    >of programming in Java, C, C++, and Pascal, but embedded applications are
    >fairly new to me.
    >
    >What is the best way, on a BS2, to pause execution while polling for a
    >button press? The best way I've come up with if using a FOR loop that does
    >absolutely nothing except run the BUTTON command. My program pauses
    >frequently for various lengths of time (in which the timing isn't all that
    >critical, although "about a second" or "about .25 seconds" is good enough.
    >
    >This seems like a kludge, though. Is there a better way?
    >
    >Steve

    Hi Steve,

    The button command is usually overkill, unless you need the
    autorepeat function. It is usually more efficient to poll the pin
    directly:

    {PBASIC 2.5}
    do until in0=0 ' waits for button low
    nap 4 ' naps by 1/4 second, saving power
    loop

    If you don't need the power saving, you can use PAUSE 250 instead of
    NAP 4. This does wait forever if the button is never pressed.

    If you need a sure escape after ~1 second, then include a timeout:

    {PBASIC 2.5}
    watchdog=4
    do until in0=0 OR watchdog=0 ' waits for button low or timeout
    nap 4 ' naps by 1/4 second, saving power
    watchdog=watchdog-1
    loop

    On the BS2p and BS2pe you have another option, using pollwait:

    pollmode 2 ' enable polling with wait action
    pollin 0 ' pin p0 for the button, waiting for low
    pollwait 4 ' wait for button low using 250 ms naps
    ' continue here
    pollmode 0 ' disable polling

    That can be configured to wait for activity from multiple pins and
    achieves the lowest power drain, especially on the '2pe.

    -- regards
    Tracy Allen
    electronically monitored ecosystems
    http://www.emesystems.com
    mailto:tracy@e...
  • ArchiverArchiver Posts: 46,084
    edited 2003-06-01 01:28
    Ah, this looks like a better solution - Using pause for shorter periods, and
    specifying how many periods to wait for, instead of an arbitrary number for
    a loop (oh, 1234 seems like a second). Like I said, exact timing isn't
    critical, but this makes for a better convention.

    Out of curiosity, if anyone knows, when a button is "tapped", approximately
    what is the average time that a person actually makes contact? A bit of
    trivia that may require some experimentation, but just a thought that came
    to mind...

    Steve

    Original Message
    From: "Tracy Allen" <tracy@e...>
    To: <basicstamps@yahoogroups.com>
    Sent: Sunday, June 01, 2003 2:20 AM
    Subject: Re: [noparse][[/noparse]basicstamps] Pausing and polling


    > >This may seem elementary to all of you, but I need advice. I've done
    plenty
    > >of programming in Java, C, C++, and Pascal, but embedded applications are
    > >fairly new to me.
    > >
    > >What is the best way, on a BS2, to pause execution while polling for a
    > >button press? The best way I've come up with if using a FOR loop that
    does
    > >absolutely nothing except run the BUTTON command. My program pauses
    > >frequently for various lengths of time (in which the timing isn't all
    that
    > >critical, although "about a second" or "about .25 seconds" is good
    enough.
    > >
    > >This seems like a kludge, though. Is there a better way?
    > >
    > >Steve
    >
    > Hi Steve,
    >
    > The button command is usually overkill, unless you need the
    > autorepeat function. It is usually more efficient to poll the pin
    > directly:
    >
    > {PBASIC 2.5}
    > do until in0=0 ' waits for button low
    > nap 4 ' naps by 1/4 second, saving power
    > loop
    >
    > If you don't need the power saving, you can use PAUSE 250 instead of
    > NAP 4. This does wait forever if the button is never pressed.
    >
    > If you need a sure escape after ~1 second, then include a timeout:
    >
    > {PBASIC 2.5}
    > watchdog=4
    > do until in0=0 OR watchdog=0 ' waits for button low or timeout
    > nap 4 ' naps by 1/4 second, saving power
    > watchdog=watchdog-1
    > loop
    >
    > On the BS2p and BS2pe you have another option, using pollwait:
    >
    > pollmode 2 ' enable polling with wait action
    > pollin 0 ' pin p0 for the button, waiting for low
    > pollwait 4 ' wait for button low using 250 ms naps
    > ' continue here
    > pollmode 0 ' disable polling
    >
    > That can be configured to wait for activity from multiple pins and
    > achieves the lowest power drain, especially on the '2pe.
    >
    > -- regards
    > Tracy Allen
    > electronically monitored ecosystems
    > http://www.emesystems.com
    > mailto:tracy@e...
    >
    >
    >
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
  • ArchiverArchiver Posts: 46,084
    edited 2003-06-01 01:31
    Why use a for / next loop?

    Here is an example in psuedocode:

    Main:

    Clear Flag Byte

    Check Pin 1:

    If Pin 1 High
    Then Set Bit 1 of Flag Byte High

    Check Pin 2:

    If Pin 2 High
    Then Set Bit 2 of Flag Byte High

    Check Flag Byte:

    If Flag Byte > 0
    Then Do Something
    Else Goto Main

    Original Message

    > That would work fine if my code executed in a nice tight loop, but I have
    > multiple Pause statements, where I want nothing else to happen except
    > checking for the button press. So, it looks like I'll have to have
    subloops
    > that simply check for the press, and do nothing otherwise. I just don't
    like
    > a loop that simply increments a variable until it reaches a certain point.
    > As my code will always run on the same stamp, the timing of the loop is
    > predictable, but I guess I have something against this since I'm used to
    > programming for regular computer systems when the execution time can vary
    > widely (a FOR loop counting from 0 to 999 may take a second, or a
    > millisecond, or less, on a PC).

    > > Assuming the button press (or whatever) is not very brief, within your
    > > program loop you poll all your inputs one right after the other and set
    > bits
    > > in a variable according to their state. When you need to, check the
    status
    > > of these bits and branch accordingly. This is how I process multiple
    > sensors
    > > on some of my robots.
    > >
    > > Obviously the more statements you have in your loop, the less often you
    > poll
    > > your inputs, but you can tweak things and write your code to make things
    > > work fairly fast.
    > >
    > >
    Original Message
    > >
    > > > What is the best way, on a BS2, to pause execution while polling for a
    > > > button press? The best way I've come up with if using a FOR loop that
    > does
    > > > absolutely nothing except run the BUTTON command. My program pauses
    > > > frequently for various lengths of time (in which the timing isn't all
    > that
    > > > critical, although "about a second" or "about .25 seconds" is good
    > enough.
  • ArchiverArchiver Posts: 46,084
    edited 2003-06-02 01:30
    Rodent,

    This is kinda what I do when looking for a key input from the pak6 keybd
    chip, check a data available pin on another chip which indicates data wants
    to come through on another pin. Is there a fairly simple string of
    components that can keep his key press on the pin as a high or a low, until
    the stamp answers with a high or a low to reset the string of components to
    the un-pressed condition? If so maybe one line could be used and the
    program wouldn't have to loop either. Would this be a hassle to do?

    Cure Cancer and Smallpox with your computer.
    It runs as a screen-saver Sponsored by Intel:
    http://members.ud.com/download/gold/

    Original Message
    From: "Rodent" <daweasel@s...>
    To: <basicstamps@yahoogroups.com>
    Sent: Saturday, May 31, 2003 8:31 PM
    Subject: Re: [noparse][[/noparse]basicstamps] Pausing and polling


    > Why use a for / next loop?
    >
    > Here is an example in psuedocode:
    >
    > Main:
    >
    > Clear Flag Byte
    >
    > Check Pin 1:
    >
    > If Pin 1 High
    > Then Set Bit 1 of Flag Byte High
    >
    > Check Pin 2:
    >
    > If Pin 2 High
    > Then Set Bit 2 of Flag Byte High
    >
    > Check Flag Byte:
    >
    > If Flag Byte > 0
    > Then Do Something
    > Else Goto Main
    >
    >
    Original Message
    >
    > > That would work fine if my code executed in a nice tight loop, but I
    have
    > > multiple Pause statements, where I want nothing else to happen except
    > > checking for the button press. So, it looks like I'll have to have
    > subloops
    > > that simply check for the press, and do nothing otherwise. I just don't
    > like
    > > a loop that simply increments a variable until it reaches a certain
    point.
    > > As my code will always run on the same stamp, the timing of the loop is
    > > predictable, but I guess I have something against this since I'm used to
    > > programming for regular computer systems when the execution time can
    vary
    > > widely (a FOR loop counting from 0 to 999 may take a second, or a
    > > millisecond, or less, on a PC).
    >
    > > > Assuming the button press (or whatever) is not very brief, within your
    > > > program loop you poll all your inputs one right after the other and
    set
    > > bits
    > > > in a variable according to their state. When you need to, check the
    > status
    > > > of these bits and branch accordingly. This is how I process multiple
    > > sensors
    > > > on some of my robots.
    > > >
    > > > Obviously the more statements you have in your loop, the less often
    you
    > > poll
    > > > your inputs, but you can tweak things and write your code to make
    things
    > > > work fairly fast.
    > > >
    > > >
    Original Message
    > > >
    > > > > What is the best way, on a BS2, to pause execution while polling for
    a
    > > > > button press? The best way I've come up with if using a FOR loop
    that
    > > does
    > > > > absolutely nothing except run the BUTTON command. My program pauses
    > > > > frequently for various lengths of time (in which the timing isn't
    all
    > > that
    > > > > critical, although "about a second" or "about .25 seconds" is good
    > > enough.
    >
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2003-06-02 14:41
    From my 'rules-of-thumb' memory, I would say that a
    button-press needs a debounce of several milli-seconds,
    (that is, when it changes you keep looking for it
    to STAY changed -- the first press is going to
    bounce for a few (1 to 3) milliseconds).

    Also, a person percieves immediate response to
    something he has pressed if it occurs within 100
    mSec. (1/10 second).

    Conceivably, you can lower this to 10 to 20 mSec.
    The fastest a person can press a button, let go,
    and press it again is around 60 mSec.

    All measures are approximate, and should be
    verified experimentally, but these will start
    you in the right range, anyway.

    --- In basicstamps@yahoogroups.com, "Steve Ziuchkovski"
    <zman97211@y...> wrote:
    > Ah, this looks like a better solution - Using pause for shorter
    periods, and
    > specifying how many periods to wait for, instead of an arbitrary
    number for
    > a loop (oh, 1234 seems like a second). Like I said, exact timing
    isn't
    > critical, but this makes for a better convention.
    >
    > Out of curiosity, if anyone knows, when a button is "tapped",
    approximately
    > what is the average time that a person actually makes contact? A
    bit of
    > trivia that may require some experimentation, but just a thought
    that came
    > to mind...
    >
    > Steve
    >
    >
    Original Message
    > From: "Tracy Allen" <tracy@e...>
    > To: <basicstamps@yahoogroups.com>
    > Sent: Sunday, June 01, 2003 2:20 AM
    > Subject: Re: [noparse][[/noparse]basicstamps] Pausing and polling
    >
    >
    > > >This may seem elementary to all of you, but I need advice. I've
    done
    > plenty
    > > >of programming in Java, C, C++, and Pascal, but embedded
    applications are
    > > >fairly new to me.
    > > >
    > > >What is the best way, on a BS2, to pause execution while polling
    for a
    > > >button press? The best way I've come up with if using a FOR loop
    that
    > does
    > > >absolutely nothing except run the BUTTON command. My program
    pauses
    > > >frequently for various lengths of time (in which the timing
    isn't all
    > that
    > > >critical, although "about a second" or "about .25 seconds" is
    good
    > enough.
    > > >
    > > >This seems like a kludge, though. Is there a better way?
    > > >
    > > >Steve
    > >
    > > Hi Steve,
    > >
    > > The button command is usually overkill, unless you need the
    > > autorepeat function. It is usually more efficient to poll the pin
    > > directly:
    > >
    > > {PBASIC 2.5}
    > > do until in0=0 ' waits for button low
    > > nap 4 ' naps by 1/4 second, saving power
    > > loop
    > >
    > > If you don't need the power saving, you can use PAUSE 250 instead
    of
    > > NAP 4. This does wait forever if the button is never pressed.
    > >
    > > If you need a sure escape after ~1 second, then include a timeout:
    > >
    > > {PBASIC 2.5}
    > > watchdog=4
    > > do until in0=0 OR watchdog=0 ' waits for button low or timeout
    > > nap 4 ' naps by 1/4 second, saving power
    > > watchdog=watchdog-1
    > > loop
    > >
    > > On the BS2p and BS2pe you have another option, using pollwait:
    > >
    > > pollmode 2 ' enable polling with wait action
    > > pollin 0 ' pin p0 for the button, waiting for low
    > > pollwait 4 ' wait for button low using 250 ms naps
    > > ' continue here
    > > pollmode 0 ' disable polling
    > >
    > > That can be configured to wait for activity from multiple pins and
    > > achieves the lowest power drain, especially on the '2pe.
    > >
    > > -- regards
    > > Tracy Allen
    > > electronically monitored ecosystems
    > > http://www.emesystems.com
    > > mailto:tracy@e...
    > >
    > >
    > >
    > >
    > >
    > >
    > > To UNSUBSCRIBE, just send mail to:
    > > basicstamps-unsubscribe@yahoogroups.com
    > > from the same email address that you subscribed. Text in the
    Subject and
    > Body of the message will be ignored.
    > >
    > >
    > > Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/
    > >
Sign In or Register to comment.