GPIO using Pi4J not working as expected

483 views
Skip to first unread message

Sreenath V

unread,
Dec 5, 2013, 8:45:34 AM12/5/13
to pi...@googlegroups.com
I have a magnetic door switch, upon door open and close, my code was working perfectly alright...now that all of a sudden, I see that, when i open the door one time, i see lots of open, close, open close, open etcetra messages (kinda a unstable high and lows)

code here...

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;

public class GPIOTest {
public static void main(String[] args) throws InterruptedException {

System.out
.println("<--Pi4J--> AbstractGPIO Control Example ... started.");

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

try {
GpioPinDigitalInput gpioInput = gpio.provisionDigitalInputPin(
RaspiPin.GPIO_06, PinPullResistance.PULL_UP);

gpioInput.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(
GpioPinDigitalStateChangeEvent event) {
// display pin state on console
System.out
.println(" --> DoorSensorInput PIN STATE CHANGE: "
+ event.getPin() + " = " + event.getState());
if (event.getState() == PinState.HIGH) {
System.out.println("Door -> opened");
} else {
System.out.println("Door -> closed");
}
}
});
Thread.sleep(15000);

} catch (Exception e) {

} finally {
// stop all AbstractGPIO activity/threads by shutting down the
// AbstractGPIO
// controller
// (this method will forcefully shutdown all AbstractGPIO monitoring
// threads
// and scheduled tasks)
gpio.shutdown();
System.out.println("Cleaned up...");
}
}
}

Please let me know why am i not getting one message like Opened or Closed....



Raul Luna Rodriguez

unread,
Dec 15, 2013, 5:54:41 PM12/15/13
to pi...@googlegroups.com
I started a project that allows you to debug your application using your IDE of choice, I think it could help: http://sourceforge.net/projects/testpi4j/

Andrew Chandler

unread,
Jan 8, 2014, 2:38:34 PM1/8/14
to pi...@googlegroups.com
I'm not an expert but typically with a switch you have to "de-bounce" it - the mechanical state change often has a messy transition that requires you to take the initial change as a start event and then wait a little while before listening to it again.   An electrical engineer could do a better job explaining it likely but when your resolution gets down to millisecond level you are able to detect the "bouncing" of the contacts.   I think if you google "switch debounce" you'll get a very clear explanation of what it is and how to solve it - it can be solved electrically and/or in software.

Raul Luna Rodriguez

unread,
Jan 10, 2014, 2:01:06 AM1/10/14
to pi...@googlegroups.com
Other way to mitigate this behavior is by installing a capacitor in the contacts of the switch, to absorb some of the variations while it is changing its state.

However it is an easiest way to simply wait until the signal its stabilized.

Reply all
Reply to author
Forward
0 new messages