Shop OBEX P1 Docs P2 Docs Learn Events
Correct Sequence of Events (Momentary Pushbutton N.O.) — Parallax Forums

Correct Sequence of Events (Momentary Pushbutton N.O.)

perception13perception13 Posts: 3
edited 2010-07-27 00:49 in BASIC Stamp
To put it simple:
I want to push Button1 then button2... lighting an LED for 5 seconds.
I currently have to push them both for the LED to light.
I want to hold the value of push Button1 high(while letting it go normally open)
then i can push button2 after pressing and releasing button1.
This would light LED.

This is the code I have sofar. (i eventually want 5 keys pressed in the correct order to "light led")

I know its ugly code, but I haven't grasped all the concepts yet. (so i am aware already)

(previous version note....ONE thing I have noticed is when the IF...THEN was nested the LED would light by just pressing button1... Button2 went high (=1) without pressing it.)

' {$STAMP BS2}
' {$PBASIC 2.5}
'-----[noparse][[/noparse] Revision History ]------------------------------------------------
'
' Rev. 21
' Taking out nested IF...THEN loops and separating them
'
'-----[noparse][[/noparse] Close History ]---------------------------------------------------
key1    VAR Nib 'Value can be 0 TO 15
key2    VAR Nib 'Value can be 0 TO 15
'key3    VAR Nib 'Value can be 0 TO 15
'key4    VAR Nib 'Value can be 0 TO 15
keycode VAR Nib  'Value can be 0 TO 15

keycode = 12

Main:
DEBUG ? IN1
DEBUG ? IN2
DEBUG ? OUT15
DO
  IF (IN1 = 1) THEN  'if button1 is pressed
    key1 = 05
    GOTO step2:
  ELSE                   'if button1 isnt pressed
    Key1 = 00
    KEY2 = 00
    GOTO main:       'start loop over
  ENDIF
step2:
  IF (IN2 = 1) THEN
    key2 = 07
    GOTO unlockdoor:
      DEBUG ? IN1
      DEBUG ? IN2
      DEBUG ? OUT15
    ELSE
        key2 = 00
        GOTO main:
  ENDIF
unlockdoor:
  IF key1 + key2 = keycode THEN
      DEBUG "key1 + key2 apparently equals keycode" ,CR
        HIGH 15
        DEBUG "Green light stays lit for 5 seconds" ,CR
        DEBUG ? IN1
        DEBUG ? IN2
        DEBUG ? OUT15
        PAUSE 5000
        LOW 15
      ELSE
        LOW 15
        GOTO main:
    ENDIF
LOOP



Again, i know its not pretty.

To compare this to PLC. It seems I need to LOCK a bit high.

Need Help

Comments

  • FranklinFranklin Posts: 4,747
    edited 2010-07-23 23:02
    You aren't taking into account the speed things are happening.
    If you press button one that becomes true and the program checks button two which has not been pressed yet so the program loops to main. You might be better off using the button command.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • rixterrixter Posts: 95
    edited 2010-07-23 23:41
    I think you need to cycle back to Step2 in that section instead of going back to Main, which is checking button 1 again. In other words you need to cycle each button section until a button is actually pressed and then store the unique value. So there is two things in your goal... whether the button was pressed, and which button it was. In your example there is only only provisions for pressing button 1 then button 2.

    You·may eventually want to have the sequence that the user must press to be your button sequence checking and·keep cycling back to the top of that section of code until the user·presses a button.

    Alternatively, you could have checking in each button press section to check to see which button was pressed. Using a CASE statement would do the trick. Only if the correct button is pressed do you set a variable to your value.



    Rick
  • perception13perception13 Posts: 3
    edited 2010-07-26 14:39
    rixter said...
    Alternatively, you could have checking in each button press section to check to see which button was pressed. Using a CASE statement would do the trick. Only if the correct button is pressed do you set a variable to your value.

    Rick

    Thanks Rick, But I'm not exactly sure how to do that.
    I'll show you what I've come up with:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '-----[noparse][[/noparse] Revision History ]------------------------------------------------
    '
    ' Rev. 22
    ' Taking out nested IF...THEN loops and replacing with SELECT...CASE statments.
    '
    '-----[noparse][[/noparse] Close History ]---------------------------------------------------
    key1    VAR Nib 'Value can be 0 TO 15
    key2    VAR Nib 'Value can be 0 TO 15
    'key3    VAR Nib 'Value can be 0 TO 15
    'key4    VAR Nib 'Value can be 0 TO 15
    keycode VAR Word  'Value can be 0 TO 255
    
    keycode = 12
    
    Main:
    DO
    
    'DEBUG DEC keycode
    
    SELECT key1
      CASE <> 05
        GOTO Main
      CASE = 05
        DEBUG "key1 is correct"         'lets me know first push is correct
        GOTO checkpush2                 'go to step 2, which is checking for correct push2
    ENDSELECT                           'if second push is not correct then clear previous pushes.
    
    checkpush2:
      SELECT key2
        CASE <> 07                      'correct push not detected.
          GOTO Main                     'leave/clear and start at push1
        CASE = 07
        DEBUG "key2 is correct"         'goto step 3, which is checking for correct push 3.
        GOTO unlock
      ENDSELECT
    
    LOOP
    
    unlock:
      HIGH 15
      PAUSE 5000
      LOW 15
    GOTO Main
    



    *Note: Code is just for 2 buttons above, but will ultimately be for a 6 button keypad (Hopefully!). I'm just trying to get a starting point.
  • rixterrixter Posts: 95
    edited 2010-07-27 00:49
    You have to think about how a combination to a lock is processed. If the user stops at the incorrect number on the first pass, the lock doesn't stop the user and say "wrong first number... start over". No, it waits until the end to tell you "you got it all wrong but I'm not telling you which numbers, if any, you got correct".

    So the code first has to cycle waiting for the first press of a button. It loops until any button is pressed. Once a button is pressed it has to check to see which button is pressed. You also have to make sure the user doesn't press more than one button at a time, or all buttons. So you are not only checking for the correct button, but making sure they didn't press incorrect ones. So you have tp pess the correct one, and ONLY the correct one. A switch variable can be used to track if the user is progressing along pressing the correct buttons. We don't care what they may press incorrectly... just if it is correct or not. So even if the user presses the first button incorrectly, we progress through the three button checking until the last button is pressed. We can also check for an additional press of a RESET (start over) button. The user would press this if they realized they hit a wrong button after pressing it. Of course after the third press, the code can loop back to the start if the sequence was incorrect.

    This code assumes a three button arrangement on pins 1-3 with a reset button on pin 6. All button switches are pulldowns. The correct button sequence is 2-3-1. You may have to tinker with the PAUSE. The idea here is to register a button press without registering a bunch of presses due to holding the button down too long. This is the debounce feature that the BUTTON command can provide, although it won't be easy to implement with multiple button monitoring. You may also want to implement a speaker BEEP as a key press confirmation to aid in user feedback. This may also give you more ideas to play with.


    sw VAR Bit ' correct press switch variable

    first:

    DO WHILE IN1=0 AND IN2=0 AND IN3=0 AND IN6=0
    LOOP

    IF IN6=1 GOTO first ' user pressed reset

    IF IN1=0 AND IN2=1 AND IN3=0 THEN
    sw=1
    ELSE
    sw=0
    ENDIF

    PAUSE 500

    second:

    DO WHILE IN1=0 AND IN2=0 AND IN3=0 AND IN6=0
    LOOP

    IF IN6=1 GOTO first ' user pressed reset

    IF IN1=0 AND IN2=0 AND IN3=1 THEN
    sw=1
    ELSE
    sw=0
    ENDIF

    PAUSE 500

    third:

    DO WHILE IN1=0 AND IN2=0 AND IN3=0 AND IN6=0
    LOOP

    IF IN6=1 THEN GOTO first ' user pressed reset

    IF IN1=1 AND IN2=0 AND IN3=0 THEN
    sw=1
    ELSE
    sw=0
    ENDIF

    IF sw=1 THEN
    DEBUG "correct combination"
    ELSE
    DEBUG "try again!"
    goto first
    ENDIF


    Rick

    Post Edited (rixter) : 7/27/2010 3:23:47 AM GMT
Sign In or Register to comment.