A simple RGB LED lamp for illuminating a keyboard tray.
To make the experience fit your profile, pick a username and tell us what interests you.
We found and based on your interests.
Each press of the button cycles the LED colors. It turns off after red.
Just wire it up and upload the following code.
int redPin = 3;
int greenPin = 5;
int bluePin = 6;
int buttonPin = 2;
int initialCount = 0;
//uncomment this line if using a Common Anode LED
#define COMMON_ANODE
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
setColor(0,0,0);
}
void loop()
{
int buttonState = digitalRead(buttonPin);
if ( buttonState == LOW ){
delay(500); //delay for debounce
initialCount++;
if (initialCount > 4 ){
initialCount = 0;
}
switch (initialCount){
case 0:
setColor(0,0,0);
break;
case 1:
setColor(0, 255, 0); // green
break;
case 2:
setColor(0, 255, 255); // aqua
break;
case 3:
setColor(0, 0, 255); // blue
break;
case 4:
setColor(255, 0, 0); // red
break;
}
}
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
Create an account to leave a comment. Already have an account? Log In.
Become a member to follow this project and never miss any updates