Arduino RGB LED: Difference between revisions
From charlesreid1
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
In which we create a circuit to control an RGB LED... | In which we create a circuit to control an RGB LED... | ||
=Sketch Code= | |||
<source lang="c"> | |||
/* | |||
Adafruit Arduino - Lesson 3. RGB LED | |||
from https://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds/arduino-sketch | |||
*/ | |||
int redPin = 11; | |||
int greenPin = 10; | |||
int bluePin = 9; | |||
//uncomment this line if using a Common Anode LED | |||
//#define COMMON_ANODE | |||
void setup() | |||
{ | |||
pinMode(redPin, OUTPUT); | |||
pinMode(greenPin, OUTPUT); | |||
pinMode(bluePin, OUTPUT); | |||
} | |||
void loop() | |||
{ | |||
setColor(255, 0, 0); // red | |||
delay(1000); | |||
setColor(0, 255, 0); // green | |||
delay(1000); | |||
setColor(0, 0, 255); // blue | |||
delay(1000); | |||
setColor(255, 255, 0); // yellow | |||
delay(1000); | |||
setColor(80, 0, 80); // purple | |||
delay(1000); | |||
setColor(0, 255, 255); // aqua | |||
delay(1000); | |||
} | |||
void setColor(int red, int green, int blue) | |||
{ | |||
#ifdef COMMON_ANODE | |||
red = 255 - red; | |||
green = 255 - green; | |||
blue = 255 - blue; | |||
#endif | |||
analogWrite(redPin, red); | |||
analogWrite(greenPin, green); | |||
analogWrite(bluePin, blue); | |||
} | |||
</source> | |||
=References= | =References= | ||
Revision as of 16:49, 19 July 2015
In which we create a circuit to control an RGB LED...
Sketch Code
/*
Adafruit Arduino - Lesson 3. RGB LED
from https://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds/arduino-sketch
*/
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
//uncomment this line if using a Common Anode LED
//#define COMMON_ANODE
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
setColor(255, 0, 0); // red
delay(1000);
setColor(0, 255, 0); // green
delay(1000);
setColor(0, 0, 255); // blue
delay(1000);
setColor(255, 255, 0); // yellow
delay(1000);
setColor(80, 0, 80); // purple
delay(1000);
setColor(0, 255, 255); // aqua
delay(1000);
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
References
- adafruit lesson on rgb led: https://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds/breadboard-layout