Shop OBEX P1 Docs P2 Docs Learn Events
Button Pressing Game Help — Parallax Forums

Button Pressing Game Help

ShaneKShaneK Posts: 9
edited 2012-11-29 09:13 in General Discussion
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:
' {$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

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2012-11-28 11:30
    Shane, welcome to the forums! What you're going to need to count pulses is a state-machine system so you can detect the transistion of the button from one state to the other. If within a loop you're just incrementing a counter when you see a button is high for example, the counter will increment every pass through the loop, most likely several times for one quick press by you. You will need the code to detect the button being pressed and then released before counting it as a press.

    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.
  • ercoerco Posts: 20,259
    edited 2012-11-28 13:53
    Your code needs to count 1's and 0's in a loop to increment your counter only after a press and release is detected.

    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
  • ercoerco Posts: 20,259
    edited 2012-11-28 14:14
    Note that with:

    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
  • ShaneKShaneK Posts: 9
    edited 2012-11-28 19:14
    Shane, welcome to the forums! What you're going to need to count pulses is a state-machine system so you can detect the transistion of the button from one state to the other. If within a loop you're just incrementing a counter when you see a button is high for example, the counter will increment every pass through the loop, most likely several times for one quick press by you. You will need the code to detect the button being pressed and then released before counting it as a press.

    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.

    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.
  • ZetsuZetsu Posts: 186
    edited 2012-11-29 05:38
    ShaneK wrote: »
    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....
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2012-11-29 09:13
    Shane,

    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. =)
Sign In or Register to comment.