Button Pressing Game Help
ShaneK
Posts: 9
Hi!
I am a highschooler and I am looking for some more advanced help than my teacher can supply to me. I have this code here for a basic button pressing game. It will count how many times you press a button in a matter of seconds. I am having trouble because when I run the code, if I just hold down the button, it will continuously add 1 to the counter variable. I need help so that it will only add 1 every time you press the button down, even if you hold the button it will only add 1.
I am using a Board of Education with a BS2. I would love any help that anyone can give me! Thanks!!
Here is my code:
I am a highschooler and I am looking for some more advanced help than my teacher can supply to me. I have this code here for a basic button pressing game. It will count how many times you press a button in a matter of seconds. I am having trouble because when I run the code, if I just hold down the button, it will continuously add 1 to the counter variable. I need help so that it will only add 1 every time you press the button down, even if you hold the button it will only add 1.
I am using a Board of Education with a BS2. I would love any help that anyone can give me! Thanks!!
Here is my code:
' {$STAMP BS2} ' {$PBASIC 2.5} counter VAR Word time VAR Byte DEBUG CLS DEBUG "Button pressing game!!",CR, "Press the top button when ready to play!",CR, "Then, press the bottom button as many times", CR, "as possible once the light turns green.",CR HIGH 14 LOW 15 DO LOOP UNTIL IN4 = 1 DEBUG CLS PAUSE 750 LOW 14 HIGH 15 counter = 0 time = 1 DO DEBUG HOME DEBUG DEC time, CR DEBUG DEC counter,CR IF IN3 = 0 THEN time = time + 1 PAUSE 10 ELSEIF IN3 = 1 AND time < 200 THEN time = time + 1 counter = counter + 1 PAUSE 10 IN3 = 0 ENDIF IN3 = 0 LOOP UNTIL time = 200 LOW 15 DEBUG CR DEBUG "You pressed the button " DEBUG DEC counter DEBUG " times in five seconds!"
Comments
Now, this adds another element for consideration...if you're trying to time the loop you're going to have issues since the loop will stop periodically. If you're trying to count button presses within a fixed length of time you will need an external RTC which you can trigger at the start and then read the elapsed time within your loop. This will give you a resolution of 1 second.
Hopefully I understand properly what you're doing so that my explanation makes sense.
And to discount switch bounce, you probably need a brief pause after each. Something like (untested):
' pushbutton on pin 0
main:
b2=0
for w0=1 to 1000 ' adjust # for timing interval
zero: if in0=0 then zero ' wait for press
pause 50
one: if in0=1 then one ' wait for release
pause 50
b2=b2+1 ' increment counter
next
debug dec b2, CR
goto main
w0 = 1 to 1000, the timing interval is ~100 seconds, more than you bargained for.
for w0=1 to 20 will make your interval ~2 seconds
Chris,
Thank you for your help, although I am fairly new at coding with the PBASIC language, I understood a good amount of what you suggested to me. Everything seemed to be pointed in the direction that I was heading. Although, I found a simple way of incorporating what I was in need of. I used a COUNT command to count the amount of times the pin 3 recieved a high signal within a set time of 5 seconds. That number would be stored at counter which is later displayed. It looks a little like this,
COUNT 3, 5000, counter
It seems to be working great.
Thank you for your help,
Shane K.
What happens if I hold down the button for more then 5 seconds....
I'm glad the COUNT command will work for you. I didn't suggest it for two reasons...it is usually used for short durations, which is probably what you want, but there is a maximum duration that COUNT will work for (65.535 seconds). Also, the minimum pulse width it will detect is 4.16uS which may mean catching some contact debounce when used with mechanical pushbuttons. There's also no way to break out of it if you should wish to stop counting. The BASIC Stamp Module will wait until the elapsed time before it will execute any other commands. Just some things to keep in mind for those who might also want to use COUNT.