pushbutton and loop algorithm

Discussion in 'Embedded Systems and Microcontrollers' started by Eric007, Aug 22, 2011.

  1. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    Good day Everybody!!!
    Its a beautiful day, isn't?

    What I'm actually trying to achieve is to control 2 loops by using a single pushbutton!

    It should work as follows:
    - 1st press, loop1 start running
    - 2nd press, loop2 start running
    - 3rd press, loop1 can re-start running
    and so on...

    Here's the algo I came up with:

    if pressed goto loop1
    if not pressed goto checkpress

    if pin=0 (which is a press) goto loop2
    if pin=1 (which is not press) goto loop1

    if pin=0 goto loop1
    if pin=1 goto loop2

    In the above algorithm, pin is the same pin in the checkpress loop...
    Does it make sense? Please I need ur comment or if u have a more elegant way of achieving this please show me

    Also, when power is put off and then put back on, where does the program start? Where it stopped or at the beginning of main? (Excuse me if this is a stupid question).

  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    Well... that ain't gonna do much. If you press the button Checkpress will jump to Loop1, Loop1 will then jump to Loop2, and Loop2 will jump back to Loop1, which jumps to loop 2, which....

    Nothing gets done. You need to also check in Loop2 that the button is NOT pressed before enabling the jump to Loop3.

    Also, if this is a real push button you need to check if it is bouncing and not follow every bounce.
  3. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    @ErmieM: can u help me then come up with a better algorithm to achieve the above?
    Actually I need to use this algo to turn a set of LEDs on and off using a "single" pushbutton!
    And yes its gona be a real pushbutton!
  4. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    @Ermie: how about I change checkpress,for debouncing, as follow:

    If pressed =>call delay
    If not pressed got checkpress
    If pressed goto loop1
    If not pressed goto checkpress

    Is that ok? And as for the delay, what's the time for it I mean how long should it take?

    And regarding ur other comment for loop2, I do check if button is nit pressed before going back to loop1, isn't ???

    Also there's no loop3...

    Hope to get a comment from u!
  5. nickelflipper

    Active Member

    Jun 2, 2010