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 no translation for that post!



LED Effects: Runnin...
 
Share:
Notifications
Clear all

[Solved] LED Effects: Running lights with 3 colors (Red, White and Blue!)

5 Posts
2 Users
0 Likes
2,842 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2680
Topic starter  

While tinkering on something similar, I was able to change my code easily to support 3 colors, showing red, white and blue.
You can select you own colors of course and the speed.

This sketch relies on FastLed.
Set the proper NUM_LEDS and the proper PIN used on your Arduino.

#include "FastLED.h"
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
#define PIN 6

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

void loop() {
RunningLights(0x00, 0x00, 0xff, // blue
0xff, 0xff, 0xff, // white
0xff, 0x00, 0x00, // red
150);
}

void RunningLights(byte red1, byte green1, byte blue1,
byte red2, byte green2, byte blue2,
byte red3, byte green3, byte blue3, int WaveDelay) {

float b1 = 0.05; // brightness LEDs 1 and 5
float b2 = 0.2; // brightness LEDs 2 and 4

int Col1Pos = 0;

for(int start=0; start<15; start++) /// "strip" is 10 leds: 2x5 leds
{
for (int i=-15; i < NUM_LEDS+15; i=i+15) {
Col1Pos = i+start;

setMyPixel(Col1Pos, b1*red1, b1*green1, b1*blue1); //turn every third pixel on
setMyPixel(Col1Pos+1, b2*red1, b2*green1, b2*blue1);
setMyPixel(Col1Pos+2, red1, green1, blue1);
setMyPixel(Col1Pos+3, b2*red1, b2*green1, b2*blue1);
setMyPixel(Col1Pos+4, b1*red1, b1*green1, b1*blue1);

setMyPixel(Col1Pos+5, b1*red2, b1*green2, b1*blue2); //turn every third pixel on
setMyPixel(Col1Pos+6, b2*red2, b2*green2, b2*blue2);
setMyPixel(Col1Pos+7, red2, green2, blue2);
setMyPixel(Col1Pos+8, b2*red2, b2*green2, b2*blue2);
setMyPixel(Col1Pos+9, b1*red2, b1*green2, b1*blue2);

setMyPixel(Col1Pos+10, b1*red3, b1*green3, b1*blue3); //turn every third pixel on
setMyPixel(Col1Pos+11, b2*red3, b2*green3, b2*blue3);
setMyPixel(Col1Pos+12, red3, green3, blue3);
setMyPixel(Col1Pos+13, b2*red3, b2*green3, b2*blue3);
setMyPixel(Col1Pos+14, b1*red3, b1*green3, b1*blue3);
}

FastLED.show();
delay(WaveDelay);
}

}

void setMyPixel(int Pixel, byte red, byte green, byte blue) {
if( (Pixel>=0) && (Pixel<=NUM_LEDS) )
{
leds[Pixel] = CRGB( red, green, blue);
}
}

 


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
 

@hans Hello :) Thank You for all these wonderful effects!

Do you have an idea how I could modify this sketch, so that I have a variable for each color to set the amount of pixles for each color?


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2680
Topic starter  

Hi Braincurd!

Thank you for the compliment!
Took me a second before I understood what you're asking (In think).

So you basically want to be able to define colors, and for each color the number of LEDs?

In that case, we'd better tart from scratch, as this sketch was not really intended to support this.
We'd have to figure a way to capture this in a formula, so we can determine values for (in the original code) b1 and b2, and that based on the number of LEDs that color "segment" may have.

I'd probably go even one step further, and even use a way to shift all LEDs.
If we'd go that route, we may even be able to use a predefined array of colors.
Not sure how many LEDs you have in mind?


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
 

Hello @hans, thank you a lot for your reply! I'm sorry for the imprecise description of my project! But I think you got what I wanted to achieve. My goal is that I want to adjust the code so that each color is a segment of pixels and the number of pixels for each segment (color) is defined by a global variable. All colors together should always be 20 pixels. I hope you got a better understanding of what I am trying to do. I already tried to adjust the code by using an Array for the Pixels, but it was not working. Do you have any idea how I could do this?


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2680
Topic starter  

Are you thinking of a fixed number of LEDs and colors, that just keep cycling?
Meaning:

1. We set all LEDs to the initial colors we like to see.
2. We copy the color of the last LED (= LED NUM_LEDS-1).
3. We shift all LEDs one position (so the last one drops off).
4. We copy that last LED color from step 2, to the 1st LED (= LED 0).

If that would be the case, then it would be good to define an array with all the colors.

The simplest, but not very efficient way, would be:

leds[0] = CRGB(r,g,b);
leds[1] = CRGB(r,g,b);
...
leds[NUM_LEDS-1] = CRGB(r,g,b);

Where "CRGB(r,g,b)" is a color of your choosing.
Note: keep in mind that the LEDs count from 0 to NUM_LEDS-1.
Note: Smarter ways would be if we can somehow calculate the colors with a formula, or when chunks repeating patterns can be copied.

 

Next we would want a loop that keeps repeating steps 2, 3 and 4.

Something like

CRGB LastLED;
...
LastLED = leds[NUM_LEDS-1];
...

Shifting can be done with a for-loop, or with the much faster memmove8 function, which looks like this:

memmove8( destination, source, amount_of_data );

so for our LEDs:

memmove8( &leds[1], &leds[0], (NUM_LEDS-1) * sizeof(CRGB) );

And of course we need to put that last LED back:

leds[0] = LastLED;

All combined something like this (untested):

CRGB LastLED;
...
void loop() 
{

  ...
  while(true)
  {
    LastLED = leds[NUM_LEDS-1];
    memmove8( &leds[1], &leds[0], (NUM_LEDS-1) * sizeof(CRGB) );
    leds[0] = LastLED;
    // possibly a delay here if this goes too fast
  }

}

  

Hope this helps.


   
ReplyQuote
Share: