1 rgb starts off, then when you press the button it changes to another color. press it again and it changes to another color. It would have say 8 colors you could go through (hard coded ofcourse, incase color changes need to be made).
so here's what i have:
1 RGB led and 3 resistors
1 push button and a resistor
i'd like to know how to connect it together and make it work right.. i haven't found an example yet. can someone please give me a diagram?
actually, there will be a second button, it will do something else, but will also turn that RGB led off as well..
#define r 3 // via R to led cathode
#define g 5
#define b 6
#define key 8 // push button to gnd
void setup()
{
pinMode(r,OUTPUT);
pinMode(g,OUTPUT);
pinMode(b,OUTPUT);
pinMode(key,INPUT_PULLUP);
}
void loop()
{
while (digitalRead(key)==HIGH); // wait for keypress
analogWrite(r,random(256);
analogWrite(g,random(256);
analogWrite(b,random(256);
while (digitalRead(key)==LOW); // wait for key release
}
Change the buttons connections. Remove resistors and connect ather side to GNG (internal PULLUPS handles it)
not exacly, but closer
no waiting for button release.. press and let go for each color
Does this means 'free running at full speed' or step to next color?
EDIT: code changed..
#define r 3 // via R to led cathode
#define g 5
#define b 6
#define key 8
byte color[][3] =
{
{200,200,0},
{200,0,200},
{0,200,200},
// add more
};
byte nbr = sizeof(color)/3;
void setup()
{
pinMode(r,OUTPUT);
pinMode(g,OUTPUT);
pinMode(b,OUTPUT);
pinMode(key,INPUT_PULLUP);
}
void loop()
{
for(byte i=0;i<nbr;i++)
{
while (digitalRead(key)==LOW); // wait for key release .. this is fair for debounce
while (digitalRead(key)==HIGH); // wait for keypress
analogWrite(r,color[i][0]);
analogWrite(g,color[i][1]);
analogWrite(b,color[i][2]);
}
}
Your loop count runs so fast (as you would expect of a microprocessor) that the bouncing of the button will be interpreted as a random number of successive keypresses - not satisfactory according to the OP's specification.
A very crude approach would be to insert delay(10) after each "while".
FWIW, I have explained - or tried to - comprehensive debounce routines here.
I know its not it.. but often works ok ..(there is a chip for this. I use it with my rotary encoder.)
1.st a placed thee release loop after writing leds.. (som ns later)
I havnt' yet fully understood how this circuit is ment to work..hope I'm closing in - a problem at a time..
Does this means 'free running at full speed' or step to next color?
i don't know what you mean by that..
the RGB led starts off, then you press the button and it's first color (say red), appears.. press the button again and it's next color appears (say purple), press it again and it's third color appears (say yellow).. so on and so forth.. that's why i say the colors must be hard coded into the code.. that way i can change the 8 colors if need be.
the second button has 2 functions.. but for now i just want it to turn off the RGB led..
// Description: This program is designed to blink each color of a RGB LED
// When the reading on the input pin is low. You can switch the delay to your
// convienience :)
//
// Designed by: Bradley Bare
#define red 12
#define blue 11
#define green 10
//Pin outputs for RGB LED
#define input 7
//Input pin for buttom
void setup()
{
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(green,OUTPUT);
pinMode(input,INPUT);
return;
}
long unsigned delayCurrent=0, delayPrev=0;
const int Delay=100;
int count=0;
void loop()
{
delayCurrent=millis();
if( (delayCurrent-delayPrev>Delay) && digitalRead(input)==LOW )
{
if(count==0 || count==1 || count==2)
digitalWrite(red,HIGH);
else
digitalWrite(red,LOW);
//RED if
if( count==1 || count==2 || count==3 || count==5)
digitalWrite(green,HIGH);
else
digitalWrite(green,LOW);
//GREEN if
if(count==2 || count==3 || count==4)
digitalWrite(blue,HIGH);
else
digitalWrite(blue,LOW);
//BLUE if
count++;
if(count>5)
count=0;
delayPrev=delayCurrent;
}
}