i'm not really a bumblebee
cookies
romillyhills.co.uk uses cookies! cookies are used for the login system and removing this bubble.
rgb%20button.jpg
clock19:26 on 15.04.12 eye163 speech bubble0
as a sunday afternoon time waster i thought i'd figure out how to use some of the random parts i have. so began the whimsical tale of tri-colour led and the button

it turns out that a tricolour led is just 3 leds with a shared cathode, after discovering which pin was the cathode [the longest one] wiring it up was pretty easy.

the button is just as easy... i googled it and found the arduino button example.

so armed with this knowledge i thought i'd make a simple circuit which changed the colour of the led when the button is pressed.

which means it's big block of code time!

int r=14;
int g=15;
int b=16;
int in=8;
int moo=1;
int poo=0;
void setup(){
pinMode(r, OUTPUT);
pinMode(g, OUTPUT);
pinMode(b, OUTPUT);
pinMode(in, INPUT);
}
void loop(){
if (poo==0){
if (digitalRead(in)==HIGH) {
poo=1;
moo=moo+1;
}
}
if (digitalRead(in)==LOW) {
poo=0;
}
if(moo>=4){
moo=1;
}
if(moo==1){
analogWrite(r, 150);
analogWrite(g, 0);
analogWrite(b, 0);
}else if(moo==2){
analogWrite(r, 0);
analogWrite(g, 150);
analogWrite(b, 0);
}else if(moo==3){
analogWrite(r, 0);
analogWrite(g, 0);
analogWrite(b, 150);
}
}


and here is a crudely drawn circuit diagram

rgb button diagram


where r1 is arbitrary and r2 is 10k ohm.

so, the interesting points here are moo and poo.

moo is set up to increase every time the button is pressed, then depending on the value of moo a different colour of led is turned on. then there is a little if statement which stops moo from getting too large.

the problem with this is that moo was increasing constantly while the button was down, which meant about 1000 time a second or whatever.

which means i added poo, poo is a switch that stops the arduino from reading pin 8 until the button is released. this way the led will only change colour once every time you press the button.

and that's how you tricolour led button
what happened to my arms!?