Created
October 19, 2014 00:10
-
-
Save jimwhitfield/8e292df5b38c9fe598d8 to your computer and use it in GitHub Desktop.
Simple little arduino sketch that cycles through a bunch of color combinations on an arduino driving an RGB LED.
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
const int RED_PIN = 9; | |
const int GREEN_PIN = 10; | |
const int BLUE_PIN = 11; | |
const int MAX_VAL=255; | |
void setup() { | |
// initialize digital pins as output: | |
for (int i = PIN_BASE; i <= PIN_BASE+PIN_CT; i++) { | |
pinMode(i, OUTPUT); | |
} | |
// Start off with the LED . (0 = | |
setColorRgb(RED_PIN,0, GREEN_PIN,0, BLUE_PIN,0); | |
} | |
const bool INVERT=true; // invert if driving a common anode RBG LED | |
void setColorRgb(unsigned int redPin, unsigned int red, unsigned int greenPin, unsigned int green, unsigned int bluePin, unsigned int blue) { | |
analogWrite(redPin, INVERT? 255-red: red); | |
analogWrite(greenPin, INVERT? 255-green: green); | |
analogWrite(bluePin, INVERT? 255-blue: blue); | |
} | |
void fader( | |
int r_pin, int r_start, int r_end, | |
int g_pin, int g_start, int g_end, | |
int b_pin, int b_start, int b_end) | |
{ | |
int steps=16; | |
int one_step=MAX_VAL/steps; | |
int delay_ms=20; | |
int r_delta = (r_end - r_start)/steps; | |
int g_delta = (g_end - g_start)/steps; | |
int b_delta = (b_end - b_start)/steps; | |
int r_now = r_start; | |
int g_now = g_start; | |
int b_now = b_start; | |
int i=0; | |
setColorRgb(r_pin, r_now, g_pin, g_now, b_pin, b_now); | |
while (i < MAX_VAL) { | |
r_now += r_delta; | |
g_now += g_delta; | |
b_now += b_delta; | |
setColorRgb(r_pin, r_now, g_pin, g_now, b_pin, b_now); | |
delay(delay_ms); | |
i += one_step; | |
} | |
} | |
void loop() { | |
// white | |
fader(RED_PIN,0,MAX_VAL, GREEN_PIN,0,MAX_VAL, BLUE_PIN,0,MAX_VAL); | |
delay(100); | |
fader(RED_PIN,MAX_VAL,0, GREEN_PIN,MAX_VAL,0, BLUE_PIN,MAX_VAL,0); | |
delay(100); | |
//red | |
fader(RED_PIN,0,MAX_VAL, GREEN_PIN,0,0, BLUE_PIN,0,0); | |
delay(100); | |
fader(RED_PIN,MAX_VAL,0, GREEN_PIN,0,0, BLUE_PIN,0,0); | |
delay(100); | |
//green | |
fader(RED_PIN,0,0, GREEN_PIN,0,MAX_VAL, BLUE_PIN,0,0); | |
delay(100); | |
fader(RED_PIN,0,0, GREEN_PIN,MAX_VAL,0, BLUE_PIN,0,0); | |
delay(100); | |
//blue | |
fader(RED_PIN,0,0, GREEN_PIN,0,0, BLUE_PIN,0,MAX_VAL); | |
delay(100); | |
fader(RED_PIN,0,0, GREEN_PIN,0,0, BLUE_PIN,MAX_VAL,0); | |
delay(100); | |
//redless (cyan) | |
fader(RED_PIN,0,0, GREEN_PIN,0,MAX_VAL, BLUE_PIN,0,MAX_VAL); | |
delay(100); | |
fader(RED_PIN,0,0, GREEN_PIN,MAX_VAL,0, BLUE_PIN,MAX_VAL,0); | |
delay(100); | |
//blueless yellow | |
fader(RED_PIN,0,MAX_VAL, GREEN_PIN,0,MAX_VAL, BLUE_PIN,0,0); | |
delay(100); | |
fader(RED_PIN,MAX_VAL,0, GREEN_PIN,MAX_VAL,0, BLUE_PIN,0,0); | |
delay(100); | |
// greenless magenta | |
fader(RED_PIN,0,MAX_VAL, GREEN_PIN,0,0, BLUE_PIN,0,MAX_VAL); | |
delay(100); | |
fader(RED_PIN,MAX_VAL,0, GREEN_PIN,0,0, BLUE_PIN,MAX_VAL,0); | |
delay(100); | |
fader(RED_PIN,0,MAX_VAL, GREEN_PIN,0,0, BLUE_PIN,0,0); | |
delay(100); | |
fader(RED_PIN,MAX_VAL,0, GREEN_PIN,0,MAX_VAL, BLUE_PIN,0,0); | |
delay(100); | |
fader(RED_PIN,0,0, GREEN_PIN,MAX_VAL,0, BLUE_PIN,0,MAX_VAL); | |
delay(100); | |
fader(RED_PIN,0,MAX_VAL, GREEN_PIN,0,0, BLUE_PIN,MAX_VAL,0); | |
delay(100); | |
fader(RED_PIN,MAX_VAL,0, GREEN_PIN,0,MAX_VAL, BLUE_PIN,0,0); | |
delay(100); | |
fader(RED_PIN,0,0, GREEN_PIN,MAX_VAL,0, BLUE_PIN,0,MAX_VAL); | |
delay(100); | |
fader(RED_PIN,0,MAX_VAL, GREEN_PIN,0,0, BLUE_PIN,MAX_VAL,0); | |
fader(RED_PIN,MAX_VAL,0, GREEN_PIN,0,0, BLUE_PIN,MAX_VAL,0); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment