Page 1 of 1

Switch Debounce with DAQ1301_0

Posted: Tue Aug 20, 2019 8:34 pm
by Rhybot
I have a limit switch hooked up to a digital input from a DAQ1301_0 Phidget.

The switch needs to be debounced. I currently use Software Approach #1 from here:
http://www.labbookpages.co.uk/electronics/debounce.html

I have a solution where basically:
1. On state change, temporarily save the digital input state and a timestamp
2. Start a thread
3. In the thread, monitor for timestamp updates for a certain time window.
- The time window is a debounce timeout (ms)
4. If no timestamp updates during that time window, accept the temporary state as a debounced state

This largely a brute force way. Is there a more elegant method to do debouncing, that integrates with state change handlers?

Re: Switch Debounce with DAQ1301_0

Posted: Wed Aug 21, 2019 10:04 am
by fraser
I don't know if this way is any better, but you can remove the StateChangeHandler inside of the StateChangeHandler, and start a timer for the maximum time expected for a bounce to occur.

When the timer ticks, turn the handler back on and turn the timer off. If you're worried about fast intentional changes within the bounce time, you can check the Digital state at the timer tick as well. something like (in C#):

Code: Select all

        bool global_DeviceState = false;
        Timer StateTimer = new Timer();

        private void Initialize(object sender, EventArgs e)
        {
            device.StateChange += new Phidget22.Events.DigitalInputStateChangeEventHandler(StateChange);
            //Set Interval to expected longest bounce
            StateTimer.Interval = 20;
        }

        private void StateTimer_Tick(object sender, EventArgs e)
        {
            device.StateChange += new Phidget22.Events.DigitalInputStateChangeEventHandler(StateChange);
            StateTimer.Stop();

            //Update value
            global_DeviceState = device.State;
        }

        private void StateChange(object sender, Phidget22.Events.DigitalInputStateChangeEventArgs e)
        {
            device.StateChange -= new Phidget22.Events.DigitalInputStateChangeEventHandler(StateChange);

            Invoke((MethodInvoker)delegate
            {
                StateTimer.Start();

                //Update value
                global_DeviceState = e.State;
            });
        }