0

I just started my developments with wiring pi but i'm facing some problems with method not always being executed on Raspberry.

    // LED1
    SoftPwm.softPwmCreate(0, 0, 100); // RED
    SoftPwm.softPwmCreate(1, 0, 100); // GREEN
    SoftPwm.softPwmCreate(2, 0, 100); // BLUE

    // LED2
    SoftPwm.softPwmCreate(3, 0, 100); // RED
    SoftPwm.softPwmCreate(4, 0, 100); // GREEN
    SoftPwm.softPwmCreate(5, 0, 100); // BLUE

    for (int i = 0; i < 10; i++) {
        SoftPwm.softPwmWrite(0, 0);
        SoftPwm.softPwmWrite(1, 100);
        SoftPwm.softPwmWrite(2, 0);
        SoftPwm.softPwmWrite(3, 0);
        SoftPwm.softPwmWrite(4, 100);
        SoftPwm.softPwmWrite(5, 0);
        Thread.sleep(2000);
        SoftPwm.softPwmWrite(0, 0);
        SoftPwm.softPwmWrite(1, 0);
        SoftPwm.softPwmWrite(2, 0);
        SoftPwm.softPwmWrite(3, 0);
        SoftPwm.softPwmWrite(4, 0);
        SoftPwm.softPwmWrite(5, 0);
        Thread.sleep(2000); //THIS WAS MISSING
    }

    Thread.sleep(2000);

    SoftPwm.softPwmWrite(0, 100);
    SoftPwm.softPwmWrite(1, 0);
    SoftPwm.softPwmWrite(2, 0);
    SoftPwm.softPwmWrite(3, 100);
    SoftPwm.softPwmWrite(4, 0);
    SoftPwm.softPwmWrite(5, 0);

    Thread.sleep(2000);

    SoftPwm.softPwmWrite(0, 0);
    SoftPwm.softPwmWrite(1, 0);
    SoftPwm.softPwmWrite(2, 0);
    SoftPwm.softPwmWrite(3, 0);
    SoftPwm.softPwmWrite(4, 0);
    SoftPwm.softPwmWrite(5, 0);

This is nothing complex but in the for loop the led never turns off, and sometimes the last piece ( turn off the leds) doesn't happen.

Do you have any idea why? I have an Pl9823-F8 led

  • "sometimes the last piece ( turn off the leds) doesn't happen." -> Please add a photograph or diagram of exactly how you have the LEDs wired up. – goldilocks Aug 26 '16 at 17:57
  • The question was answered, thanks for you help! i was missing one sleep, see the edit on top – tiagocarvalho92 Aug 26 '16 at 18:02
1

in the for loop the led never turns off

Here's a loop much like that one.

boolean x;
for (int i = 0; i < 10; i++) {
    x = true;
    Thread.sleep(2000);
    x = false;
}

This loop will take about 20 seconds to complete. Looking closely, for how many of those seconds will x be true?

About 20.

Doing some simple math, for how many seconds will x be false? About 0.

In case it isn't obvious yet, what you're doing is turning the LEDs on, sleeping for 2 seconds, then turning them off, then immediately turning them on again. Since PWM by nature is actually a rapid on/off pulse, this will have little or no impact; the LEDs will effectively always be in the state described in the first half of the loop.

  • While i was reading this i thought: he must me joking , am i really that dumb? Of course, sorry about this stupid and 2 year old question – tiagocarvalho92 Aug 26 '16 at 18:01
  • I've done things equally daft and stared at them for half the afternoon before realizing I should quit programming and stay away from computers ;) – goldilocks Aug 26 '16 at 18:03

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.