Introduction: RGB LED Change of Colours+light Sensor+LCD
this project embed an RGB led that changes colours in a continous manner, an LCD which shows the current colour and the next one, or, in it's different mode, show you the intensity of the sorounding light, when there is enough.the power of the maximum light for the LED not to work can be calibrated easaly throught a change of only one parameter in the code. (power)
You will need: 1. an arduino uno board
2. standard 16*2 LCD
3. common anode RGB LED
4. standard light sensor
5.10K & 330 ohm resistors
step 1. connect the rgb led anode to the uno GND
2. connect the red green & blue legs of the led through 330 ohm resistor to three of the PWM ports of uno
3. connect the light sensor from vcc to gnd through 10K resistor (so that the sensor is "up")
4. connect the second leg of the sensor to A5 analog input of uno.
5. connect the LCD (look at the code or change it for the position of connections-see file:///D:/My%20Documents/Arduino/arduino-1.0.1/reference/LiquidCrystalConstructor.html for reference)
6. after the adjosments of the code, compile and burn it to the UNO and your done :-)
there is a lot for creativity in replacing the light detector by almost every analog sensor... Enjoy!
the code:
#include
#include
LiquidCrystal lcd(7, 6, 2, 3, 4, 5);
void setup() {
lcd.begin(16,2); // start the library
lcd.setCursor(0,0);
}
void loop() {
int power=150;
beg:analogWrite(11,0);
analogWrite(10,0);
analogWrite(9,0);
digitalWrite(8,HIGH);// providing ref V
int a;
a=analogRead(A5);//read the value
char bufg[5];
// convert a to string [buf]
itoa(a,bufg, 10);
lcd.print(itoa(a, bufg, 10));//print value
delay(1000);//delay
lcd.clear();// clear LCD for next iteration
if (a>power) {goto beg;} else {goto next;}// check whether there is light
next:
int vec2[3]={random(255),random(130),random(30)};//random vec2
int vec1[3]={random(255),random(130),random(30)};//random vec1
int vec3[3]={234,100,20};//reff vector-random, but constant
int vec4[3]={random(255),random(130),random(30)};//random vec4
int vec5[3]={random(255),random(130),random(30)};//random vec5
int vec6[3]={random(255),random(130),random(30)};//random vec6
int r=vec3[0];
int g=vec3[1];
int b=vec3[2];//initiation of the RGB to reff vec
while (1) {
char bufr1[5];
// convert 123 to string [buf]
;
char bufg1[5];
// convert 123 to string [buf]
itoa(vec1[0], bufg1, 10);
char bufb1[5];
// convert 123 to string [buf]
itoa(vec1[1], bufb1, 10);
lcd.clear();
lcd.setCursor(0,1);
lcd.print ("R:");
lcd.print(itoa(vec1[0], bufr1, 10));
lcd.print (" G:");
lcd.print(itoa(vec1[1], bufg1, 10));
lcd.print (" B:");
lcd.print(itoa(vec1[2], bufb1, 10));// prins the values of rgb led by usinf "itoa" function
//itoa can convert an integer value to a string
delay(10);
if (r>vec1[0]) {r--;} else
if (r
if (g>vec1[1]) {g--;} else
if (g
if (b>vec1[2]) {b--;} else
if (b
analogWrite(11,b);
analogWrite(10,g);
analogWrite(9,r);
char bufr[5];
// convert 123 to string [buf]
;
char bufg[5];
// convert 123 to string [buf]
itoa(g, bufg, 10);
char bufb[5];
// convert 123 to string [buf]
itoa(b, bufb, 10);
lcd.clear();
lcd.setCursor(0,0);
lcd.print ("R:");
lcd.print(itoa(r, bufr, 10));
lcd.print (" G:");
lcd.print(itoa(g, bufg, 10));
lcd.print (" B:");
lcd.print(itoa(b, bufb, 10));// prins the values of rgb led by usinf "itoa" function
//itoa can convert an integer value to a string
delay (10);
if (r==vec1[0] && g==vec1[1] && b==vec1[2]) break;//this loop zooms the RGB integers to the target vector &output the change with delay
//(repeats for all vectors one after the other)
digitalWrite(8,HIGH);// providing ref V
a=analogRead(A5);//read
if (a>power) {analogWrite(11,0);
analogWrite(10,0);
analogWrite(9,0);
goto beg;}
}
while (1) {
char bufr1[5];
// convert 123 to string [buf]
;
char bufg1[5];
// convert 123 to string [buf]
itoa(vec2[0], bufg1, 10);
char bufb1[5];
// convert 123 to string [buf]
itoa(vec2[1], bufb1, 10);
lcd.clear();
lcd.setCursor(0,1);
lcd.print ("R:");
lcd.print(itoa(vec2[0], bufr1, 10));
lcd.print (" G:");
lcd.print(itoa(vec2[1], bufg1, 10));
lcd.print (" B:");
lcd.print(itoa(vec2[2], bufb1, 10));// prins the values of rgb led by usinf "itoa" function
//itoa can convert an integer value to a string
delay(10);
if (r>vec2[0]) {r--;} else
if (r
if (g>vec2[1]) {g--;} else
if (g
if (b>vec2[2]) {b--;} else
if (b
analogWrite(11,b);
analogWrite(10,g);
analogWrite(9,r);
char bufr[5];
// convert 123 to string [buf]
;
char bufg[5];
// convert 123 to string [buf]
itoa(g, bufg, 10);
char bufb[5];
// convert 123 to string [buf]
itoa(b, bufb, 10);
lcd.clear();
lcd.setCursor(0,0);
lcd.print ("R:");
lcd.print(itoa(r, bufr, 10));
lcd.print (" G:");
lcd.print(itoa(g, bufg, 10));
lcd.print (" B:");
lcd.print(itoa(b, bufb, 10));// prins the values of rgb led by usinf "itoa" function
//itoa can convert an integer vlue to a string
delay (10);
if (r==vec2[0] && g==vec2[1] && b==vec2[2]) break;
digitalWrite(8,HIGH);// providing ref V
a=analogRead(A5);//read
if (a>power) {analogWrite(11,0);
analogWrite(10,0);
analogWrite(9,0);
goto beg;}
}
while (1) {
char bufr1[5];
// convert 123 to string [buf]
;
char bufg1[5];
// convert 123 to string [buf]
itoa(vec4[0], bufg1, 10);
char bufb1[5];
// convert 123 to string [buf]
itoa(vec4[1], bufb1, 10);
lcd.clear();
lcd.setCursor(0,1);
lcd.print ("R:");
lcd.print(itoa(vec4[0], bufr1, 10));
lcd.print (" G:");
lcd.print(itoa(vec4[1], bufg1, 10));
lcd.print (" B:");
lcd.print(itoa(vec4[2], bufb1, 10));// prins the values of rgb led by usinf "itoa" function
//itoa can convert an integer value to a string
delay(10);
if (r>vec4[0]) {r--;} else
if (r
if (g>vec4[1]) {g--;} else
if (g
if (b>vec4[2]) {b--;} else
if (b
analogWrite(11,b);
analogWrite(10,g);
analogWrite(9,r);
char bufr[5];
// convert 123 to string [buf]
;
char bufg[5];
// convert 123 to string [buf]
itoa(g, bufg, 10);
char bufb[5];
// convert 123 to string [buf]
itoa(b, bufb, 10);
lcd.clear();
lcd.setCursor(0,0);
lcd.print ("R:");
lcd.print(itoa(r, bufr, 10));
lcd.print (" G:");
lcd.print(itoa(g, bufg, 10));
lcd.print (" B:");
lcd.print(itoa(b, bufb, 10));// prins the values of rgb led by usinf "itoa" function
//itoa can convert an integer vlue to a string
delay (10);
if (r==vec4[0] && g==vec4[1] && b==vec4[2]) break;
digitalWrite(8,HIGH);// providing ref V
a=analogRead(A5);//read
if (a>power) {analogWrite(11,0);
analogWrite(10,0);
analogWrite(9,0);
goto beg;}
}
while (1) {
char bufr1[5];
// convert 123 to string [buf]
;
char bufg1[5];
// convert 123 to string [buf]
itoa(vec5[0], bufg1, 10);
char bufb1[5];
// convert 123 to string [buf]
itoa(vec5[1], bufb1, 10);
lcd.clear();
lcd.setCursor(0,1);
lcd.print ("R:");
lcd.print(itoa(vec5[0], bufr1, 10));
lcd.print (" G:");
lcd.print(itoa(vec5[1], bufg1, 10));
lcd.print (" B:");
lcd.print(itoa(vec5[2], bufb1, 10));// prins the values of rgb led by usinf "itoa" function
//itoa can convert an integer value to a string
delay(10);
if (r>vec5[0]) {r--;} else
if (r
if (g>vec5[1]) {g--;} else
if (g
if (b>vec5[2]) {b--;} else
if (b
analogWrite(11,b);
analogWrite(10,g);
analogWrite(9,r);
char bufr[5];
// convert 123 to string [buf]
;
char bufg[5];
// convert 123 to string [buf]
itoa(g, bufg, 10);
char bufb[5];
// convert 123 to string [buf]
itoa(b, bufb, 10);
lcd.clear();
lcd.setCursor(0,0);
lcd.print ("R:");
lcd.print(itoa(r, bufr, 10));
lcd.print (" G:");
lcd.print(itoa(g, bufg, 10));
lcd.print (" B:");
lcd.print(itoa(b, bufb, 10));// prins the values of rgb led by usinf "itoa" function
//itoa can convert an integer vlue to a string
delay (10);
if (r==vec5[0] && g==vec5[1] && b==vec5[2]) break;
digitalWrite(8,HIGH);// providing ref V
a=analogRead(A5);//read
if (a>power) {analogWrite(11,0);
analogWrite(10,0);
analogWrite(9,0);
goto beg;}
}
while (1) {
char bufr1[5];
// convert 123 to string [buf]
;
char bufg1[5];
// convert 123 to string [buf]
itoa(vec6[0], bufg1, 10);
char bufb1[5];
// convert 123 to string [buf]
itoa(vec6[1], bufb1, 10);
lcd.clear();
lcd.setCursor(0,1);
lcd.print ("R:");
lcd.print(itoa(vec6[0], bufr1, 10));
lcd.print (" G:");
lcd.print(itoa(vec6[1], bufg1, 10));
lcd.print (" B:");
lcd.print(itoa(vec6[2], bufb1, 10));// prins the values of rgb led by usinf "itoa" function
//itoa can convert an integer value to a string
delay(10);
if (r>vec6[0]) {r--;} else
if (r
if (g>vec6[1]) {g--;} else
if (g
if (b>vec6[2]) {b--;} else
if (b
analogWrite(11,b);
analogWrite(10,g);
analogWrite(9,r);
char bufr[5];
// convert 123 to string [buf]
;
char bufg[5];
// convert 123 to string [buf]
itoa(g, bufg, 10);
char bufb[5];
// convert 123 to string [buf]
itoa(b, bufb, 10);
lcd.clear();
lcd.setCursor(0,0);
lcd.print ("R:");
lcd.print(itoa(r, bufr, 10));
lcd.print (" G:");
lcd.print(itoa(g, bufg, 10));
lcd.print (" B:");
lcd.print(itoa(b, bufb, 10));// prins the values of rgb led by usinf "itoa" function
//itoa can convert an integer vlue to a string
delay (10);
if (r==vec6[0] && g==vec6[1] && b==vec6[2]) break;
digitalWrite(8,HIGH);// providing ref V
a=analogRead(A5);//read
if (a>power) {analogWrite(11,0);
analogWrite(10,0);
analogWrite(9,0);
goto beg;}
}
while (1) {
char bufr1[5];
// convert 123 to string [buf]
;
char bufg1[5];
// convert 123 to string [buf]
itoa(vec3[0], bufg1, 10);
char bufb1[5];
// convert 123 to string [buf]
itoa(vec3[1], bufb1, 10);
lcd.clear();
lcd.setCursor(0,1);
lcd.print ("R:");
lcd.print(itoa(vec3[0], bufr1, 10));
lcd.print (" G:");
lcd.print(itoa(vec3[1], bufg1, 10));
lcd.print (" B:");
lcd.print(itoa(vec3[2], bufb1, 10));// prins the values of rgb led by usinf "itoa" function
//itoa can convert an integer value to a string
delay(10);
if (r>vec3[0]) {r--;} else
if (r
if (g>vec3[1]) {g--;} else
if (g
if (b>vec3[2]) {b--;} else
if (b
analogWrite(11,b);
analogWrite(10,g);
analogWrite(9,r);
char bufr[5];
// convert 123 to string [buf]
;
char bufg[5];
// convert 123 to string [buf]
itoa(g, bufg, 10);
char bufb[5];
// convert 123 to string [buf]
itoa(b, bufb, 10);
lcd.clear();
lcd.setCursor(0,0);
lcd.print ("R:");
lcd.print(itoa(r, bufr, 10));
lcd.print (" G:");
lcd.print(itoa(g, bufg, 10));
lcd.print (" B:");
lcd.print(itoa(b, bufb, 10));// prins the values of rgb led by usinf "itoa" function
//itoa can convert an integer vlue to a string
delay (10);
if (r==vec3[0] && g==vec3[1] && b==vec3[2]) break;//the last loop returns the RGB integers to the refference vector-this is critical
// so their won't be any "jumps" in colors
digitalWrite(8,HIGH);// providing ref V
a=analogRead(A5);//read
if (a>power) {analogWrite(11,0);
analogWrite(10,0);
analogWrite(9,0);
goto beg;}
}
goto next;
}
please take notice that the original code was for an LCD shield that is not available and was purchased here:
http://www.dan-e.co.il/index.php?route=common/home
also notice, that the program my contain uneeded parts and is builed "patch over patch" so there is some work to do if you want the code to be "clean" you my also use different ports at the uno, just don't forget to adjust the code for those changes.
good luck :-)
Dekel Ziv