Reputation: 323
I use Pi4j on a Raspberry Pi to control a LED strip. It is made on chip ws2811. I have rewrote the example from the official homepage to have the ability to switch on some LEDs. The color is only white. I set the values of the first LED (parameter1) and last LED (parameter2) from external classes. When I call the function execute() I want that the LEDs with numbers from parameter1 to parameter2 will be switched on. But in the reality all the LEDs are shifted on one LED to end. For example: when I set parameter1 on 0 and parameter2 on 4 the LEDs from 1 to 5 will be switched on. But must be LEDs with numbers 0 to 4. Yesterday after this command the LED with the number 0 flashed red and from 1 to 5 - white. Help me please find the error.
public class LedChainManager extends Manager{
private LinuxRaspberryPiGpioManager linuxRaspberryPiGpioManager;
private static LedStrip ledStrip;
private final int leds = 10;
private int parameter1, parameter2;
public LedChainManager(LinuxRaspberryPiGpioManager linuxRaspberryPiGpioManager) {
this.linuxRaspberryPiGpioManager = linuxRaspberryPiGpioManager;
init();
}
private void init() {
ledStrip = new LedStrip(linuxRaspberryPiGpioManager.getPi4j(), leds, 1);
}
protected void delay(long milliseconds) {
try {
Thread.sleep(milliseconds);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public void setParameter1(int parameter1) {
this.parameter1 = parameter1;
}
public void setParameter2(int parameter2) {
this.parameter2 = parameter2;
}
public void execute() {
Logger.logGpio("LED colors changing started");
ledStrip.allOff(); //Sometimes some LEDs stay switched on or start to flash red
delay(5);
ledStrip.allOff(); //I clear all the LEDs again.
if (parameter1 <= parameter2 ){
if (parameter1 == 0 && parameter2 == 0){ //When parameter1 and parameter2 are 0 the LED strip must be switched off
Logger.logGpio("All LEDs were switched off");
}
else {
long startTime = System.currentTimeMillis();
if (parameter1 >= 0 && parameter2 <= 9) {
for (int i = 0 ; i < leds; i++){
if (i>=parameter1 && i <= parameter2) {
ledStrip.setPixelColor(i, PixelColor.WHITE);
}
}
ledStrip.render();
}
Logger.logGpio("LED strip colors were changed in " + (System.currentTimeMillis()-startTime) + " milliseconds. LEDs from: " + parameter1 + " to " + parameter2 + " flash");
}
}
}
public void dispose(){
ledStrip.close();
}
}
Upvotes: 0
Views: 113