Pi4J minimal example's LED doesn't blink

11 Views Asked by At

I have been learning how to use the Pi4J library and tried to implement the minimal example application that can be found in the Pi4J website

The code that I used is here:

package org.PruebaRaspberry11;

import com.pi4j.Pi4J;
import com.pi4j.io.gpio.digital.DigitalInput;
import com.pi4j.io.gpio.digital.DigitalOutput;
import com.pi4j.io.gpio.digital.DigitalState;
import com.pi4j.io.gpio.digital.PullResistance;
import com.pi4j.util.Console;

public class PruebaRaspberry11 {

    private static final int PIN_BUTTON = 24;
    private static final int PIN_LED = 22;
    private static int pressCount = 0;

    public static void main (String[] args) throws InterruptedException, IllegalArgumentException {
        final var console = new Console();
        var pi4j = Pi4J.newAutoContext();

        var ledConfig = DigitalOutput.newConfigBuilder(pi4j)
                .id("led")
                .name("LED Flasher")
                .address(PIN_LED)
                .shutdown(DigitalState.LOW)
                .initial(DigitalState.LOW);

        var led = pi4j.create(ledConfig);

        var buttonConfig = DigitalInput.newConfigBuilder(pi4j)
                .id("button")
                .name("Press button")
                .address(PIN_BUTTON)
                .pull(PullResistance.PULL_DOWN)
                .debounce(3000L);

        var button = pi4j.create(buttonConfig);

        button.addListener(e -> {
            if (e.state().equals(DigitalState.LOW)) {
                pressCount++;
                console.println("Button was pressed for the " + pressCount + "th time");
            }
        });

        while (pressCount<5) {
            if(led.state().equals(DigitalState.HIGH)){
                led.low();
                console.println("LED low");
            } else {
                led.high();
                console.println("LED high");
            }
            Thread.sleep(500/(pressCount+1));
        }

        pi4j.shutdown();
    }
}

I've tested all the hardware (the LED, the button) to make sure that they work fine. In the console, the "LED high" "LED low" lones are printed, but the LED doesn't blink and nothing happens when I press the button.

What is more strange is that the first time I ran the code it worked, but I tried to do some modifications and never worked again, even after deleting the changes I did.

0

There are 0 best solutions below