0

I am new to using Raspberry Pis GPIO and I am using pi4j to control it. I simply tried to make a Button work, that only triggers when releasing the button.

Here is my code

        if (Gpio.wiringPiSetup() == -1) {
            System.out.println(" ==>> GPIO SETUP FAILED");
            return;
        }
        gpio = GpioFactory.getInstance();
        final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, PinPullResistance.PULL_UP);
        myButton.setDebounce(1000);

        GpioUtil.setEdgeDetection(RaspiPin.GPIO_00.getAddress(), GpioUtil.EDGE_FALLING);
        myButton.addListener(new GpioPinListenerDigital() {
            @Override
            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
                System.out.println("GPIO 0" + event.getEdge());
                // do something
            }

        });

I also tried using Gpio.wiringPiISR, Code here:

        Gpio.pinMode(2,  Gpio.INPUT);
        Gpio.pullUpDnControl(2, Gpio.PUD_UP);
        Gpio.wiringPiISR(2, Gpio.INT_EDGE_FALLING, new GpioInterruptCallback() {
            @Override
            public void callback(int pin) {
                System.out.println("2nd callback called" + pin);
                // do something
            }
        });

Problem with first Code: It should only print "GPIO 0falling" but also prints "GPIO 0rising" Problem with 2nd Code: I am not even sure if edge detection works, because I could not find any method to debounce the pins input.

I am very close to examples from pi4j libary, but I must be doing something wrong. Can anyone help me out with this?

Note: I am using a Raspberry Pi 3 Model B (dont know if it could matter).

  • You say close to an example. Why not just use an example which checks for a falling edge? – joan Dec 26 '18 at 16:42
  • I simply changed the System.out.println statements but can not find the example anymore. I think I will check every edge by using if(event.getEdge()==...), even though it does not really look nice. I will probably switch to python anyway soon. The pi4j libary seems pretty messy to me. – user84037 Dec 27 '18 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.