# RGB led wave of cycling colors

hello all;

How could one go about writing a code that would fade colors of rgb leds in a line. Here is a video of exactly what im try to do: https://www.youtube.com/watch?v=WBn8rV8zzVc

I "manged" to make all the lights cycle through colors at the same time using a series of for loops but I cant figure out how to offset the leds.

Any help is appreciated!!

So post what you have written so far and we can give you help and guidance.

This is what I have so far:

``````#include "Tlc5940.h"

int reds[] = {0,3,6,9,12};
int greens[] = {1,4,7,10,13};
int blues[] = {2,5,8,11,14};

const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

int a;
const int DISPLAY_TIME = 25;

void setup(){
Tlc.init();
}
void loop(){
for (greenIntensity = 0; greenIntensity <= 255; greenIntensity+=5) {
redIntensity = 255-greenIntensity;
for(a=0; a<=4; a++){
Tlc.set(greens[a], greenIntensity);
Tlc.set(reds[a], redIntensity);
Tlc.update();
}
delay(DISPLAY_TIME);
}

for (blueIntensity = 0; blueIntensity <= 255; blueIntensity+=5) {
greenIntensity = 255-blueIntensity;
for(a=0; a<=4; a++){
Tlc.set(blues[a], blueIntensity);
Tlc.set(greens[a], greenIntensity);
Tlc.update();
}
delay(DISPLAY_TIME);
}

for (redIntensity = 0; redIntensity <= 255; redIntensity+=5) {
blueIntensity = 255-redIntensity;
for(a=0; a<=4; a++){
Tlc.set(reds[a], redIntensity);
Tlc.set(blues[a], blueIntensity);
Tlc.update();
}
delay(DISPLAY_TIME);
}

}
``````

This works… but all five leds are at the same point in the color cycle and I cant figure out how to offset their cycles.

Also I changed the value 255 to 4095 to use with a tlc5940.

That code changes all the LEDs together. To change just one LED then increment the intensity as you increment the LED's index.

I dont quite understand what that means?

``````for (greenIntensity = 0; greenIntensity <= 255; greenIntensity+=5) {
redIntensity = 255-greenIntensity;
for(a=0; a<=4; a++){
Tlc.set(greens[a], greenIntensity);
Tlc.set(reds[a], redIntensity);
Tlc.update();
}
delay(DISPLAY_TIME);
``````

Changes ALL the LEDs to be the same colour.
where as if you did this:-

``````for (greenIntensity = 0; greenIntensity <= 255; greenIntensity+=5) {
redIntensity = 255-greenIntensity;
for(a=0; a<=4; a++){
Tlc.set(greens[a], greenIntensity);
greenIntensity +=5;
Tlc.set(reds[a], redIntensity);
redIntensity +=5;
Tlc.update();
}
delay(DISPLAY_TIME);
``````

Or something like it all the LEDs would be of a different colour. This is because as you address a different LED you give it a different intensity.

You need to do this for each of your colour changes.

That doesnt seem to work they still all change color at the same time but flicker a bit. Iv been reading about sin and how you can use that to create this effect. Iv tried to apply it but I can only understand how to make it fade an led on and off. Do you have any idea how to use sin to create color waves... or maybe at this point just one color fading in and out in a wave. Here is another video of exactly what I want to do and it sounds like he used sin to create it. arduino led sin wave

Hi I am trying to make a color wave go across a line of leds. Iv been reading about sin and I can apply it to the code but I can only use it to make leds fade up and down at the same time. How would you use it to make the leds fade up and down in a wave. Here is a video of exactly what im trying to do and it sounds like he used sin to created this effect but I cant manage to replicate it : https://www.youtube.com/watch?v=sxD8HEs_ips

How are your LED’s connected to the arduino and what LED controller are you using (if any)? If they are led strips then they need to be the individually addressable type.
Post your code (Using the </> code tags)

Im using TLC5940’s. This is all i understand about sin:

``````#include "Tlc5940.h"
int reds[] = {0,3,6,9,12};
int greens[] = (1,4,7,10,13};
int blues[] = {2,5,8,11,14};
void setup()
{
Tlc.init();
}

void loop()
{
float in, out;

for (in = 0; in < 6.283; in = in + 0.001)
{
out = sin(in) * 127.5 + 127.5;
Tlc.set(reds[0],out);
Tlc.update();
}
}
``````

Few problems here.

One is that you are using ridiculously intricate maths functions to implement something that needs to be nowhere near that accurate. While that certainly can be done - presumably is what the fellow did in the YouTube video - it is not a particularly efficient way to do it.

Another is that you do not understand the logarithmic response of the eye to brightness - but I would not suggest you add an exp() function to the sin()!

In any case, the way to do it is to set up an array of values (of whatever type suits the PWM values) corresponding to the desired length of the cycle - indeed, an array for each colour, and "move" either by shifting the values in the (cyclic) array along each time you perform a transfer of them all to the display, or arrange to read them to the display starting from a different point each time.

In respect of my first point, a triangle wave will be just as good as a sine wave (you can flatten the peaks to make it almost indistinguishable visually), and you can calculate successive values by repeated multiplication (by factors such as .95 or 1.05) to produce a logarithmic/ exponential progression.

So what have you done. The way is forum works is you make changes and post your new code so we can check if you have done it right.

I was illustrating what you need to do, I only gave example of one of your changes. Forget sin wave fading, it is no panacea. You have the basic idea of fading wrong. Giving only a 25mS delay is going to do nothing to make you able to see the changes it will all be too fast.

Thank you Paul I will research what that means and try to apply it. Mike: Wasn't aware I was cross posting, thought I asked two separate questions.

Grumpy_Mike:

``````for (greenIntensity = 0; greenIntensity <= 255; greenIntensity+=5) {
``````

redIntensity = 255-greenIntensity;
for(a=0; a<=4; a++){
Tlc.set(greens[a], greenIntensity);
Tlc.set(reds[a], redIntensity);
Tlc.update();
}
delay(DISPLAY_TIME);

``````

Changes ALL the LEDs to be the same colour.
where as if you did this:-

``````

for (greenIntensity = 0; greenIntensity <= 255; greenIntensity+=5) {
redIntensity = 255-greenIntensity;
for(a=0; a<=4; a++){
Tlc.set(greens[a], greenIntensity);
greenIntensity +=5;
Tlc.set(reds[a], redIntensity);
redIntensity +=5;
Tlc.update();
}
delay(DISPLAY_TIME);

``````

Or something like it all the LEDs would be of a different colour. This is because as you address a different LED you give it a different intensity.

You need to do this for each of your colour changes.
``````

Okay I am working on implementing this again. I think the problem is the + 5 eventually exceeds 255. Should I/Can I/How would you: wrap the +5 back to zero after it exceeds 255?

Paul__B: "move" either by shifting the values in the (cyclic) array along each time you perform a transfer of them all to the display

Paul this sounds like something I would be able to do however I cannot fathom it into code. Could you give me a quick example of what your saying.