0

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

0andriy
  • 4,183
  • 1
  • 24
  • 37

1 Answers1

0

IMHO your execute() method is wrong.

After turning of all LEDs (ledStrip.allOff()) you immediately turn them on again (ledStrip.setStripColor(PixelColor.WHITE);):

    public void execute() {
        System.out.println("LED colors changing started");
        long startTime = System.currentTimeMillis();
        ledStrip.allOff();
        ledStrip.setStripColor(PixelColor.WHITE); // ?! do you really want to turn on all pixels?
        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");
    }

To verify this you could change that line to

        ledStrip.setStripColor(PixelColor.RED);
0andriy
  • 4,183
  • 1
  • 24
  • 37
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
  • I have fixed this before the second test. I don't know why I published the previous code here. I have edited the question and add the newest code at the start of the topic. Your idea was applied and used for the code repair. The customer sent me the new result. The result is again very strange. See it please: https://www.youtube.com/watch?v=qL6ph2J-_BM – Alexander Gorodilov Apr 06 '23 at 07:56
  • Thanks, I see, You are right. On every fix stage in every experiment I use in my code ledStrip.setStripColor(PixelColor.WHITE) .I thought this function only selects the color for the next work but this function renders the LED chain. Thank you very much! – Alexander Gorodilov Apr 06 '23 at 08:21