I have an LED RGB module and I wanted to control it through pmw in order to mix the colors and create others with mixed values.
I see a project similar but with an android app that mix the colors, i just wanted to control the 3 colors in an infinite loop form 0 to 255 in each color one at a time.
The code that I wrote only changes the red color (the first one) and the others are full on since the beginning
this is my code:
int pinRed = 11;
int pinGreen = 10;
int pinBlue = 9;
int color = 1;
byte brightnessRed = 0;
byte brightnessGreen = 0;
byte brightnessBlue = 0;
int tonoRojo = 2;
int tonoVerde = 2;
int tonoAzul = 2;
//------
void setup(){
pinMode(pinRed,OUTPUT);
pinMode(pinGreen,OUTPUT);
pinMode(pinBlue,OUTPUT);
}
void loop(){
switch(color){
case 1:
brightnessRed=brightnessRed + tonoRojo;
analogWrite(pinRed,brightnessRed);
break;
case 2:
brightnessGreen=brightnessGreen+tonoVerde;
analogWrite(pinGreen,brightnessGreen);
break;
case 3:
brightnessBlue=brightnessBlue + tonoAzul;
analogWrite(pinBlue,brightnessBlue);
break;
}
delay(100);
corrector();
}
void corrector(){
if (brightnessRed == 0 || brightnessRed == 255)
{
tonoRojo = -tonoRojo;
color++;
}
if (brightnessGreen == 0 || brightnessGreen ==255)
{
tonoVerde = - tonoVerde;
color++;
}
if (brightnessBlue == 0 || brightnessBlue == 255)
{
tonoAzul = -tonoAzul;
color++;
}
if (color == 4){
color = 1;
}
}
please if you can tell me what i have wrong I will thank you.
thank you Johnwasser I didn't realize of that, perhaps that issue makes the led blink in the top of the brightens, I try to use individual LEDs because the module has a rgb led on it and each color is in full brightens, and with that I didn't notice which color does up and down the luminescence...
Now with this configuration I notice that two LEDs works, red and green, i mean they does the up and down of the brightens
the only problem is that each color brights in different amounts, only the green is weak, and I guess the problem whit the rgb module is the voltage that is too much 5V in each pmw pin, is not?
thank you Msquare I'll do a research on that, I really appreciate that kind of advice
Look in example sketches that came with Arduino software. There should be a sketch called "ColorMixingLamp" under the "StarterKit" group. You can replace the photoresistors with potentiometers and play around to get a better idea how it all works.
I started with this sketch and tweaked it to create a sketch that cycles through colors of an RGB LED by itself, and I can control the speed of it with a pot.
ehrja:
the only problem is that each color brights in different amounts, only the green is weak, and I guess the problem whit the rgb module is the voltage that is too much 5V in each pmw pin, is not?
You cant have "too much 5v". Either it is 5V or not.
The voltage drop accross the LED is differnet for the different colours (the metals/doping used to make the colours), thus the voltage across the resistors is different, thus the current will be differnet for the same voltage from the Arduino. So you need to choose different values of resistors for each colour, to make the current equal. That should even them out.
There is a second effect - your eyes percieve the colours slightly differently, but more importantly they are logarithmic in response. So you see the differnece between analogWrite with 1 and 5 as several steps, but nothing seems to happen going from (say) 100 to 130.
For a good colour mix you need to : get the resistors balanced and software wise let "ideal brightness" go from 0 to (say) 25 which a formula maps non-linearly to 0-255. If you just want to show different colours - blinky blinky flash like - just use a few discrete analogWrite values in a table (say with 6 steps) that have PWM value which appears to you as "equally spaced intensity steps". Find the values by trial and error and the sowftare will thus (partially) compensate for wrong resistor values.