Help needed for a code to light RGB LED

Hey guys,

I want to light an RGB LED using PWM digital ports. I have made a code referring to various sources on the internet. I’m stuck now and I would like some guidance and help. What I want to accomplish is that:
*I made an array for red, green and blue pins
*An array for the pwm values to put to these pins
I am stuck in the part where i dont know how to pass the pwm value arrays to the pin arrays to light up the led. The point of this is to mix colours.

Attached is the code, plz help me out here. One more thing when I pass parameters it gives me error if I dont make pointers in this line void outputColour(int* led, const byte* colour). Why is it? In arduino reference section (http://www.arduino.cc/en/Reference/FunctionDeclaration) they dont make pointers. Can someone explain this to me plz.

Thanks

RGB LED.ino (687 Bytes)

I am stuck in the part where i dont know how to pass the pwm value arrays to the pin arrays to light up the led.

You can't pass an array to an array. You can only pass arrays to functions.

One more thing when I pass parameters it gives me error if I dont make pointers in this line void outputColour(int* led, const byte* colour). Why is it?

There are two ways to pass data to a function - pass by value and pass by reference. With pass by value, the value is copied. With pass by reference, it is not. Since arrays can be quite large, they are always, by default, passed by reference. A pointer is a type of reference - it refers/points to the memory location where the data is.

What you are doing in outputColor is all wrong. The function was passed a pointer to memory. Use that pointer just like an array.

In the function, you need to call analogWrite() with the pin in the nth position in the led array and the value in the nth position in the color (you misspelled it) array.

What you are doing in outputColor is all wrong. The function was passed a pointer to memory. Use that pointer just like an array.

I don't quite understand what you mean by this, can you please explain a little with an example.

In the function, you need to call analogWrite() with the pin in the nth position in the led array and the value in the nth position in the color (you misspelled it) array.

I do understand that i need to put analogWrite() to output. But how can I assign the values of PWM to RGB LED pins. (For example pin 9 = 83, how can I accomplish this)

Your function, outputColour() has two arguments, a pointer to a block of memory where the pin numbers are stored, and a pointer to a block of memory where the colors are stored.

Ignore the fact that led is a pointer, and just treat it as an array, and the fact that colour is a pointer, and just treat it as an array:

void outputColour(int *led, const byte *colour)
{
   for(int i=0; i<3; i++)
   {
      analogWrite(led[i], colour[i]);
   }
}

Thanks alot. Really appreciate it :D