Last active
November 11, 2020 21:11
-
-
Save shfitz/2c8c5dabc1daf0fd03db3cfcd39cb78c to your computer and use it in GitHub Desktop.
This file contains 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
// 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