Created
December 26, 2011 13:28
-
-
Save dundee/1521148 to your computer and use it in GitHub Desktop.
RGB led
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
// Output | |
int redPin = 9; // Red LED, connected to digital pin 9 | |
int greenPin = 10; // Green LED, connected to digital pin 10 | |
int bluePin = 11; // Blue LED, connected to digital pin 11 | |
int r, g, b; | |
int rd, gd, bd; | |
int wait = 20; //20ms | |
void outputColour(int red, int green, int blue) { | |
analogWrite(redPin, red); | |
analogWrite(bluePin, blue); | |
analogWrite(greenPin, green); | |
} | |
void setup() | |
{ | |
pinMode(redPin, OUTPUT); // sets the pins as output | |
pinMode(greenPin, OUTPUT); | |
pinMode(bluePin, OUTPUT); | |
r = g = b = 0; | |
Serial.begin(9600); | |
} | |
// Main program | |
void loop() | |
{ | |
if (r >= 255) { | |
rd = -1; | |
} | |
if (r <= 1) { | |
rd = 1; | |
} | |
if (g >= 254) { | |
gd = -2; | |
} | |
if (g <= 1) { | |
gd = 2; | |
} | |
if (b >= 253) { | |
bd = -3; | |
} | |
if (b <= 1) { | |
bd = 3; | |
} | |
r += rd; | |
g += gd; | |
b += bd; | |
outputColour(r, g, b); | |
Serial.print(r); | |
Serial.print(g); | |
Serial.println(b); | |
delay(wait); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment