B4J Question [IOT] piface 2 debounce

Discussion in 'B4J Questions' started by jayel, Feb 22, 2016.

  1. jayel

    jayel Active Member Licensed User


    Can I set debounce time on a digital input?

    Sub Process_Globals

    Private controller As GpioController
    Private pin0,pin1,pin2,pin3,pin4,pin5,pin6,pin7 As GpioPinDigitalInput
    End Sub

    Sub AppStart (Args() As String)

    End Sub

    Sub Pin0_StateChange(State As Boolean)
    Log("Pin0 StateChange event: " & State)
    End Sub
    Sub Pin1_StateChange(State As Boolean)
    Log("Pin1 StateChange event: " & State)
    End Sub
    Sub Pin2_StateChange(State As Boolean)
    Log("Pin2 StateChange event: " & State)
    End Sub
    Sub Pin3_StateChange(State As Boolean)
    Log("Pin3 StateChange event: " & State)
    End Sub
    Sub Pin4_StateChange(State As Boolean)
    Log("Pin4 StateChange event: " & State)
    End Sub
    Sub Pin5_StateChange(State As Boolean)
    Log("Pin5 StateChange event: " & State)
    End Sub
    Sub Pin6_StateChange(State As Boolean)
    Log("Pin6 StateChange event: " & State)
    End Sub
    Sub Pin7_StateChange(State As Boolean)
    Log("Pin7 StateChange event: " & State)
    End Sub
  2. Cableguy

    Cableguy Expert Licensed User

    it would be safer to implement the debouncing through a dedicated circuit... why do you want to do it in code?
  3. Roycefer

    Roycefer Well-Known Member Licensed User

    There are advantages and disadvantages to debouncing in hardware. It's harder to modify a hardware solution than a software solution. Hardware debouncing can also be quite tedious to set up (especially on 8 pins) and might ruin an otherwise elegant hardware design by adding previously unneeded external circuit boards. If you want to do debouncing in software, I suggest you do as follows.

    First, you'll need to determine the characteristic duration of bouncing. An oscilloscope will be helpful here, or you can just examine the logs in your posted code (be sure to add some DateTime.Now so you can see how long the bouncing lasts). For each pin, create a global Boolean variable. You can put them in Array. Here is an example for one pin that assumes bouncing has stopped after 5 milliseconds (Debouncing0 is the global Boolean for this pin):
    Sub Pin0_StateChange(State As Boolean)
    If Debouncing0 Then  'Note that while Debouncing0=False, nothing will happen in this sub
           Debouncing0 = False
    05, State)
    End If
    End Sub

    Sub DebounceDelay(pin As Int, delay As Long, State As Boolean)
    'In this sub, you can use CallSubPlus or a Timer to run a sub in "delay" milliseconds (5 in this example)
        'That sub should set Debouncing0 back to True and then execute whatever code you want in response to the
        'state change (using the State argument)
    End Sub
    Last edited: Feb 23, 2016
  4. jayel

    jayel Active Member Licensed User


    Thanks for the solution... it can be done.
    Now I found in the examples folder of the pi4J (/opt/pi4J) on the raspberry an example :
    // START SNIPPET: listen-gpio-snippet

    * #%L
    * **********************************************************************
    * PROJECT       :  Pi4J :: Java Examples
    * FILENAME      :  DebounceGpioExample.java
    * This 
    file is part of the Pi4J project. More information about
    * this project can be found here:  http://www.pi4j.com/
    * **********************************************************************
    * %%
    * Copyright (C) 
    2012 - 2015 Pi4J
    * %%
    * This program 
    is free software: you can redistribute it and/or modify
    * it under the terms of the GNU Lesser General 
    Public License as
    * published by the Free Software Foundation, either version 
    3 of the
    * License, 
    or (at your option) any later version.
    * This program 
    is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * GNU General Lesser 
    Public License for more details.
    * You should have received a copy of the GNU General Lesser 
    * License along with this program.  
    If not, see
    * <http://www.gnu.org/licenses/lgpl-

    import com.pi4j.io.gpio.*;
    import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
    import com.pi4j.io.gpio.event.GpioPinListenerDigital;

    import java.util.Date;

    * This example code demonstrates how 
    to setup a listener
    for GPIO pin state changes on the Raspberry Pi.
    * @author Robert Savage
    public class DebounceGpioExample {

    public static void main(String args[]) throws InterruptedException {
    "<--Pi4J--> GPIO Debounce Example ... started.");

            // create gpio controller
    GpioController gpio = GpioFactory.getInstance();

            // provision gpio pin 
    #02 as an input pin with its internal pull down resistor enabled
    GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);

            // ----- DEBOUNCE -----
            // Bouncing 
    is the tendency of any two metal contacts in an electronic device to generate multiple signals as
            // the 
    contacts close or open; debouncing is any kind of hardware device or software that ensures that only a
            // single signal will be acted upon 
    for a single opening or closing of a contact.
            // Pi4J supports a debounce feature 
    to suppress state change event notifications on GPIO input pins.
            // This feature allows the consumer 
    to set a debounce delay time in milliseconds.  When a pin state change
            // occurs, the initial 
    event will be raised and the debounce delay timer will be started.  Any subsequent
            // pin state changes will be suppressed 
    until after the debounce delay timer has expired.  When the debounce
            // delay 
    timer expires and if the pin state is different than the start when the debounce timer started, a
            // pin 
    event will be raised to notify the consumer of the pin state change.   (You can optionally set a
            // different debounce delay 
    for each pin state or use the example below to set the same delay time for all
            // pin states)
            // Please note that 
    if you make a call to 'getState()', 'isHigh()' or 'isLow()' the actual current state
            // will be returned, the debounce feature only suppresses event notifications, is does not attempt to
            // mask the actual pin state.

            // create 
    and register gpio pin listener
            myButton.addListener(new GpioPinListenerDigital() {
    public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
                    // display pin state on console
    "[" + new Date() +
    "] --> GPIO PIN STATE CHANGE: " +
    event.getPin() + " = " + event.getState());


            System.out.println(" ... complete the GPIO #02 circuit and see the listener feedback here in the console.");

            // keep program running until user aborts (CTRL-C)
            for (;;) {

            // stop all GPIO activity/threads by shutting down the GPIO controller
            // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
            // gpio.shutdown();   <--- implement this method call if you wish to terminate the Pi4J GPIO controller

    // END SNIPPET: listen-gpio-snippet
    Can't this be implemented in the wrapper that Erel made?

  5. Erel

    Erel Administrator Staff Member Licensed User

    You can call it with JavaObject:
    Dim jo As JavaObject = inputpin
    jayel likes this.
  6. jayel

    jayel Active Member Licensed User

    Thanks, I wil give it a try
  7. jayel

    jayel Active Member Licensed User

    it worked ! thx Erel
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice