Giter Site home page Giter Site logo

FadeOn and stay on forever about jled HOT 7 CLOSED

oscgonfer avatar oscgonfer commented on June 20, 2024
FadeOn and stay on forever

from jled.

Comments (7)

jandelgado avatar jandelgado commented on June 20, 2024 1

Regarding the JLedSequence, I think you hit the same problem as described in #90:

The JLedSequence sequence object has a state and in your setup, the sequence is in state "not running", because both JLed objects are already/initially Off. So further calls to sequence.Update() will have no effect. Try to call sequence.Reset() in your ProcessLine function.

In your case also once the sequence "is done", it will not re-start when you reset the contained LEDs. The JLedSequence object itself has to be resetted using Reset()

from jled.

jandelgado avatar jandelgado commented on June 20, 2024

How often is the laser_fade_in_slow function called?

from jled.

oscgonfer avatar oscgonfer commented on June 20, 2024

On demand, i.e. triggered remotely, but never anywhere close to intervals shorter than FADESLOW, which is 3000ms. Basically the idea is to turn the "laser" on with a fade in but leave it on after that until another function makes it turn off.

from jled.

jandelgado avatar jandelgado commented on June 20, 2024

I think you need to change to something like:

#include <jled.h>

#define LASER_PIN       21
#define FRONTLIGHT_PIN  22
#define FADESLOW 3000

JLed pwm_pins[] = {JLed(LASER_PIN).Breathe(255).Forever(), JLed(FRONTLIGHT_PIN).Off()};

void setup() {
    Serial.begin(9600);
}

void laser_fade_in_slow(int brightness) {
    if (brightness){
        Serial.print("Set to FadeOn with brightness=");
        Serial.println(brightness);
        pwm_pins[0].FadeOn(FADESLOW).MaxBrightness(brightness).Repeat(1).Reset();
    } else {
        Serial.println("Set to FadeOff");
        pwm_pins[0].FadeOff(FADESLOW).Repeat(1).Reset();
    }
}


void loop() {
    static unsigned long last_change_time_ = 0;

    pwm_pins[0].Update();
    pwm_pins[1].Update();

    delay(1);

    // simulation: every 5s call laser_fade_in_slow with a new random brightness
    if (millis() - last_change_time_ > 5000) {
        last_change_time_ = millis();

        const auto brightness= random(0, 255);
        if (brightness < 100) {
            laser_fade_in_slow(0);
        } else {
            laser_fade_in_slow(brightness);
        }
    }
}

Otherwise, the effect for pwn_pins[0] will run Forever, because it was initially set to do so.
Also when using the JLedSequence, once it is finished, changes to the contained Leds in pwm_pins will have no effect, since the JLedSequence is configured to just run once.

from jled.

oscgonfer avatar oscgonfer commented on June 20, 2024

Okay... so say I do this for both pins:

#include <jled.h>

#define LASER_PIN       21
#define FRONTLIGHT_PIN  22
#define FADESLOW 3000

JLed pwm_pins[] = {JLed(LASER_PIN).Breathe(255).Forever(), JLed(FRONTLIGHT_PIN).Off()};

void laser_fade_in_slow(int brightness, int what) {
    if (brightness){
        Serial.print("Set to FadeOn with brightness=");
        Serial.println(brightness);
        pwm_pins[what].FadeOn(FADESLOW).MaxBrightness(brightness).Repeat(1).Reset();
    } else {
        Serial.println("Set to FadeOff");
        pwm_pins[what].FadeOff(FADESLOW).Repeat(1).Reset();
    }
}

Note that pwm_pins[0] starts with a Breathe(255).Forever(), while the other one doesn't.

Then, when I call laser_fade_in_slow(255, 1), it works perfectly (I can later on update with no issue), but with laser_fade_in_slow(255, 0) it makes the FadeOn on LASER_PIN, but afterwards I can't update it.

from jled.

jandelgado avatar jandelgado commented on June 20, 2024

I modified the example to call your laser_fade_in_slow function every 5s with random values. Works as expected, LEDs changing state continously (I am using an ESP32 for the test). Could you pleas try the example and the latest JLed Version? Which Micro controller and Framework are you using?

#include <jled.h>

#define LASER_PIN 21
#define FRONTLIGHT_PIN 22
#define FADESLOW 3000

#define LASER_PIN 21
#define FRONTLIGHT_PIN 22
#define FADESLOW 3000

JLed pwm_pins[] = {JLed(LASER_PIN).Breathe(255).Forever(),
                   JLed(FRONTLIGHT_PIN).Off()};

void laser_fade_in_slow(int brightness, int what) {
    Serial.print("For LED=");
    Serial.print(what);
    if (brightness) {
        Serial.print(", set to FadeOn with brightness=");
        Serial.println(brightness);
        pwm_pins[what]
            .FadeOn(FADESLOW)
            .MaxBrightness(brightness)
            .Repeat(1)
            .Reset();
    } else {
        Serial.println(", set to FadeOff");
        pwm_pins[what].FadeOff(FADESLOW).Repeat(1).Reset();
    }
}

void setup() {
    Serial.begin(9600);
}

void loop() {
    static unsigned long last_change_time_ = 0;

    pwm_pins[0].Update();
    pwm_pins[1].Update();

    delay(1);

    // every 5s call laser_fade_in_slow with a new random brightness
    if (millis() - last_change_time_ > 5000) {
        last_change_time_ = millis();

        const auto brightness = random(0, 255);
        const auto what = random(0, 2);
        if (brightness < 100) {
            laser_fade_in_slow(0, what);
        } else {
            laser_fade_in_slow(brightness, what);
        }
    }
}

from jled.

oscgonfer avatar oscgonfer commented on June 20, 2024

Hi!

Sorry for not answering for a while. I didn't have the time to test this part until now.

TL;DR

  • your test code works perfectly, no issue
  • the framework I am using is Arduino, board is a RF69HCW featherm0 board. I am also using RadioHead and neopixelbus for managing radio communications and controlling RGB ledstrips respectively. The code for the project I am making is here
  • Issue is now solved, process for it is below

Step 1. Adding Repeat(1) to pwm_pins[X].FadeOn(FADESLOW).MaxBrightness(brightness); makes the animation only be triggered once, which accomplishes the "stay on forever" part.

Step 2. Adding Reset() to pwm_pins[X].FadeOn(FADESLOW).MaxBrightness(brightness).Repeat(1); doesn't seem to solve the "unable to update sequence" afterwards (see below)

Step 3. I saw in your code that you perform pwm_pins[0].Update(); and pwm_pins[1].Update(); in the loop, instead of a sequence.Update(); of a JLedSequence sequence(JLedSequence::eMode::PARALLEL, pwm_pins); that I had declared after the pwm pins (see first comment). This seems to solve the issue, and actually, seems like the JLedSequence is no longer necessary and I don't fully understand why. Eitherway, issue seems to be solved and code is updated... so many thanks for your support, your code, and your library ❤️

from jled.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.