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 ...  

Page 1 / 2
  RSS

Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1373
March 6, 2020 3:41 AM  

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
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1373
March 6, 2020 4:42 AM  
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
asa123456789
(@asa123456789)
Active Member
Joined: 4 months ago
Posts: 7
March 12, 2020 4:58 AM  

@hans

Hey Hans!

Thanks for getting back to me and sorry for the late reply, I also had to create a new account as mine wasn't working for some reason...

Thanks for your help with this, to give a bit more clarity; what I want to do is have the lights power on from being off, possibly from a cold color temprature to a slightly warmer one, then once they've hit full brightness have them stay static on that color for a period of time before fading to complete darkness and stay off for another period of time. Some of the early fade trials I tried resulted in the lights not properly turning off but maybe there was an issue with the code or the delay speed.

Could I ask how the code, specifically the new bits fits together into the template you created, I've put it all together but I'm still such a beginner I'm not really sure how it's all supposed to work as a complete section of code.

#include "FastLED.h"
#define NUM_LEDS 255
CRGB leds[NUM_LEDS];
#define PIN 2

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

FastLED.setBrightness 100

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

float r, g, b;

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();
}

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();
}

 

Lastly since installing FastLED I keep getting a weird error msg, which I've put below. Do you know what the situation is? I've tried re-installing as well as downgrading the versions several generations below the current one but It's still flagging up. 

In file included from /Users/Asa 1/Documents/Arduino/NEW_LED_Strip_fade_for_lightboxMASTERCODE/FASTLEDNEW_LED_Strip_fade_for_lightbox.ino/FASTLEDNEW_LED_Strip_fade_for_lightbox.ino.ino:1:0:
/Users/Asa 1/Documents/Arduino/libraries/FastLED/FastLED.h:14:21: note: #pragma message: FastLED version 3.003.003
# pragma message "FastLED version 3.003.003"

 

Many thanks again for your generous time and attention.

 

Asa


ReplyQuote
Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1373
March 12, 2020 6:04 AM  
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
asa123456789
(@asa123456789)
Active Member
Joined: 4 months ago
Posts: 7
March 16, 2020 3:35 AM  

Hey Hans,

Thanks for getting back to me, okay. So under your recommendation I actually spent last week learning the arduino code from scratch and simultaneously trying to piece together something that would be better suited to what I want to do. I took your advice with the delay commands and wrote a section after the first loop which will delay the second loop starting. It's coming together but I still had a few questions.

One thing I still can't work out at the moment, I need the lights to stay fully lit once they reach maximum brightness which I've currently done with the delay script, however the effect isn't quite perfect as It means that the lights don't stay being fully bright they just start to dim over time - what I really need it some way to have the lights hit full brightness without fading out slowly then use the delay function to dictate how long they will fade out for.

Another question also related to delay is about if I can change the time duration from miliseconds to a longer unit of time, I tried to enter 10000 for a delay when I was messing around but It didn't seem to do anything unless I was supposed to enter a decimal point or something? Im' going to need the LED strips to be on and off for a fairly long periods of time like 5 - 10 minutes so it would be useful to know if the best way to achieve this is through using milliseconds or if a more suitable alternative is available.

I've added my code below and would really appreciate your advice.

Thanks again for all your amazing help so far!   

 

//*An array is a consecutive group of memory locations that are
//of the same type. To refer to a particular location or element
//in the array, we specify the name of the array and the position
//number of the particular element in the array//
//FastLED.show(); //the LEDs will not be lit until FastLED.show() is called

#include "FastLED.h" //referencing the needed library to draw on//
#define NUM_LEDS 250 //defines the number of LED's to use in a macro way so anywhere the code is gets replaced with what is here//
#define DATA_PIN 2 //defining which Digital Arduino Pin to use//
CRGB leds[NUM_LEDS]; //*sets up an array to manipulate/set/clear LED Date

void setup() {
//for (int i = 0; i < NUM_LEDS; i++){//
//leds[i] = CRGB:Green;// - Turns all LED's to a certain color in this case Green//

FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); //This tells the library that there's a strand of NEOPIXEL's
//on pin 2 (remember, the value that PIN was set to), and those leds will use the led array leds, and there are
//NUM_LEDS (aka 60) of them.//
}

void loop() {
FadeInOut (0xEF, 0xEF, 0xEF); //hex color code. To change colour alter last two digits of each section with Green, Red, Blue from color picker website//
//FadeInOut(0x00, 0xff, 0xff);// - for adding additional colours on the LED copy and paste above and change colours//
}

void FadeInOut (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();
}

//below paragraph: parameters for the 2nd fade - fading 'OFF' or 'DOWN'//
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);
}

//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(10000);
}

}

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
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1373
March 16, 2020 4:33 AM  

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
asa123456789
(@asa123456789)
Active Member
Joined: 4 months ago
Posts: 7
March 17, 2020 5:15 AM  

@hans

Okay that 5 minute delay is working out great, thank you! Any idea how to resolve the other issue, so keeping the lights are maximum brightness without fading probably for a period of 5 - 10 minutes before the delay kicks in?

 

Many thanks.

 


ReplyQuote
Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1373
March 17, 2020 5:35 AM  

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
asa123456789
(@asa123456789)
Active Member
Joined: 4 months ago
Posts: 7
March 17, 2020 8:41 AM  

Okay thanks, just checking as I've not been able to figure this out myself yet, how do I define delay when I get the error message listed below? I'm guessing it goes at the top of the page along with my definitions not inside the setup or void loop?

 

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

}

error message from above code: 'Delay' was not declared in this scope

 

Thanks! 


ReplyQuote
Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1373
March 17, 2020 10:37 AM  

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


ReplyQuote



asa123456789
(@asa123456789)
Active Member
Joined: 4 months ago
Posts: 7
March 18, 2020 5:50 AM  

Okay thanks, I'm getting a few error message now which is really frustrating mostly to do with more things not being defined in scope again: the FadeOut section in the Void Loop() but then also when I removed that see how it would work without it almost every line of my code is being highlighted with the 'a function-definition is not allowed here before '{'. Full code and errors below. Any ideas on what I'm doing wrong, I've spent ages trying to figure this out and have got no where! Any advice would be appreciated. 

 

#include "FastLED.h" //referencing the needed library to draw on//
#define NUM_LEDS 250 //defines the number of LED's to use in a macro way so anywhere the code is gets replaced with what is here//
#define DATA_PIN 2 //defining which Digital Arduino Pin to use//
CRGB leds[NUM_LEDS]; //*sets up an array to manipulate/set/clear LED Date

void setup() {
//for (int i = 0; i < NUM_LEDS; i++){//
//leds[i] = CRGB:Green;// - Turns all LED's to a certain color in this case Green //

FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); //This tells the library that there's a strand of NEOPIXEL's
//on pin 2 (remember, the value that PIN was set to), and those leds will use the led array leds, and there are
//NUM_LEDS (aka 60) of them.//

}

void loop() {
fadeIn (0xEF, 0xEF, 0xEF);
delay(1000ul*60*5);
FadeOut(0xEF, 0xEF, 0xEF);

}

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);
}
}
//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);
}

}

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();
}

 

Errors:

 

/var/folders/cj/mmx_npc16cv3w3b8w0pz_crc0000gn/T/arduino_modified_sketch_913419/sketch_mar17a.ino: In function 'void loop()':
sketch_mar17a:23:3: error: 'fadeIn' was not declared in this scope
fadeIn (0xEF, 0xEF, 0xEF);
^~~~~~
/var/folders/cj/mmx_npc16cv3w3b8w0pz_crc0000gn/T/arduino_modified_sketch_913419/sketch_mar17a.ino:23:3: note: suggested alternative: 'FadeIn'
fadeIn (0xEF, 0xEF, 0xEF);
^~~~~~
FadeIn
sketch_mar17a:25:3: error: 'FadeOut' was not declared in this scope
FadeOut(0xEF, 0xEF, 0xEF);
^~~~~~~
/var/folders/cj/mmx_npc16cv3w3b8w0pz_crc0000gn/T/arduino_modified_sketch_913419/sketch_mar17a.ino:25:3: note: suggested alternative: 'FadeIn'
FadeOut(0xEF, 0xEF, 0xEF);
^~~~~~~
FadeIn
/var/folders/cj/mmx_npc16cv3w3b8w0pz_crc0000gn/T/arduino_modified_sketch_913419/sketch_mar17a.ino: In function 'void FadeIn(byte, byte, byte)':
sketch_mar17a:43:47: error: a function-definition is not allowed here before '{' token
void FadeOut(byte red, byte green, byte blue) {
^
exit status 1
'fadeIn' was not declared in this scope


ReplyQuote
Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1373
March 18, 2020 9:36 AM  

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
asa123456789
(@asa123456789)
Active Member
Joined: 4 months ago
Posts: 7
March 24, 2020 12:18 PM  

Sorry about that, the way that the code pastes into these chats doesn't translate to how it appears in the Arduino window. So that's all working now I just had a couple other questions if you don't mind:

In the FadeIn delay what does each section of numbers refer to, I tried changing each one to find out how they work but I can't work out what each one does:  

delay(1000ul*60*5) 

Thanks, hope you're staying well and safe.

 


ReplyQuote
asa123456789
(@asa123456789)
Active Member
Joined: 4 months ago
Posts: 7
March 24, 2020 12:40 PM  

Just a couple of other things, eventually I need to power a second, maybe third strip of these LED's 150 per 5 meter strip so maybe something like 450 LED's in total. Do you recommend using separate power sources for each LED strip to stop the Arduino from too much power being used? Any advice would be really appreciated!!!


ReplyQuote
Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1373
March 25, 2020 3:21 AM  

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



Page 1 / 2
Share: