Last active
July 8, 2025 14:35
-
-
Save zachflower/79df9ed5ca398264d3b6 to your computer and use it in GitHub Desktop.
Smooth RGB LED Color Transitions (Arduino)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int redPin = 11; | |
int greenPin = 10; | |
int bluePin = 9; | |
int r = 0; | |
int g = 0; | |
int b = 0; | |
void setup() { | |
pinMode(redPin, OUTPUT); | |
pinMode(greenPin, OUTPUT); | |
pinMode(bluePin, OUTPUT); | |
} | |
void loop() { | |
setColor(255, 0, 0); // red | |
setColor(0, 255, 0); // green | |
setColor(0, 0, 255); // blue | |
setColor(255, 255, 0); // yellow | |
setColor(80, 0, 80); // purple | |
setColor(0, 255, 255); // aqua | |
} | |
void setColor(int red, int green, int blue) { | |
while ( r != red || g != green || b != blue ) { | |
if ( r < red ) r += 1; | |
if ( r > red ) r -= 1; | |
if ( g < green ) g += 1; | |
if ( g > green ) g -= 1; | |
if ( b < blue ) b += 1; | |
if ( b > blue ) b -= 1; | |
_setColor(); | |
delay(10); | |
} | |
} | |
void _setColor() { | |
analogWrite(redPin, r); | |
analogWrite(greenPin, g); | |
analogWrite(bluePin, b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Made this with reduceable brightness, my led was too bright to stare at and i figured some people might have the same problem. :D