I trying out different java programs for my RPi B+ V1.2 and as far I have only tried to blink with a lamp for a few seconds. It works.
Now I want to try to blink with a lamp by using a button. In this case, I need to use a listener for my button.
I have been using this java code from this page:
import com.pi4j.io.gpio.*;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
/**
* This example code demonstrates how to setup a listener
* for GPIO pin state changes on the Raspberry Pi.
*
* @author Robert Savage
*/
public class ListenGpioExample {
public static void main(String args[]) throws InterruptedException {
System.out.println("<--Pi4J--> GPIO Listen Example ... started.");
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// provision gpio pin #02 as an input pin with its internal pull down resistor enabled
final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);
// set shutdown state for this input pin
myButton.setShutdownOptions(true);
// create and register gpio pin listener
myButton.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
// display pin state on console
System.out.println(" --> 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)
while(true) {
Thread.sleep(500);
}
// 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
}
}
This listener won't even print out a single message from the method handleGpioPinDigitalStateChangeEvent
I have WiringPi installed:
I'm trying connect GPIO_02 to 3.3v but nothing happes:
Have I forgot something? It was a long time ago I worked with microcontrollers. Mabey I have missed something?
One weird thing is that if I write this command:
Then GPIO18 will go HIGH/LOW and ligth a LED lamp.
Then I tried this pinmap
And now it wokring.
Question:
Why does my Raspberry Pi B+ has the Raspberry Pi 2 B pinmap?