Go Down

Topic: Change direction of the leds (Read 595 times) previous topic - next topic

MK323F

Hello all,

First i like to introduce myself, I am marco and living in the netherlands i am a conveyor belt technician and in my spare time i like to learn all about programming arduino.
I just started a few months ago so i am still a newbie, but i want to learn as much as possible.

My first real project will involve leds, since there is already to much waste ocross the world, i like to make something i could also use. So i decide to make a light for my sons room which simulates the natural day and night light.

I have learned much from reading this forum and examples on internet.
I have made my own simple sketches and learned much from changing examples to what i liked and most of it went well.
But now i am stuck on some code and have to reach out for help.

I have a code which works fairly well, but i am trying to change the direction of the leds which i cant get to work.
Now all the leds turn on at the same time and slowly go up in brightness, i like to learn how i can change that into leds lighting up one after the other till all are on.

Code: [Select]

#include "FastLED.h"

// Number of leds in 1 strip
#define NUM_LEDS 84

DEFINE_GRADIENT_PALETTE( Sunup_gp )
{
    0,   0,   0,   0,  // Black
   51,  25,  25, 112,  // Midnight blue
  101,   0,   0, 205,  // Medium blue
  151,   0,  91, 225,  // Deepsky blue
  178, 135, 206, 250,  // Lightsky blue
  255,  50,  50, 200}; // Daglicht

CRGBPalette16 myPal = Sunup_gp;

    // Define the array of leds
    CRGB ledstrip_1[NUM_LEDS];
    CRGB ledstrip_2[NUM_LEDS];
    CRGB ledstrip_3[NUM_LEDS];
    CRGB ledstrip_4[NUM_LEDS];

void setup()
{
    delay( 3000 ); // power-up safety delay   
    FastLED.addLeds<WS2812B, 4, GRB>(ledstrip_1, NUM_LEDS);
    FastLED.addLeds<WS2812B, 5, GRB>(ledstrip_2, NUM_LEDS);
    FastLED.addLeds<WS2812B, 6, GRB>(ledstrip_3, NUM_LEDS);
    FastLED.addLeds<WS2812B, 7, GRB>(ledstrip_4, NUM_LEDS);
}

void loop()
{
  sunrise();
 
    FastLED.show();
}

void sunrise() {
 
  // total sunrise length, in minutes
  static const uint8_t sunriseLength = 60;

  // how often (in seconds) should the heat color increase?
  // for the default of 60 minutes, this should be about every 14 seconds
  // 14 seconds x 256 gradient steps = 3,584 seconds = ~60 minutes
  static const float interval = ((float)(sunriseLength * 60) / 256)*1000;

  // current gradient palette color index
  static uint8_t heatIndex = 0; // start out at 0

    // HeatColors_p is a gradient palette built in to FastLED
    // that fades from black to red, orange, yellow, white
    // feel free to use another palette or define your own custom one
    CRGB color = ColorFromPalette(myPal, heatIndex);

    // fill the entire strip with the current color
    fill_solid(ledstrip_1, NUM_LEDS, color);
    fill_solid(ledstrip_2, NUM_LEDS, color);
    fill_solid(ledstrip_3, NUM_LEDS, color);
    fill_solid(ledstrip_4, NUM_LEDS, color);

  // slowly increase the heat
  EVERY_N_MILLISECONDS(interval ) {
    // stop incrementing at 255, we don't want to overflow back to 0
    if(heatIndex < 255) {
      heatIndex++;
    }   
  }
}


Also there is a curious thing if i use my own palette with brightness, which fades down after the sunrise is completed, but when i use a pre-defined palette like lava or ocean colors, the brightness does not change after completion

PaulRB

Quote
but i am trying to change the direction of the leds which i cant get to work.
Well done for using code tags. But if you want or help you need to be more precise. What do you mean by "change the direction"? Do you mean dimming/fading, or animating? What do you mean by "can't get it to work"? What happens and what did you expect to happen?

MK323F

#2
Apr 20, 2018, 07:48 am Last Edit: Apr 20, 2018, 07:17 pm by MK323F
To be more precise, i like the led strip to have the leds lighting up one after the other starting on 1 side of the strip, so its in the animation i think.
Like the sun coming up in the east and going down in the West.
I think i need to change the fill_solid command which lights up all the leds at once, to a command which fills the strip 1 led by one till all the leds are in on position.

Thanks

edit:

got home and made a sketch of the direction of the leds, which i was hoping to fit in the sunrise sketch.

Code: [Select]

#include "FastLED.h"

// Number of leds in 1 strip
#define NUM_LEDS 84

// Define the array of leds
CRGB ledstrip_1[NUM_LEDS];
CRGB ledstrip_2[NUM_LEDS];
CRGB ledstrip_3[NUM_LEDS];
CRGB ledstrip_4[NUM_LEDS];

void setup()
{
 
    FastLED.addLeds<WS2812B, 4, GRB>(ledstrip_1, NUM_LEDS);
    FastLED.addLeds<WS2812B, 5, GRB>(ledstrip_2, NUM_LEDS);
    FastLED.addLeds<WS2812B, 6, GRB>(ledstrip_3, NUM_LEDS);
    FastLED.addLeds<WS2812B, 7, GRB>(ledstrip_4, NUM_LEDS);
}

void loop() {
  for ( byte i = 0; i < NUM_LEDS; i++ ) {
    ledstrip_1[i] = CRGB::Red;
    ledstrip_2[i] = CRGB::Red;
    ledstrip_3[i] = CRGB::Red;
    ledstrip_4[i] = CRGB::Red;
    FastLED.delay(1000);

  }
}

PaulRB

What about the other 3 strips?

Why have you set them up as 4 strips instead of one?

MK323F

I have set them up as 4 strips so i can control them seperatly if i need to.
If there is a better way, i would love to know all about it.

I will place the ledstrips side by side, i hope thats what u ment whit
Quote
What about the other 3 strips?

PaulRB

I have set them up as 4 strips so i can control them seperatly if i need to.
They are ws2812b LEDs. You can control every single led separately, as I'm sure you know already. So why did you really split them into 4 strips?

MK323F

Cause i bought them as 4 separate lightfixtures,  i cant make 1 strip of them since the led strip is built in an aluminium fixture.

In the start of my learning proces i had set them up like this cause they all light up this way, so i thought i was on the right track.
But if there is a better way please point me in the right direction.
If i get something to work, i get some kind of tunnel vision thinking  i have done good, but if i am pointed at the mistakes or pointed at a better way to do the same job i can learn from that.


PaulRB

#7
Apr 21, 2018, 11:54 am Last Edit: Apr 21, 2018, 12:06 pm by PaulRB
Normally you would just connect a wire from the data out connector on the end of one strip to the data in connector on the next strip and so on. That way, only one Arduino pin is needed instead of 4. If the data out connector is not accessible, you have to do it the way you did, but I would expect the data out connector is accessible so that the modules can be daisy chained.

Alternatively, if all 4 strips are to always show identical patterns as each other, all 4 could be wired to a single Arduino pin.

So you have 4 strips of 84 LEDs. How will they be physically arranged? When you light up LEDs in turn, which strip? Should each led fade on to full brightness before the next led begins to light, or will some/all leds fade at the same time but staggered?

rickerman

#8
Apr 21, 2018, 11:58 am Last Edit: Apr 21, 2018, 12:07 pm by rickerman
In order to turn off an LED you set the colour to Black.

So, to set the first 10 lights on each strip to red.

Code: [Select]
for ( byte i = 0; i < NUM_LEDS; i++ ) {
    If (i > 9) {
        ledstrip_1[i] = CRGB::Black;
        ledstrip_2[i] = CRGB::Black;
        ledstrip_3[i] = CRGB::Black;
        ledstrip_4[i] = CRGB::Black;
        FastLED.delay(1000);
        Coninue;
   }

    ledstrip_1[i] = CRGB::Red;
    ledstrip_2[i] = CRGB::Red;
    ledstrip_3[i] = CRGB::Red;
    ledstrip_4[i] = CRGB::Red;
    FastLED.delay(1000);

  }



MK323F

Normally you would just connect a wire from the data out connector on the end of one strip to the data in connector on the next strip and so on. That way, only one Arduino pin is needed instead of 4. If the data out connector is not accessible, you have to do it the way you did, but I would expect the data out connector is accessible so that the modules can be daisy chained.

Thanks, Unfortunately i can only acces them on one side, so i need to make use of 4 different pins.


Quote
Alternatively, if all 4 strips are to always show identical patterns as each other, all 4 could be wired to a single Arduino pin.
If i learn more programming, i would like to make some cloud patterns which show different patterns on each strip, or lightning whichs flashes a few leds on each strip to make it more realistic.
So i thought it could come in handy to have them on 4 pins, if it could be done different and get the same effect i would like to learn.

Quote
So you have 4 strips of 84 LEDs. How will they be physically arranged? When you light up LEDs in turn, which strip? Should each led fade on to full brightness before the next led begins to light, or will some/all leds fade at the same time but staggered?
I have placed them next to eachother with a bracket so if i finish this project i can hang the leds on the ceiling on my sons room. All connecting wires are on the same side.
Ideally i would make a sunset which last an hour, starting from led 1 on each strip slowly lighting up till all 84 leds on all strips are on. Starting in a darkblue color low brightness and fading up to white in a higher brightness. For sunset doing the opposite.
I also have a ds3231 to tell the arduino the exact time and show different lightprograms during the day.


Quote
In order to turn off an LED you set the colour to Black.

So, to set the first 10 lights on each strip to red.
Thanks, i got something going yesterday somewhat similar.

Code: [Select]

#include "FastLED.h"

// Number of leds in 1 strip
#define NUM_LEDS 84

DEFINE_GRADIENT_PALETTE( Sunrise_gp )
{

   51,  0,  0, 51,  // Midnight blue
  101,   0,   0, 150,  // Deepsky blue
  151,   10,   10, 212,  // Medium blue 
  201, 25, 25, 205,  // Lightsky blue
  241, 50, 50, 205,  // Lightsky blue
  255,  100,  100, 200}; // Daylight

CRGBPalette16 myPal = Sunrise_gp;

    // Define the array of leds
    CRGB ledstrip_1[NUM_LEDS];
    CRGB ledstrip_2[NUM_LEDS];
    CRGB ledstrip_3[NUM_LEDS];
    CRGB ledstrip_4[NUM_LEDS];

void setup()
{
    delay( 3000 ); // power-up safety delay   
    FastLED.addLeds<WS2812B, 4, GRB>(ledstrip_1, NUM_LEDS);
    FastLED.addLeds<WS2812B, 5, GRB>(ledstrip_2, NUM_LEDS);
    FastLED.addLeds<WS2812B, 6, GRB>(ledstrip_3, NUM_LEDS);
    FastLED.addLeds<WS2812B, 7, GRB>(ledstrip_4, NUM_LEDS);
}

void loop()
{
  sunrise();
 
    FastLED.show();
}

void sunrise() {
 
  // total sunrise length, in minutes
  static const uint8_t sunriseLength = 3;

  // how often (in seconds) should the heat color increase?
  // for the default of 60 minutes, this should be about every 14 seconds
  // 14 seconds x 256 gradient steps = 3,584 seconds = ~60 minutes
  static const float interval = ((float)(sunriseLength * 60) / 256)*1000;

  // current gradient palette color index
  static uint8_t heatIndex = 0; // start out at 0

   
    // current used color palette
    CRGB color = ColorFromPalette(myPal, heatIndex);

    // fill the entire strip with the current color palette starting on 1 side
  for ( byte i = 0; i < NUM_LEDS; i++ ) {
    ledstrip_1[i] = CRGB(color);
    ledstrip_2[i] = CRGB(color);
    ledstrip_3[i] = CRGB(color);
    ledstrip_4[i] = CRGB(color);
    FastLED.delay(1000);

  // slowly increase the heat
  EVERY_N_MILLISECONDS(interval ) {
    // stop incrementing at 255, we don't want to overflow back to 0
    if(heatIndex < 255) {
      heatIndex++;
    }   
  }
}
}


I dont know if this is the best way to do it, (probably not:P) but it is working.
It is starting the leds on 1 side slowly turning them all on  one by one, and fading the leds trough the colors in 3 minute's(3 minutes will change to an hour it is for testing)


PaulRB

I would take a different approach, which did not rely on delay() for timing. I would calculate seconds from midnight from the h, m & s from the rtc. Then I would come up with a formula/function which calculates the brightness/heat at a given time (in seconds) for a led in a given position. Then, once per second, or every 5 seconds, I would read the rtc and use the function to recalculate each led's brightness, then update the strips.

MK323F

thnx, thats something new to learn, i will make a new sketch and hopefully can turn your idea in something that works

Go Up