To improve my knowledge of RGB led’s , I decided to follow some tutorials. I started with one from the Instructables website to make a RGB led flash randomly. It worked out nicely:
arduino code:
int ledcolor = 0;
int a = 100; //this sets how long the stays one color for
int red = 13; //this sets the red led pin
int green = 12; //this sets the green led pin
int blue = 11 ; //this sets the blue led pin
void setup() { //this sets the output pins
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}
void loop() {
int ledcolor = random(7); //this randomly selects a number between 0 and 6
switch (ledcolor) {
case 0: //if ledcolor equals 0 then the led will turn red
analogWrite(red, 204);
delay(a);
analogWrite(red, 0);
break;
case 1: //if ledcolor equals 1 then the led will turn green
digitalWrite(green, HIGH);
delay(a);
digitalWrite(green, LOW);
break;
case 2: //if ledcolor equals 2 then the led will turn blue
digitalWrite(blue, HIGH);
delay(a);
digitalWrite(blue, LOW);
break;
case 3: //if ledcolor equals 3 then the led will turn yellow
analogWrite(red, 160);
digitalWrite(green, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(green, LOW);
break;
case 4: //if ledcolor equals 4 then the led will turn cyan
analogWrite(red, 168);
digitalWrite(blue, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(blue, LOW);
break;
case 5: //if ledcolor equals 5 then the led will turn magenta
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
delay(a);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
break;
case 6: //if ledcolor equals 6 then the led will turn white
analogWrite(red, 100);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
delay(a);
analogWrite(red, 0);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
break;
}
}
After this, I stumbled across a forum post where someone describes how to control the Hue value of a LED and how to make fluent transitions between two colors.
video (EV -2 for better viewing):
arduino code:
int potpin = 2; // Switch connected to digital pin 2
int rpin = 9;
int gpin = 10;
int bpin = 11;
float h;
int h_int;
int r=0, g=0, b=0;
int val=0;
void h2rgb(float h, int &R, int &G, int &B);
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
}
void loop() // run over and over again
{
val=analogRead(potpin); // Read the pin and display the value
//Serial.println(val);
h = ((float)val)/1024;
h_int = (int) 360*h;
h2rgb(h,r,g,b);
Serial.print("Potentiometer value: ");
Serial.print(val);
Serial.print(" = Hue of ");
Serial.print(h_int);
Serial.print("degrees. In RGB this is: ");
Serial.print(r);
Serial.print(" ");
Serial.print(g);
Serial.print(" ");
Serial.println(b);
analogWrite(rpin, r);
analogWrite(gpin, g);
analogWrite(bpin, b);
}
void h2rgb(float H, int& R, int& G, int& B) {
int var_i;
float S=1, V=1, var_1, var_2, var_3, var_h, var_r, var_g, var_b;
if ( S == 0 ) //HSV values = 0 ÷ 1
{
R = V * 255;
G = V * 255;
B = V * 255;
}
else
{
var_h = H * 6;
if ( var_h == 6 ) var_h = 0; //H must be < 1
var_i = int( var_h ) ; //Or ... var_i = floor( var_h )
var_1 = V * ( 1 - S );
var_2 = V * ( 1 - S * ( var_h - var_i ) );
var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );
if ( var_i == 0 ) {
var_r = V ;
var_g = var_3 ;
var_b = var_1 ;
}
else if ( var_i == 1 ) {
var_r = var_2 ;
var_g = V ;
var_b = var_1 ;
}
else if ( var_i == 2 ) {
var_r = var_1 ;
var_g = V ;
var_b = var_3 ;
}
else if ( var_i == 3 ) {
var_r = var_1 ;
var_g = var_2 ;
var_b = V ;
}
else if ( var_i == 4 ) {
var_r = var_3 ;
var_g = var_1 ;
var_b = V ;
}
else {
var_r = V ;
var_g = var_1 ;
var_b = var_2 ;
}
R = (1-var_r) * 255; //RGB results = 0 ÷ 255
G = (1-var_g) * 255;
B = (1-var_b) * 255;
}
}
code with fade:
//i/o pin declarations
int rpin = 9;
int gpin = 10;
int bpin = 11;
//function prototypes
void solid(int r, int g, int b, int t);
void fade(int r1, int g1, int b1, int r2, int g2, int b2, int t);
void setup()
{
//empty
}
void loop()
{
//colour sequence instructions
int iVal1 = analogRead(0);
int iVal2 = analogRead(2);
int b1 = iVal2/4;
int b2 = iVal1/4;
int t= 1500;
// solid(r1,g1,b1,t);
fade(255,0,b1,0,255,b1,t);
fade(0,255,b1,255,0,b1,t);
//Example sequence one: Rainbow fade over 15 seconds:
//fade(255,0,0,0,255,0,5000); //fade from red to green over 5 seconds
}
//function holds RGB values for time t milliseconds
void solid(int r, int g, int b, int t)
{
//map values - arduino is sinking current, not sourcing it
r = map(r, 0, 255, 255, 0);
g = map(g, 0, 255, 255, 0);
b = map(b, 0, 255, 255, 0);
//output
analogWrite(rpin,r);
analogWrite(gpin,g);
analogWrite(bpin,b);
//hold at this colour set for t ms
delay(t);
}
//function fades between two RGB values over fade time period t
//maximum value of fade time = 30 seconds before gradient values
//get too small for floating point math to work? replace floats
//with doubles to remedy this?
void fade(int r1, int g1, int b1, int r2, int g2, int b2, int t)
{
float r_float1, g_float1, b_float1;
float r_float2, g_float2, b_float2;
float grad_r, grad_g, grad_b;
float output_r, output_g, output_b;
//declare integer RGB values as float values
r_float1 = (float) r1;
g_float1 = (float) g1;
b_float1 = (float) b1;
r_float2 = (float) r2;
g_float2 = (float) g2;
b_float2 = (float) b2;
//calculate rates of change of R, G, and B values
grad_r = (r_float2-r_float1)/t;
grad_g = (g_float2-g_float1)/t;
grad_b = (b_float2-b_float1)/t;
//loop round, incrementing time value "i"
for ( float i=0; i {
output_r = r_float1 + grad_r*i;
output_g = g_float1 + grad_g*i;
output_b = b_float1 + grad_b*i;
//map values - arduino is sinking current, not sourcing it
output_r = map (output_r,0,255,255,0);
output_g = map (output_g,0,255,255,0);
output_b = map (output_b,0,255,255,0);
//output
analogWrite(rpin, (int)output_r);
analogWrite(gpin, (int)output_g);
analogWrite(bpin, (int)output_b);
//hold at this colour set for 1ms
delay(1);
}
}