Shop OBEX P1 Docs P2 Docs Learn Events
Second newest to the newest Newbie question?? — Parallax Forums

Second newest to the newest Newbie question??

Hard TimesHard Times Posts: 7
edited 2004-08-05 15:07 in BASIC Stamp
Is there a good "Generic" piece of code that can be used as a debouncer for an input?·I've seen the "BUTTON" command, but I want to·observe how some simple code·would work instead.(BUTTON is over my head at this point of my learning).·I've seen some examples in other folks projects, but they don't seem to use any standard method.·
The input at IN3 is what·I need to debounce, follows:
··
·· DO
····· IF IN3 = 1 THEN
·········· runnumber = runnumber + 1
········ IF runnumber·< = 3 THEN
········ HIGH 14
········ PAUSE 3000
········ etc.
········ etc.
·· LOOP

Thank you for your time and patience regarding this small matter.
·

Comments

  • AlWilliamsAWCAlWilliamsAWC Posts: 135
    edited 2004-08-03 19:44
    In many cases debouncing switch input is vastly overrated. For example, suppose that you want someone to push a button. That button signals a PC via a serial port and starts a 3 minute multimedia presentation. When the PC is done, it signals the Stamp that it is ready to fire again.

    You don't need debouncing in this case. Once you see the switch go down, it will be minutes before you care what state the switch is in anyway. This leads to a simple observation about detecting bounce:

    ' assume switches go low when pressed
    noswitch:
    · if in8=1 then noswitch
    · pause 10· ' wait 10mS
    · if in8=1 then noswitch· ' false alarm
    ' OK switch was really pressed and stayed pressed for over 10mS

    That's one way to do it. Keep in mind that the Stamp's step speed is high enough that in many cases, you simply don't care (although the above scenario was extreme to make a point).

    Regards,

    Al Williams
    AWC
    Stamp Prototyping Board
    http://www.awce.com/asp3.htm
    ·
  • Hard TimesHard Times Posts: 7
    edited 2004-08-03 23:22
    Got it Al. I kinda guessed/ thought that a PAUSE 3 (or so) and then a re-query would indicate that the switch closure was real and not a bounce, but I wanted to make sure. You have helped me twice now with my project. I am grateful for your advice. Still learning----

    Many honest Thanks,
    Bill
  • Tracy AllenTracy Allen Posts: 6,656
    edited 2004-08-04 02:12
    "Debounce" can mean a couple of different things. The example Al gave addresses both #1 and #2. The BUTTON command addresses only #1 below.
    1) You want a code sequence to happen only once when you press a button, even if you keep the button held down. The button has to come up and be detected as such and then go down again before it counts as a second event. If everything in your system centers around one button, then it can be as simple as,
    DO
    DO:LOOP WHILE in0 ' wait until in0 is low
    FREQOUT 1,1000,1000 ' the on buttondown code goes here
    DO:LOOP UNTIL in0 ' wait until in0 is high
    LOOP

    If you want the event to happen "On buttonup" (like a mouse click), then you would rearrange the above lines.

    Sometimes your program has a lot more to do than just look at one button all the time. Al suggested a situation where the button might start a process that takes minutes, and the person pressing the button has probably let go by that time. But there are situations where you really do want to know that the button has been released before the special code reruns. In that case you need one bit extra state variable.
    oldbuttonstate VAR bit
    newbuttonstate VAR bit
    DO
    newbuttonstate = in0
    IF newbuttonstate ^ oldbuttonstate & oldbuttonstate THEN GOSUB wowzer
    oldbuttonstate = newbuttonstate
    ' other program code goes here
    LOOP

    The special code wowzer in only executed when in0 goes from 1->0, and then it has to be detected as a 1 again before wowzer will run again. The trick is in the logic statement in the IF. This is the kind of code you will almost always find in debouncing, that or a bunch of IF THEN statements that amount to the same thing. The XOR operator, ^, between the old and the new bit will result in a one only if the two states are different, and then the AND operator & filters it further so that the only trigger will be the 1->0 transition.

    The BUTTON command in the Stamp uses similar logic, but it uses a whole byte instead of a single bit for the state. And it adds the capability for autorepeat and delay, which I've never found useful, but it is an interesting command. I have a writup on its inner workings on my web page at www.emesystems.com/BS2fsm.htm#BUTTON .

    #2 ) The second meaning of "debounce" is noise immunity. The classic case is a situation where the contacts of a switch will actually bounce and with a pullup resistor they will make and break contact within a period of a few milliseconds. If you hook up a switch to the Stamp and use the COUNT command, you can often see it bounce as the Stamp detects multiple counts for each closure of the switch. Often the best way to deal with that is with hardware, an capacitor and resistor RC circuit. The delay Al mentioned and that you mentioned and then a second sample is another way to deal with it, in software. Some on-off processes (more generally than a simple pushbutton) can be quite noisy and it may take counter to sort out the real event from the noise. It might be an local counter as you suggested in your initial posting, or debouncing might be one task that the program performs in a main loop.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • ChipCircuitChipCircuit Posts: 23
    edited 2004-08-04 19:31
    Please, please, please moderators, say you can chang the title of a thread !


    Dave


    (btw, NICE on that reading without signing in thing ! )
  • DerrickDerrick Posts: 1
    edited 2004-08-05 06:59
    You could·solve the problem another way.· Add a debounce circuit.

    http://www.mitedu.freeserve.co.uk/Circuits/Switching/debounce.htm

    Very simple and inexpensive, but does add additional components.· When you are ready to actually build the circuit, you could use surface mount components and wouldn't take up very little space.
  • KenMKenM Posts: 657
    edited 2004-08-05 15:07
    The IC described below is "old school" but very effective. The user can easily set the time the pin must be in a state before the output changes.

    Personally I prefer to do this in software.


    http://www.onsemi.com/pub/Collateral/MC14490-D.PDF
Sign In or Register to comment.