Shop OBEX P1 Docs P2 Docs Learn Events
Debounce used in conjunction with "Select Case" and reserved word (INA) — Parallax Forums

Debounce used in conjunction with "Select Case" and reserved word (INA)

ZeusZeus Posts: 79
edited 2013-03-19 19:08 in BASIC Stamp
All,

Is the above even possible, and if so does anyone have an example of how it would be employed? I am working with the following code and I have tried several configurations just to get it to compile without generating errors but yet I do not seem to be get a debounced input.
' -----[ Subroutines ]-----------------------------------------------------

Set_Mode:

  SEROUT LCD, Baud, [22, 17, $0C]               ' "$0C" Clears LCD Display.
  SEROUT LCD, Baud, ["Enter 2 digit year"]

  BUTTON INA, 0, 200, 200, wkspc ,1 , Set_Mode

  DO UNTIL (IN4 = 0)

  SELECT INA & 3                       ' Possibilities are 00, 01, 10 and 11.
   CASE = 0                            ' Default
     year = 13

    CASE = 1                           ' Count down
     year = year - 1

    SEROUT LCD, Baud, [148, DEC2 year]

    CASE = 2                           ' Count up
     year = year + 1

    SEROUT LCD, Baud, [148, DEC2 year]

  ENDSELECT

  PAUSE 250

  LOOP

Thanks, Zeus

Comments

  • davejamesdavejames Posts: 4,047
    edited 2013-03-17 18:47
    Hi Zeus - odd question for you:

    --->what do expect to happen with the code that you've shown?

    Armed with that information, there may be someone able to help out.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-03-17 19:07
    BUTTON INA, 0, 200, 200, wkspc ,1 , Set_Mode

    You've got the BUTTON command wrong. It only debounces the input on 1 single pin, not the pin group inA. You will not get what you expect. It will read the value on the 4-bit port inA, and use that value as a pin number to debounce. Also, I doubt if you need the autorepeat function. Actually, the BUTTON command is not at all what you want. You could individually debounce pins p0 and p1, but you really do want to do it as a group. Maybe something like the following for a debounce subroutine, looking for inA&3 to be in a stable state for 0.2 second...
    [SIZE=1][FONT=courier new]new var nib    ' state variables
    old var nib
    stable var nib
    
    
    debounce:
      old = inA & 3
      stable = 0 
    debounce_loop:
      DO
    [/FONT][/SIZE][SIZE=1][FONT=courier new]    pause 20   ' key down for 0.2 second to be recognized.
    [/FONT][/SIZE][SIZE=1][FONT=courier new]    new := inA & 3
        IF old = new THEN stable = stable+1 else stable = 0
        old = new
      LOOP UNTIL stable>10  [/FONT][/SIZE][SIZE=1][FONT=courier new]' got a debounced inA&3 in variable new[/FONT][/SIZE]
    
  • ZeusZeus Posts: 79
    edited 2013-03-18 15:07
    Dave,

    I need to debounce my inputs (4 total) and my stamp is reading multiple button strikes currently. I would like to use the BUTTON auto-repeat function as well. I am having trouble specifically using the BUTTON command with the "INA & 3" statement (I am not sure how to incorporate this with a "Select Case" statement either). What I would like to happen is to press the inputs for a reasonable amount of time without multiple detections being registered, and if the input is held down then take advantage of the auto-repeat function as this code is for setting the time on a clock project that I am working on.

    Tracy,

    Always happy when I see that you have replied to one of my posts but your latest reply has got me running in circles a bit. I am still trying to understand the BUTTON command so your code above is lost on me right now. To back up for a minute, I want to use the auto-repeat function, so is the BUTTON command still the wrong choice given what I am trying to accomplish?

    Thanks to the both of you.

    Zeus
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-03-18 16:40
    Okay, I'm getting a better picture of what you want to do. Still a bit hazy though, because the code you posted only does p0, p1 and p4. The last one is not on inA, and it seems to be your "enter" key.

    I see why you want to have the autorepeat to move the time values up and down. You'll need two BUTTON commands and two workspace variables (bytes), also a special condition to detect when both buttons are down.
    [SIZE=1][FONT=courier new]DO UNTIL in4=0
         BUTTON 0, 0, 200, 200, wkspc0 ,1 , decrement
         BUTTON 1, 0, 200, 200, wkspc1 , 1, increment
         IF inA&3 = 0 THEN year=13    ' both buttons
         GOTO again
     decrement:
           year = year - 1
           SEROUT LCD, Baud, [148, DEC2 year]
           GOTO again
    increment:
           year = year+1
           SEROUT LCD, Baud, [148, DEC2 year]
    again: LOOP[/FONT][/SIZE]
    

    The separate button commands implement the autorepeat separately on the two pins, p0 and p1. Like I said above, you can't put inA in the spot where a pin number belongs. There are two separate workspace variables and a special condition for both buttons down at once. The loop executes rapidly over and over, but only executes the autorepeat at the programmed frequency (once every 200th time around the loop). You'll have to play with the autorepeat parameters to get the right feel.
  • davejamesdavejames Posts: 4,047
    edited 2013-03-18 18:22
    ...thanks for the explanation. I see Mr. Allen is "on it" and will yield the floor to him!
  • ZeusZeus Posts: 79
    edited 2013-03-19 19:08
    Tracy,

    A few things... very helpful to know that I cannot globally debounce all five of my buttons with one statement.This explains once and for all why I cannot get it to compile no matter what the configuration. You are correct I really only need the three buttons debounced; up, down and enter. Also, I really don't need the two button command, that was just a nice to have. The bigger issue by far is debouncing the up and down values.

    Your latest bit of code is much more my speed, the more I look at it the more I like it. There is quite a bit going on yet at the same time it is still easy to follow. Thank you again for helping me out.

    Zeus
Sign In or Register to comment.