Shop OBEX P1 Docs P2 Docs Learn Events
Button Counter — Parallax Forums

Button Counter

I am new to this and need to create a program that will calculate the number of times a button has been pressed within a 10 second time limit. At the end of this 10 second time period, the count of presses should be displayed to the screen. I have the following code, although for some reason it is not working. I have tested the board and the button, it works, so there is something wrong with my code that I am looking over. Please help!


'{$STAMP BS2}
'{$PBASIC 2.5}

Counter VAR Word 'counted taps

Main:
DO
DEBUG CLS, "How many times can you push the button in 10 seconds?", CR
PAUSE 1000
DEBUG "Ready, set... "
PAUSE 500
DEBUG "GO!", CR

COUNT 0, 10000, Counter 'Button is connected at PIN 0

DEBUG CR, "Your score: ", DEC Counter, CR
PAUSE 3000
DEBUG "Press button if you want to go again."
DO : LOOP UNTIL (IN3=1)'wait for button to be pressed
LOOP

Comments

  • annamaria,

    Welcome to the Parallax forums.

    I made two changes to your program and it runs just fine. First, where you wait for a button press to go again, I changed the IN3 to IN0, assuming you had just a single button wired up. Second, since a button press shows up as a zero on my setup, I changed the =1 to =0. That may not be needful for your setup. So the entire instruction is as shown below.
    Have fun and best of luck with your BS-2.
    {$STAMP BS2}
    '{$PBASIC 2.5}
    
    Counter VAR Word 'counted taps
    
    Main:
    DO
    DEBUG CLS, "How many times can you push the button in 10 seconds?", CR
    PAUSE 1000
    DEBUG "Ready, set... "
    PAUSE 500
    DEBUG "GO!", CR
    
    COUNT 0, 10000, Counter 'Button is connected at PIN 0
    
    DEBUG CR, "Your score: ", DEC Counter, CR
    PAUSE 3000
    DEBUG "Press button if you want to go again."
    DO : LOOP UNTIL (IN0=0)'wait for button to be pressed
    LOOP
    
Sign In or Register to comment.