lizardman1499
Posts: 4
Joined: Mon Jul 03, 2017 7:39 pm

Java Pi4J LED Strip Strobing/Flashing

Mon Jul 03, 2017 8:11 pm

Hello,
This is my first post on here so please excuse an improper format. I have recently begun to work on a program to control an RGBW LED strip using Java. I have used the PI4J library and had more success that I expected until now. It seems when I turn on colors that incorporate an R, G, and B value, the led strip begins to strobe very fast, however, the strip works completely fine when just the R, G, B, or W LEDs are turned on. I have done countless hours of research on PWM frequency to find a solution to no avail. I anyone could point me in some direction that would be much appreciated.

Cheers,
Aaron

Below is the "Light Controller" Class I have written and been using to control the LEDs.

Code: Select all

import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.SoftPwm;

public class LightController {
	private final int RANGE = 100;
	private final int RED_PIN;
	private final int GREEN_PIN;
	private final int BLUE_PIN;
	private final int WHITE_PIN;
	private boolean lightsOn;
	private int R_VAL;
	private int G_VAL;
	private int B_VAL;
	private int W_VAL;


	public LightController(int red, int green, int blue, int white) {
		lightsOn = false;
		RED_PIN = red;
		GREEN_PIN = green;
		BLUE_PIN = blue;
		WHITE_PIN = white;
		try {
			Gpio.wiringPiSetup();
			SoftPwm.softPwmCreate(RED_PIN, 0, RANGE);
			SoftPwm.softPwmCreate(GREEN_PIN, 0, RANGE);
			SoftPwm.softPwmCreate(BLUE_PIN, 0, RANGE);
			SoftPwm.softPwmCreate(WHITE_PIN, 0, RANGE);

		} catch (IllegalArgumentException e) {
			System.out.println("Unsupported System...");
		}

	}

	//Converts number for pin output
	private int convertNumForPin(int num, int inMin, int inMax, int outMin, int outMax) {
		return (num - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
	}

	public void setLedColor(int r, int g, int b) {

		R_VAL = convertNumForPin(r, 0, 255, 0, RANGE);
		G_VAL = convertNumForPin(g, 0, 255, 0, RANGE);
		B_VAL = convertNumForPin(b, 0, 255, 0, RANGE);

		if (R_VAL == G_VAL && G_VAL == B_VAL && B_VAL == R_VAL) { //If R, G, and B are equal, turn on White LED
			W_VAL = R_VAL;
			R_VAL = 0;
			G_VAL = 0;
			B_VAL = 0;
		} else {
			W_VAL = 0;
		}

		if (lightsOn) updateLights();

	}

	public void updateLights() { //Change the pin value to the new color
	
		SoftPwm.softPwmWrite(RED_PIN, R_VAL);
		SoftPwm.softPwmWrite(GREEN_PIN, G_VAL);
		SoftPwm.softPwmWrite(BLUE_PIN, B_VAL);
		SoftPwm.softPwmWrite(WHITE_PIN, W_VAL);

	}

	public void lightsOn(int r, int g, int b) {
		lightsOn = true;
		setLedColor(r, g, b);
	}

	public void lightsOff() {
		setLedColor(0, 0, 0);
		lightsOn = false;
	}

	public boolean lightsOn() {
		return lightsOn;
	}
}

User avatar
DougieLawson
Posts: 29790
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Java Pi4J LED Strip Strobing/Flashing

Tue Jul 04, 2017 6:42 am

You've got no delays in your code, so it will spin round as fast as the system can despatch it. Try using

Code: Select all

            //Pause for 0.1 seconds
            Thread.sleep(100);
between each state change for a gpio pin.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

lizardman1499
Posts: 4
Joined: Mon Jul 03, 2017 7:39 pm

Re: Java Pi4J LED Strip Strobing/Flashing

Wed Jul 05, 2017 4:51 am

DougieLawson wrote:You've got no delays in your code, so it will spin round as fast as the system can despatch it. Try using

Code: Select all

            //Pause for 0.1 seconds
            Thread.sleep(100);
between each state change for a gpio pin.
I changed my updateLights function to the following with no success. I may have misunderstood so feel free to correct me if I'm wrong.

Code: Select all

	public void updateLights() { //Change the pin value to the new color
		try {
			SoftPwm.softPwmWrite(RED_PIN, R_VAL);
			Thread.sleep(100);
			SoftPwm.softPwmWrite(GREEN_PIN, G_VAL);
			Thread.sleep(100);
			SoftPwm.softPwmWrite(BLUE_PIN, B_VAL);
			Thread.sleep(100);
			SoftPwm.softPwmWrite(WHITE_PIN, W_VAL);
			Thread.sleep(100);

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

User avatar
DougieLawson
Posts: 29790
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Java Pi4J LED Strip Strobing/Flashing

Wed Jul 05, 2017 5:39 am

Try increasing the length of that delay.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

lizardman1499
Posts: 4
Joined: Mon Jul 03, 2017 7:39 pm

Re: Java Pi4J LED Strip Strobing/Flashing

Wed Jul 05, 2017 6:56 am

DougieLawson wrote:Try increasing the length of that delay.
Still no luck. With more research on the GPIO pins, there is only one that can do PWM. As i need 4 I am going to abadon the Pi and go to an Arduino which has 6 PWM pins.

mattlewis
Posts: 104
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java Pi4J LED Strip Strobing/Flashing

Wed Jul 05, 2017 8:56 pm

If you want to Staci with Java you can try use my pigpio wrapper library: https://GitHub.com/mattjlewis/pigpioj.
pigpio supports optimised PWM output on all GPIOs.
For a user friendly interface you could use diozero (http://www.diozero.com/) which supports pigpio as a backend.

Matt
http://www.diozero.com/

lizardman1499
Posts: 4
Joined: Mon Jul 03, 2017 7:39 pm

Re: Java Pi4J LED Strip Strobing/Flashing

Thu Jul 06, 2017 2:10 am

mattlewis wrote:If you want to Staci with Java you can try use my pigpio wrapper library: https://GitHub.com/mattjlewis/pigpioj.
pigpio supports optimised PWM output on all GPIOs.
For a user friendly interface you could use diozero (http://www.diozero.com/) which supports pigpio as a backend.

Matt
I'm attempting to use your pigpio library and I'm getting this error

Code: Select all

Exception in thread "main" java.lang.UnsatisfiedLinkError: PigpioGpio.initialise()I
	at PigpioGpio.initialise(Native Method)
	at PigpioOutputTest.main(PigpioOutputTest.java:9)

mattlewis
Posts: 104
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java Pi4J LED Strip Strobing/Flashing

Thu Jul 06, 2017 7:25 am

Are you calling

Code: Select all

uk.pigpioj.PigpioJ.getImplementation()
? You need to call this method to ensure that the shared library is loaded. I'm just writing up some usage notes on the GitHub project now.

Matt
http://www.diozero.com/

mattlewis
Posts: 104
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Java Pi4J LED Strip Strobing/Flashing

Thu Jul 06, 2017 7:35 am

I've just noticed my own examples are out of date; updating them too now.

Matt
http://www.diozero.com/

Return to “Java”

Who is online

Users browsing this forum: No registered users and 4 guests