LED fade color spectrum sketch

I have been searching the internet a few days for this now and im completely lost. Just bought the uno to make art projects and the essential use will be for LED fun.

my main question is how does one write a sketch to fade through the color spectrum with a full color LED? If there is a tutorial on this please link it to me. I have yet to find one that makes any sense or is actually doing the simple task of cycling through the spectrum. I know its possible because I see very complex projects with a fading color spectrum.

The less important question is how does one program the arduino so the the spectrum with delay on specific colors. Plus fade at different speeds.

Im positive this is simple but for me its proving to be very complicated and help is very much appreciated.

Thank You

You are asking for a linear path (a time vs color graph) through a three dimensional space (red, green, and blue). You'll have to come up with that path yourself (Do you want it to go from 0 to full red, then 0 to full blue, then full red to 0 then...?)

Once you figure out the pattern, you can implement it with a combination of for loops, delay(), and analogWrite().

I want the path to go through all the colors. Starting with red. ending with lets say purple. Do i have to type in all of these colors or could it just fade through them by typing in the first and last color?

Do i have to type

No, certainly not!
If each colour component is represented by 8 bits, that's 224 values to type in (about 16.8 million).

No, you use for loops.

Thats great to hear. What type of loops are to be used though? Like it says on my profile. Im a newb

You could use three nested for loops

for (int red = 0; red < 256; ++red) {
  analogWrite (redLED, red);
  for (int green = 0; green < 256; ++green) {
    analogWrite (greenLED, green);
    for (int blue = 0; blue < 256; ++blue) {
      analogWrite (blueLED, blue);
    }
  }
}

with suitable setup.
Of course, this will produce some abrupt changes, which may not be what you want, but it should get you started.

Different colour spaces would requre different loops and colour space conversion.

use a `for loop'

You have to be more specific than "go through all the colors." As I said, you want the output to be a linear one through a 3 dimensional space. The easiest way to do it is to have several straight segments (the harder way is to have several curves). You need to specify the starting and ending points of each of those segments. Check out the `fade' example.

if you look in some color spectrums you can see that they go like this (in rgb):

255,0,0 255,127,0 255,255,0 0,255,0 0,255,255 0,0,255 255,0,255 255,0,0

so you should have a loop that constantly increments one value, while keeping the last, and if it reaches 255, start decreasing the last one.

e.g.
increment red from 0 to 255, if red is 255, increase green to 255, if this is 255, decrement red to 0. then keep green at 255 and increment blue, if blue is 255, start decrementing green etc etc

Ok thanks everyone for your help. Im going to just keep doing my homework. This should all start making sense soon.

i made a quick sketch that should work, but i cannot test it.

it is very bad, since it does not fully use for loop, but it should give you an idea on how to handle it:

const int pins[3] = {9,10,11};
int vals[3] = {255,0,0};

void setup()
{
	for(int i=0; i<sizeof(pins)/sizeof(pins[0]); i++)
		pinMode(pins[i], OUTPUT);
}

void loop()
{

	//set green to 255
	for(int i=0; i<=255; i++)
	{
		vals[1]++;
		updatLed();
	}

	//set red to 0
	for(int i=0; i<=255; i++)
	{
		vals[0]--;
		updatLed();
	}

	//set blue to 255
	for(int i=0; i<=255; i++)
	{
		vals[2]++;
		updatLed();
	}

	//set green to 0
	for(int i=0; i<=255; i++)
	{
		vals[1]--;
		updatLed();
	}

	//set red to 255
	for(int i=0; i<=255; i++)
	{
		vals[0]++;
		updatLed();
	}
	//set blue to 0
	for(int i=0; i<=255; i++)
	{
		vals[2]--;
		updatLed();
	}
}

void updateLed()
{
	for(int i=0; i<=2; i++)
		if(vals[i]>255)
			vals[i]=255;
	
	for(int i=0; i<=2; i++)
		if(vals[i]<0)
			vals[i] = 0;

	for(int i=0; i<=2; i++)
		anaglogWrite(pins[i], vals[i]);
}

thank you for making up a sketch for me, really appreciate it. i think im getting somewhere here. but i dont understand the updatLed() and why do you put int vals[3] = {255,0,0}; and not ={0,0,0}; wont that make it red?

The sketch will not work - the writer is confused between pin numbers and the PWM values to be written to them.

ok

ok, i found a mistake, but it does not change anything on the working fact, it should work fine..

it was in the check if the value is between 0 and 255, where i did check the pin instead of the value.

edit: i modified the code, it should be fine now

and to answer to you question:
i start with {255,0,0} which is red indeed because at the last part I end up with {255,0,0}, and if i would increment it more i would have 500, which is not a possible value for analogWrite

I realized that a lot of my confusion was due to the wrong pins. once i changed the pin position on the arduino old code that did not really work started working.

ow, that's something you really have to watch out for.
for now it's ok, it did not harm anything, but if you start using other hardware parts you really have to be sure what pins you use, it's very important not to mix them up!

but i'm glad you got it working, is it a sketch with that color changing or not?