Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is most likely no translated for the post!





Some LED effects qu...
 
Share:
Notifications
Clear all

Some LED effects questions ...


 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1758
Topic starter  

From the original comment here:

Thanks so much for this comprehensive LED tutorial! It’s the best i’ve found. I had a few questions, sorry if this is easy, I’ve only started with Arduino and LED strips today so am struggling a lot to understand the coding parts. I’m using your approach to fade in and out of the for the LED strip, I’ve adapted bits of the code you posted and the working code I’m using is pasted below. Can you please give a bit more information about the points mentioned below.

 

Thanks again for such an amazing tutorial!

 

//how to change brightness to range from completely dark to full brightness?

//how do I change speed with which the LED’s transition from off to full brightness?

//how to change the fade duration?

//how to change the amount of time the LED’s are of/completely dark?

//how to transition from one colour to another – which command in the code does this ? 

 

#include <Adafruit_NeoPixel.h>

#define PIN 2

#define NUM_LEDS 255

// Parameter 1 = number of pixels in strip

// Parameter 2 = pin number (most are valid)

// Parameter 3 = pixel type flags, add together as needed:

// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)

// NEO_KHZ400 400 KHz (classic ‘v1’ (not v2) FLORA pixels, WS2811 drivers)

// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)

// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);




void setup() {

  strip.begin();

  strip.show(); // Initialize all pixels to ‘off’

}




// REPLACE FROM HERE

void loop() { 

// FadeInOut(0xff, 0x00, 0x100); // red – DISABLED with // at beginning of line

  FadeInOut(0xff, 0xff, 0xff); // white 

// FadeInOut(0x00, 0x00, 0xff); // blue – DISABLED with // at beginning of line

}




void FadeInOut(byte red, byte green, byte blue){

  float r, g, b;

      

  for(int k = 0; k < 256; k=k+1) { 

    r = (k/256.0)*red;

    g = (k/256.0)*green;

    b = (k/256.0)*blue;

    setAll(r,g,b);

    showStrip();

  }

     

  for(int k = 255; k >= 0; k=k-2) {

    r = (k/256.0)*red;

    g = (k/256.0)*green;

    b = (k/256.0)*blue;

    setAll(r,g,b);

    showStrip();

  }

}// —> here we define the effect function <—

// REPLACE TO HERE




void showStrip() {

 #ifdef ADAFRUIT_NEOPIXEL_H 

   // NeoPixel

   strip.show();

 #endif

 #ifndef ADAFRUIT_NEOPIXEL_H

   // FastLED

   FastLED.show();

 #endif

}




void setPixel(int Pixel, byte red, byte green, byte blue) {

 #ifdef ADAFRUIT_NEOPIXEL_H 

   // NeoPixel

   strip.setPixelColor(Pixel, strip.Color(red, green, blue));

 #endif

 #ifndef ADAFRUIT_NEOPIXEL_H 

   // FastLED

   leds[Pixel].r = red;

   leds[Pixel].g = green;

   leds[Pixel].b = blue;

 #endif

}




void setAll(byte red, byte green, byte blue) {

  for(int i = 0; i < NUM_LEDS; i++ ) {

    setPixel(i, red, green, blue); 

  }

  showStrip();

}

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1758
Topic starter  
Posted by: @Asa

//how to change brightness to range from completely dark to full brightness?

//how do I change speed with which the LED’s transition from off to full brightness?

//how to change the fade duration?

//how to change the amount of time the LED’s are of/completely dark?

//how to transition from one colour to another – which command in the code does this ? 

 

Well, let me start by saying that it would be better to use the FastLED library for some of these extra functions. FastLED is faster and more mature than NeoPixel, and for this particular question, NeoPixel does not have an easy answer.

If you use FastLED, brightness of the entire strip can be set/modified with:

FastLED.setBrightness(X);

Where X is a value 0 (darkest) ... 255 (brightest).

Woops .... I just realized you're look for answers in your code, so I'll continue with that.

So the fade code was:

void FadeInOut(byte red, byte green, byte blue){

  float r, g, b;

for(int k = 0; k < 256; k=k+1) { 
  r = (k/256.0)*red;
  g = (k/256.0)*green;
  b = (k/256.0)*blue;
  setAll(r,g,b);
  showStrip();
  }

for(int k = 256; k >= 0; k=k-2) {
  r = (k/256.0)*red;
  g = (k/256.0)*green;
  b = (k/256.0)*blue;
  setAll(r,g,b);
  showStrip();
}
}

Completely dark to full brightness is what should already be happening in the first for-loop - so I'm not sure what you're trying to ask.

Just to explain how the first loop works (the second for-loop works the same way, but instead from 0..255 it no goes from 255..0)
For each color we define a fraction of 256 (k/256 where k = 0..256), and multiply that fraction (0 ... 1) with the selected color (red, green, blue).
So a color element (red,green.blue) will always start with 0 (black) and climb up to the color number you've passed to the function (when k=255).

To influence the speed (we can only slow down), you could add a "delay()" call just after each "ShowStrip()". For example:

 for(int k = 0; k < 256; k=k+1) { 
  r = (k/256.0)*red;
  g = (k/256.0)*green;
  b = (k/256.0)*blue;
  setAll(r,g,b);
  showStrip();
delay(100); // milliseconds! So 1 second = 1000
  }

You should play with the value (100) for the delay function a little bit to see what looks right to you.
Or you could even make this value change so the transition is not linear. For example:

 for(int k = 0; k < 256; k=k+1) { 
  r = (k/256.0)*red;
  g = (k/256.0)*green;
  b = (k/256.0)*blue;
  setAll(r,g,b);
  showStrip();
delay(k); // or: delay(256-k);
  }

To fade from one color to another color, things become a lot more complex (see for example these posts: StackExchange, GitHub).
I'd have to do some experiments to see what is the best approach.

The trick will be to reduce each fraction of the color element (red,green,blue) of the starting color, to the end color.
Probably something like

r = ( ((k/256.0)*RedStart) + ((k/256.0)*RedEnd) ) / 2;

But that is just a wild guess early in the morning.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1758
Topic starter  
Posted by: @asa123456789

...
void setup()
{
FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}

FastLED.setBrightness 100

void FadeInOut(byte red, byte green, byte blue){
...

I see in this part of the code a line that is misplaced.
It should probably be in the void setup() function.
But you also have to pass the value to the setBrightness function, like so:

...

void setup()
{
FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness(100);
}

void FadeInOut(byte red, byte green, byte blue){
...

Note: for readability, it's always a good idea to write as clean as possible,
So for example indent the lines in a function or a loop (the sections between accolades) by placing a few spaces in front of it - but this could also be caused by pasting the code of course. Just thought I'd mention it. 😋 

Next you're missing the void loop() function.

It may be a good idea to read through this intro to get an idea how things work.
There is no shame in being new to this topic ... we all had to start at some point, and I'd be happy to help out  😉 

Posted by: @asa123456789

#pragma message

The pragma message is just a message - not an error. You can safely ignore this one.

Now since your project is not super complicated, I'd step away from the code I had in my project and focus on a simpler approach.
But looking at the code you've posted, there are quite a few pieces missing (probably caused by copy and paste), so I'd probably start by cleaning that up first.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1758
Topic starter  

Hi Asa123456789,

Let me try to answer your questions 😊 

Posted by: @asa123456789

I need the lights to stay fully lit once they reach maximum brightness which I've currently done with the delay script

A delay for 5 minutes would be 5(minutes)*60(seconds)*1000(milliseconds) = 300,000 and you already figured that one out I presume.
So I'd try "delay(300000);" - granted, I haven't tried a 5 minute delay yet, but as far as I can see, the delay() function should be able to work that way.

Ideally (to keep your code readable) would be doing something like this (where the red "5" indicates the minutes):

delay(1000ul*60*5);

The "ul" force the calculation to result in an unsigned long integer - something I read (here) while looking for an answer for you.

The best place to put this delay would be in indeed where you've put yours, in your FadeInOut function.

Let me know if the delay worked this way.
Like I said, I've never tried a 5 minute delay.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1758
Topic starter  

You're welcome, and good to hear that worked.
For your purpose we should probably break the FadeInOut function in two pieces: FadeIn() and FadeOut().
This way you can easily add delays wherever you'd want.

For example (untested):

void FadeIn(byte red, byte green, byte blue) {
float r, g, b;

//below paragraph: parameters for the 1st fade - fading 'ON' or 'UP'//
for (int k = 0; k < 256; k = k + 1)
{ //0 on this line stands for 'off' and '256' is the maximum brightness to go to//
r = (k / 256.0) * red;
g = (k / 256.0) * green;
b = (k / 256.0) * blue;
setAll(r, g, b);
showStrip();
}
}


void FadeOut(byte red, byte green, byte blue) {
float r, g, b;

for (int k = 255; k >= 0; k = k - 2)
{
r = (k / 256.0) * red;
g = (k / 256.0) * green;
b = (k / 256.0) * blue;
setAll(r, g, b);
showStrip();
delay(1000);
}
}

And call these in loop():

void loop() {
FadeIn (0xEF, 0xEF, 0xEF);
Delay(1000ul*60*5);

FadeOut(0xEF, 0xEF, 0xEF);
}

 


ReplyQuote



 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1758
Topic starter  

Seems function names are case-sensitive, so replace "Delay" with "delay" ... 😉 


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1758
Topic starter  

I think there is something going wrong with what you're copying and pasting ...

First: keep in mind that function names are case-sensitive:

void loop() {
// typo: fadeIn (0xEF, 0xEF, 0xEF);
FadeIn (0xEF, 0xEF, 0xEF);
delay(1000ul*60*5);
FadeOut(0xEF, 0xEF, 0xEF);
}

Additionally there seems to be some mixing up going on in the functions:

void FadeIn(byte red, byte green, byte blue) {
float r, g, b;

//below paragraph: parameters for the 1st fade - fading 'ON' or 'UP'//
for (int k = 0; k < 256; k = k + 1)
{ //0 on this line stands for 'off' and '256' is the maximum brightness to go to//
r = (k / 256.0) * red;
g = (k / 256.0) * green;
b = (k / 256.0) * blue;
setAll(r, g, b);
showStrip();
}
} // <--- you were missing this accolade

void FadeOut(byte red, byte green, byte blue) {
float r, g, b;

for (int k = 255; k >= 0; k = k - 2)
{
r = (k / 256.0) * red;
g = (k / 256.0) * green;
b = (k / 256.0) * blue;
setAll(r, g, b);
showStrip();
delay(1000);
}
}

// and I don't know where this came from:
//below paragraph: parameters for the 'OFF' setting using delay//
for(int k = 0; k >= 0; k=k-2) {
r = (k/256.0) *red;
g = (k/256.0) *green;
b = (k/256.0) *blue;
setAll(r,g,b);
showStrip();
delay(1000ul*60*5);
}

}

As you can see, it's key to be accurate when working with code.
Also, and this could also be because the forum doesn't seem to honor indentation properly, indentation makes this more readable.

Hope this helps 😉 


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1758
Topic starter  

Hey there! 

Well, what the numbers mean ...

Posted by: @hans

void FadeIn(byte red, byte green, byte blue) {
  float r, g, b;

  //below paragraph: parameters for the 1st fade - fading 'ON' or 'UP'//
  for (int k = 0; k < 256; k = k + 1)
  { //0 on this line stands for 'off' and '256' is the maximum brightness to go to//
    r = (k / 256.0) * red;
    g = (k / 256.0) * green;
    b = (k / 256.0) * blue;
    setAll(r, g, b);
    showStrip();
  }
}

For colors, the mac value of each RGB element is 255. So White would be 255,255,255.
To Fade into a given color we'd have at most 256 steps (0 to 255).

So lets say we'd want to fade into white (255, 255, 255 or in hexadecimal notation: 0xFF, 0xFF, 0xFF), we'd follow this sequence:

0,0,0 - 1,1,1 - 2,2,2 - 3,3,3 - ... - 254,254,254 - 256,256,256

However, when designing this function we do not know yet what the target color will be. We do however know that at most it will be 256 steps at the most. So this is why I have chosen for to multiply each RGB element by a 256-fraction (eg.  k/256).
So this way, each RGB element has 256 steps, and the max is reach when we do 256/256 * color. (since 256/256 = 1)

As for the delay numbers:

Posted by: @asa123456789

delay(1000ul*60*5) 

This means 1000 milliseconds in a second * 60 seconds in a minute * 5 minutes.
We add "UL" to make the "1000" a unsigned long integer - this forces the result to become a unsigned integer.
This is to prevent that (for example) a regular integer will be used.
A regular Integer could have a max value of 32768, but our calculation (1000*60*5=300000) would go beyond that and the outcome will become unpredictable and most certainly incorrect.

 

To power the strips, you'd most certainly need a good power supply. With 150 leds, you'd theoretically need a capacity of 150 * 3 * 20mA (one LED block on the strip, actually has 3 LEDs in it, one for red, one for green and one for blue, and each of those pulls 20mA max). So this would result in 9000 mA (max) = 9 A. Now keep in mind that this would be needed when you set your entire strip to white at max brightness.
In my experience (depending on your project!) you may not need this much, so when your strip changes colors often, you may get away with a less powerful power supply. I've had most projects work just fine with taking somewhere between 1/3 and 2/3 of the calculated max, so in your case for a 150 LED strip maybe somewhere 3A and 6A may be fine.

Since you want 3x 150 LEDs, your power supply may need to be pretty potent (between 9A and 18A, or max 27A).
A 5V 27A power supply may be hard to find ... so using 3 separate power supplies can work very well as well.

To hook this up:

1) Make sure Arduino, LED strips and all power supplies have GND in common (so connect Arduino GND to GND of each power supply, and to GND of each LED strip.

2) +5V of the power supply will only go to each individual LED strip.
If you have the Arduino as stand alone (not connected to its own power supply and not connected to the USB of your computer), then you can use the +5V of one of the power supplies to power the Arduino.

Note that with long strands, the brightness may reduce towards the end of the strip. To prevent this, connect the +5V of the power supply connected to that strip, is also connected to the +5V at the end of the LED strip. For very long strands, you can even connect that +5V somewhere in the middle of the strand to +5V to "freshen up" the +5V.
Use this only if needed (you'll notice if this would affect your strands: the last LEDs get dimmer).


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1758
Topic starter  

p.s. don't worry about the pasting of code, I noticed the same thing 😋 
That's why I manually indent the code after pasting, it makes it much more readable.
Usually when someone posts code without indenting, I copy the code and paste it here: https://codebeautify.org/c-formatter-beautifier

It will clean up the missing indents.


ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: