narancsikon
Posts: 1
Joined: Tue Apr 23, 2019 5:40 pm

Setup button and programing in JAVA

Tue Apr 23, 2019 6:00 pm

Hi!

I make a project where i need to use a button as input. I don't understund why doesn't work it. I link my program and the wiring as well. I use the pull down resistor. If I press the button, nothing happens.

Code: Select all

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

	        GpioController gpio = GpioFactory.getInstance();
	        GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_04, PinPullResistance.PULL_DOWN);
	       
	        myButton.addListener(new GpioPinListenerDigital() {

	    		@Override
	    		public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
	    			System.out.println("myButton");
	    			
	    			event.getState();
	    			if (event.getState() == PinState.HIGH) {
	    				System.out.println("HIGH");
	    			} else {
	    				System.out.println("LOW");
	    			}
	    		}
	    	});
	        System.out.println(" ... complete the GPIO #02 circuit and see the listener feedback here in the console.");

	        while(true) {
	            Thread.sleep(500);
	        }
	    }
Image

knute
Posts: 424
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: Setup button and programing in JAVA

Wed Apr 24, 2019 1:40 pm

I'm not sure what your problem with your circuit is but I would change the way it works to use a pull up on the input pin and then just ground the gpio pin with the button. No resistor needed.

Return to “Other projects”