Skip to content

Instantly share code, notes, and snippets.

@shfitz
Last active November 11, 2020 21:11
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 shfitz/2c8c5dabc1daf0fd03db3cfcd39cb78c to your computer and use it in GitHub Desktop.
Save shfitz/2c8c5dabc1daf0fd03db3cfcd39cb78c to your computer and use it in GitHub Desktop.
// color mixing with common cathode rgb led
const int rPin = 4;
const int gPin = 3;
const int bPin = 2;
// value to increment/decrement
int rDir = 1;
int gDir = -1;
int bDir = -1;
// inital color vals
int rVal = 1;
int gVal = 127;
int bVal = 254;
void setup() {
// set the pinmodes
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(bPin, OUTPUT);
// check the colors
colorTest();
}
void loop() {
// write the color values
analogWrite(rPin, rVal);
analogWrite(gPin, gVal);
analogWrite(bPin, bVal);
// check if the brightness has reached upper or lower limit
// if so, reverse direction
if (rVal <= 0 || rVal >= 255) rDir = -rDir;
if (gVal <= 0 || gVal >= 255) gDir = -gDir;
if (bVal <= 0 || bVal >= 255) bDir = -bDir;
// increment or decrement values
rVal += rDir;
gVal += gDir;
bVal += bDir;
// slight delay
delay(15);
}
void colorTest() {
analogWrite(rPin, 255);
delay(250);
analogWrite(rPin, 0);
analogWrite(gPin, 255);
delay(250);
analogWrite(bPin, 255);
analogWrite(gPin, 0);
delay(250);
analogWrite(rPin, 0);
analogWrite(gPin, 255);
analogWrite(bPin, 255);
delay(250);
analogWrite(rPin, 255);
analogWrite(gPin, 255);
analogWrite(bPin, 0);
delay(250);
analogWrite(rPin, 255);
analogWrite(gPin, 0);
analogWrite(bPin, 255);
delay(250);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment