0

I have the following Java program to turn off the LED connected to wiringpi GPIO_07 at start, then blink based on user input.

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import java.util.Scanner;

public class led {
    public static void main (String[] args) throws InterruptedException{
    System.out.println("LED Control System 1.0 Started");
    final GpioController gpio = GpioFactory.getInstance();
    final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, "LedGreen", PinState.HIGH);
    pin.setShutdownOptions(true, PinState.LOW);
    pin.low();
    Scanner s = new Scanner(System.in);
    System.out.print("Enter Blink Speed  in Seconds> ");
    String speed  = s.nextLine();

    System.out.print("Enter times to blink> ");
    String times = s.nextLine();
    pin.blink(Integer.parseInt(tempo),Integer.parseInt(times) * Integer.parseInt(tempo));


    }
}

When gpio.provisionDigitalOutputPin is called the LED lights up. However, when pin.low() is called the LED is still on.

When the program is terminated or gpio.shutdown() is called the LED shuts down.

After I enter the input, the LED does not blink.

I am using a Raspberry Pi 3 B.

  • what happens if you reverse the LED state, start LOW and then turn on the LED? – jsotola Sep 1 '18 at 19:33
  • see this page ... pi4j.com/example/shutdown.html .... connect as per diagram and run the code .... does the LED respond as expected? – jsotola Sep 1 '18 at 19:48
1

I presume you want to get this working.

I suggest you first find out if the software or the hardware is at fault.

With that in mind I suggest the following steps.

  1. Remove the wire from wiringPi 7 (which happens to be physical pin 7).
  2. In a terminal window enter the command sudo pigpiod. This starts the pigpio daemon in the background.
  3. Download and unzip monitor.py.
  4. In a terminal window enter the command ./monitor.py
  5. In a separate terminal window run your Java script.

monitor.py should report level changes of GPIO 4 at the blink rate (GPIO 4 is wiringPi pin 7).

If monitor.py reports the level changes then the LED wiring is faulty, otherwise the Java script is faulty.

  • monitor.py does not report anything. What are the problems in the Java script? – The Old Man and the C Aug 30 '18 at 13:16
  • I have no idea, I don't use Java. I did not expect that result. Put the LED back and enter the following command in a terminal window pigs pfs 4 0 p 4 128 mils 2000 p 4 0. Does the LED blink? – joan Aug 30 '18 at 14:04
  • I did see monitor.py report things saying G=17... – The Old Man and the C Aug 30 '18 at 14:08
  • Only when the Java script was running? GPIO 17 is wiringPi 0, physical pin 11, see pinout.xyz – joan Aug 30 '18 at 14:11
  • The Java script is not terminating anymore... – The Old Man and the C Aug 30 '18 at 14:16
0
final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, "LedGreen", PinState.HIGH);

how about some delay here and then same code as above with PinState.LOW

pin.setShutdownOptions(true, PinState.LOW);

pin.low();  is this "low" correct syntax here ?

Scanner s = new Scanner(System.in);
  • How long should be the delay – The Old Man and the C Aug 30 '18 at 19:28
  • About a second so you can test observe the LED to go off - from on state you know it works. – Jan Hus Aug 30 '18 at 23:13
  • When I use provisionDigitalOutputPin the second time with PinState.LOW a GpioPinExistsException is thrown – The Old Man and the C Aug 31 '18 at 10:59
0

Sorry, but it makes sense. Perhaps you need somebody with Java background to truly analyze your code. All we know for sure the LED turns on and off . You need to find at what point it turns off by stepping thru the code.

final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, "LedGreen", PinState.HIGH);

LED on HERE ?

pin.setShutdownOptions(true, PinState.LOW);

Stop processing here – use breakpoint , exit or some kind of infinite loop , delay Whatever method you are comfortable with, does not matter.

LED still on?

pin.low();
  • pin.low() does not turn the LED off but provisionDigitalOutputPin does turn the light on – The Old Man and the C Sep 1 '18 at 10:55
0

I had the same problem, so I ended up writing my own class

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;

public class Light {

    private final Pin pinNumber = RaspiPin.GPIO_07;

    private boolean status;
    private GpioController gpio;
    private GpioPinDigitalOutput pin;
    private Thread t;

    public Light() {
        gpio = GpioFactory.getInstance();
        pin = gpio.provisionDigitalOutputPin(pinNumber, "LedGreen", PinState.LOW);
        pin.setShutdownOptions(true, PinState.LOW);
        status = pin.isHigh();

    }

    public boolean isOn() {
        return status;
    }

    public void blink(int duration) {
        t = new Thread(new Runnable() {

            @Override
            public void run() {
                while (status) {
                    try {
                        pin.high();
                        Thread.sleep(duration);
                        pin.low();
                        Thread.sleep(duration);
                    } catch (InterruptedException e) {
                        pin.low();
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
    }

    public void enable(int duration) {
        status = true;
        blink(duration);
    }

    public void disable() {
        status = false;
        t.interrupt();
    }

    public void shutdown() {
        disable();
        gpio.shutdown();
    }

}

Pop that class in with your current project, then you can create an instance of it and call enable(duration) and disable(duration) to start or stop the led from blinking. In your case, your main would look something like this:

public static void main(String[] args) throws InterruptedException {
    System.out.println("LED Control System 1.0 Started");

    Light led = new Light();

    Scanner s = new Scanner(System.in);
    System.out.print("Enter Blink Speed  in Seconds> ");
    String speed = s.nextLine();

    System.out.print("Enter times to blink> ");
    String times = s.nextLine();
    led.enable(Integer.parseInt(tempo), Integer.parseInt(times) * Integer.parseInt(tempo));


   //to stop the blinking, use
   //led.disable();
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.