Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LED strip library: fading #74

Open
tobiaszehntner opened this issue Oct 7, 2013 · 4 comments
Open

LED strip library: fading #74

tobiaszehntner opened this issue Oct 7, 2013 · 4 comments

Comments

@tobiaszehntner
Copy link

Hey there,

I have a problem making my LEDs fade individually. The problem is that the library that controls the strip only allows brightness values for RBG 0-255, and if it goes over that, it will loop back to 0.

This means that the following code never gets into the second if-loop to have goingUp become false.

if(goingUp[i])
            {
              ledPos[i].r += fadeRate;
              ledPos[i].g += fadeRate;
              ledPos[i].b += fadeRate;

                 if(ledPos[i].r > 255)
                 {       
                   goingUp[i] = false;
                 }              
            } else
            {
              ledPos[i].r -= fadeRate;
              ledPos[i].g -= fadeRate;
              ledPos[i].b -= fadeRate;
            }

Now I could set it to >250. However, if fadeRate = 20 it would go from 240 back to 0 before reaching goingUp[i] = false, because the library doesn't allow it to go over 255. Is there a smart way to go around that while still being able to have a variable fadeRate?

Thanks,
Tobias

@bakercp
Copy link
Member

bakercp commented Oct 7, 2013

Perhaps consider the modulo operator ... ?

@mikewesthad
Copy link
Contributor

If I understand this correctly, your rate variable is just being added/subtracted each time loop is called?

How about this?

if(goingUp[i])
{
    if(ledPos[i].r + fadeRate > 255)
    {       
        goingUp[i] = false;
        // Set your leds to the maximum brightness here if you like
    } 
    else {
        ledPos[i].r += fadeRate;
        ledPos[i].g += fadeRate;
        ledPos[i].b += fadeRate;
    }
}
else 
{
// Do a similar operation but with -fadeRate
}

...

@tobiaszehntner
Copy link
Author

The modulo still messes with my brain, so I went with Mike's suggestion. Works like a charm, thanks!

@bakercp
Copy link
Member

bakercp commented Oct 7, 2013

nice. ... but ... you will not regret mastering the modulus!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants