Switch Debounce with DAQ1301_0

Any hardware type questions or problems for all other Phidget devices.
Post Reply
Rhybot
Phidgeteer!
Posts: 52
Joined: Wed Aug 14, 2019 4:58 pm
Contact:

Switch Debounce with DAQ1301_0

Post 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?
fraser
Engineering
Posts: 324
Joined: Thu Nov 19, 2009 4:41 pm
Contact:

Re: Switch Debounce with DAQ1301_0

Post 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;
            });
        }
Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests