Correct Sequence of Events (Momentary Pushbutton N.O.)
perception13
Posts: 3
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.)
Again, i know its not pretty.
To compare this to PLC. It seems I need to LOCK a bit high.
Need Help
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
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
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
Thanks Rick, But I'm not exactly sure how to do that.
I'll show you what I've come up with:
*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.
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