Skip to content

Instantly share code, notes, and snippets.

@BDFife
Created December 8, 2018 19:57
Show Gist options
  • Save BDFife/d44c17271687f4ef86914110f8252d63 to your computer and use it in GitHub Desktop.
Save BDFife/d44c17271687f4ef86914110f8252d63 to your computer and use it in GitHub Desktop.
Fairy Lamp Code Arduino
int redPin = 5;
int greenPin = 6;
int bluePin = 3;
int ledMax = 40;
int ledMin = 5;
//int brightness = ledMin;
//int fadeAmount = 1;
int redVal = ledMin;
int greenVal = ledMin;
int blueVal = ledMin;
int redFade = 1;
int greenFade = 2;
int blueFade = 3;
void setup() {
pinMode (redPin, OUTPUT);
pinMode (greenPin, OUTPUT);
pinMode (bluePin, OUTPUT);
}
void loop() {
// setColor(brightness, brightness, brightness);
// brightness = brightness + fadeAmount;
// if (brightness <= ledMin || brightness >= ledMax) {
// fadeAmount = -fadeAmount;
// }
setColor(redVal, greenVal, blueVal);
redVal = redVal + redFade;
greenVal = greenVal + greenFade;
blueVal = blueVal + blueFade;
if (redVal <= ledMin || redVal >= ledMax) {
redFade = -redFade;
}
if (greenVal <= ledMin || greenVal >= ledMax) {
greenFade = -greenFade;
}
if (blueVal <= ledMin || blueVal >= ledMax) {
blueFade = -blueFade;
}
delay(120);
}
void setColor (int red, int green, int blue) {
// using common anode LED
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
analogWrite (redPin, red);
analogWrite (greenPin, green);
analogWrite(bluePin, blue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment