Skip to content

Instantly share code, notes, and snippets.

@aryan-02
Created June 18, 2018 06:13
Show Gist options
  • Save aryan-02/de6f8b289f740387955a48bbc4ccf164 to your computer and use it in GitHub Desktop.
Save aryan-02/de6f8b289f740387955a48bbc4ccf164 to your computer and use it in GitHub Desktop.
A simple way to fade colors of RGB LEDs in Arduino
#define red 9 // Red pin number (PWM)
#define blue 10 // Green pin number (PWM)
#define green 11 // Blue pin number (PWM)
const int fadeAmount = 7; // Indicates how fast fading should happen.
void setup()
{
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}
void loop()
{
// Here is some sample code using the fade function.
rgb(0,0,0);
delay(1000);
fade(0, 0, 0, 255, 215, 5);
rgb(255, 215, 0);
delay(2000);
fade(255, 215, 0, 255, 0, 90);
delay(2000);
fade(255, 0, 90, 0, 20, 0);
delay(2000);
fade( 0, 20, 0, 200, 50, 50);
delay(2000);
fade(200, 50, 50 ,50, 70, 50);
delay(2000);
fade(50, 70, 50, 0, 90, 0);
delay(2000);
fade(0, 90, 0, 0, 255, 0);
delay(2000);
fade(0, 255, 0, 255, 0, 0);
delay(2000);
fade(255, 0, 0, 0, 0, 255);
delay(2000);
fade(0, 0, 255, 99, 100, 105);
}
int rgb (int redC, int greenC, int blueC) {
analogWrite(red, redC);
analogWrite(green, greenC);
analogWrite(blue, blueC);
}
int fade(int fromR, int fromG, int fromB, int toR, int toG, int toB)
{
rgb(fromR, fromG, fromB);
bool incrementR = (fromR <= toR);
bool incrementG = (fromG <= toG);
bool incrementB = (fromB <= toB);
if(incrementR) {
while(fromR < toR) {
analogWrite(red ,fromR);
fromR += (fadeAmount);
delay(30);
}
}
else{
while(fromR > toR) {
analogWrite(red ,fromR);
fromR -= (fadeAmount);
delay(30);
}
}
if(incrementG) {
while(fromG < toG) {
analogWrite(green ,fromG);
fromG += (fadeAmount);
delay(30);
}
}
else{
while(fromG > toG) {
analogWrite(green ,fromG);
fromG -= (fadeAmount);
delay(30);
}
}
if(incrementB) {
while(fromB < toB) {
analogWrite(blue ,fromB);
fromB += (fadeAmount);
delay(30);
}
}
else{
while(fromB > toB) {
analogWrite(blue ,fromB);
fromB -= (fadeAmount);
delay(30);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment