Skip to content

Instantly share code, notes, and snippets.

@teos0009
Last active December 28, 2021 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teos0009/0b90bd53d8b8034e63cfa47bd6579995 to your computer and use it in GitHub Desktop.
Save teos0009/0b90bd53d8b8034e63cfa47bd6579995 to your computer and use it in GitHub Desktop.
// Arduino RGB Fading for Common Anode Rgb Leds
//D3,D5,D6 are PWM pins
const int redPin = 3;
const int grnPin = 5;
const int bluPin = 6;
// Setup for outputs
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
}
// Loop for fading
void loop() {
redtoyellow();
yellowtogreen();
greentocyan();
cyantoblue();
bluetomagenta();
magentatored();
}
// Functions for fade between 2 colors
void redtoyellow()
{
digitalWrite(redPin, LOW);
digitalWrite(bluPin, HIGH);
// fade up green
for(byte i=1; i<100; i++) {
byte on = i;
byte off = 100-on;
for( byte a=0; a<100; a++ ) {
digitalWrite(grnPin, LOW);
delayMicroseconds(on);
digitalWrite(grnPin, HIGH);
delayMicroseconds(off);
}
}
}
void yellowtogreen()
{
digitalWrite(grnPin, LOW);
digitalWrite(bluPin, HIGH);
// fade down red
for(byte i=1; i<100; i++) {
byte on = 100-i;
byte off = i;
for( byte a=0; a<100; a++ ) {
digitalWrite(redPin, LOW);
delayMicroseconds(on);
digitalWrite(redPin, HIGH);
delayMicroseconds(off);
}
}
}
void greentocyan()
{
digitalWrite(grnPin, LOW);
digitalWrite(redPin, HIGH);
// fade up blue
for(byte i=1; i<100; i++) {
byte on = i;
byte off = 100-on;
for( byte a=0; a<100; a++ ) {
digitalWrite(bluPin, LOW);
delayMicroseconds(on);
digitalWrite(bluPin, HIGH);
delayMicroseconds(off);
}
}
}
void cyantoblue()
{
digitalWrite(bluPin, LOW);
digitalWrite(redPin, HIGH);
// fade down green
for(byte i=1; i<100; i++) {
byte on = 100-i;
byte off = i;
for( byte a=0; a<100; a++ ) {
digitalWrite(grnPin, LOW);
delayMicroseconds(on);
digitalWrite(grnPin, HIGH);
delayMicroseconds(off);
}
}
}
void bluetomagenta()
{
digitalWrite(bluPin, LOW);
digitalWrite(grnPin, HIGH);
// fade up red
for(byte i=1; i<100; i++) {
byte on = i;
byte off = 100-on;
for( byte a=0; a<100; a++ ) {
digitalWrite(redPin, LOW);
delayMicroseconds(on);
digitalWrite(redPin, HIGH);
delayMicroseconds(off);
}
}
}
void magentatored()
{
digitalWrite(redPin, LOW);
digitalWrite(grnPin, HIGH);
// fade down blue
for(byte i=1; i<100; i++) {
byte on = 100-i;
byte off = i;
for( byte a=0; a<100; a++ ) {
digitalWrite(bluPin, LOW);
delayMicroseconds(on);
digitalWrite(bluPin, HIGH);
delayMicroseconds(off);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment