I'm creating an arcade cabinet for one firm. In this project I use Pi4j library for GPIO-interaction. I use examples from the official repo to control a LED-strip. The LED-Strip contains 10 LED's. I wrote a code that reads from the console two digits. The first is the number of the first LED, must be switched on. Second digit is the number of the last LED must be switched on. The LEDs between the first and the second must be switched on. Colors are only white. I can not test my code at home. I sent the project to the customer for tests. He sent me the results:
He entered numbers 12 and the LEDs with numbers 8 and 9 (number 0 is the first LED and 9 is the last) were switched on. Than he entered 16 and all LEDs were switched on. I have added the video of the experiments. See it please: Video_test
Do you have experience with the LED-strip control using Raspberry Pi? What can be wrong? Have I forgot something important? The customer has tried to use 3,3V <-> 5V voltage converter for the MOSI pin of the PI but the situation is same.
My code is:
public class LedChainManager extends Manager {
private GpioManager gpioManager;
private LedStrip ledStrip;
private final int leds = 10;
protected int parameter1, parameter2;
public LedChainManager(GpioManager gpioManager) {
this.gpioManager = gpioManager;
init();
}
private void init() {
ledStrip = new LedStrip(gpioManager.getPi4j(), leds, 1);
}
public void execute() {
System.out.println("LED colors changing started");
long startTime = System.currentTimeMillis();
ledStrip.allOff();
ledStrip.setStripColor(PixelColor.WHITE);
ledStrip.render();
delay(20);
if (parameter1 <= parameter2) {
if (parameter1 >= 0 && parameter2 <= 9) {
for (int i = parameter1; i <= parameter2; i++) {
ledStrip.setPixelColor(i, PixelColor.WHITE);
ledStrip.render();
delay(20);
}
}
}
System.out.println("LED strip colors were changed in " + (System.currentTimeMillis()-startTime) + " milliseconds");
}
public void dispose() {
ledStrip.close();
}
public void setParameter1(int parameter1) {
this.parameter1 = parameter1;
}
public void setParameter2(int parameter2) {
this.parameter2 = parameter2;
}
}
LED strip class is a template class. It can be found on github: source for LED_strip
I have changed my code and sent it to the customer again. The execute function is:
public void execute() {
System.out.println("LED colors changing started");
long startTime = System.currentTimeMillis();
ledStrip.allOff();
ledStrip.render();
delay(20);
if (parameter1 <= parameter2) {
if (parameter1 == 0 && parameter2 == 0) {
System.out.println("All LEDs were switched off");
}
else {
if (parameter1 >= 0 && parameter2 <= 9) {
ledStrip.setStripColor(PixelColor.WHITE);
for (int i = 0 ; i < leds; i++){
if (i >= parameter1 && i <= parameter2) {
ledStrip.setPixelColor(i, PixelColor.WHITE);
delay(10);
}
}
ledStrip.render();
}
System.out.println("LED strip colors were changed in " + (System.currentTimeMillis()-startTime) + " milliseconds. LEDs from: " + parameter1 + " to " + parameter2 + " flash");
}
}
}
The result is also not adequate. The video is: here