RGBLED: Difference between revisions
Jump to navigation
Jump to search
m (added resistor values for 5v) |
(Added starter code) |
||
Line 15: | Line 15: | ||
Volts: 2.0 0.0 3.2 3.2 | Volts: 2.0 0.0 3.2 3.2 | ||
</pre> | </pre> | ||
== Code == | |||
Modified version of "Fade" sample code, to cycle thru fading one at a time, Reg, Green and Blue. | |||
<pre> | |||
/* | |||
RGB Fade | |||
This example shows how to fade an LED on pin 3, 5 & 6 | |||
using the analogWrite() function. | |||
This is an extension of "Fade" example code in the public domain. | |||
This version is provided as-is for whatever purposes -thex | |||
*/ | |||
int redPin = 3; // PWM out pin for Red | |||
int greenPin = 6; // PWM out pin for Green | |||
int bluePin = 5; // PWM out pin for Blue | |||
float redMax = 0.5; | |||
float greenMax = 0.8; | |||
float blueMax = 1.0; | |||
int activePin; // currently active color | |||
float activeMax; | |||
int brightness; // how bright the LED is | |||
int fadeAmount = 5; // how many points to fade the LED by | |||
void setup() { | |||
brightness = int(fadeAmount); | |||
activePin = redPin; | |||
// declare output pins to be an output: | |||
pinMode(redPin, OUTPUT); | |||
pinMode(greenPin, OUTPUT); | |||
pinMode(bluePin, OUTPUT); | |||
} | |||
void loop() { | |||
// set the brightness of active color | |||
analogWrite(activePin, brightness * activeMax); | |||
// change the brightness for next time through the loop: | |||
brightness = brightness + fadeAmount; | |||
if (brightness == 255) { | |||
// Reverse fade | |||
fadeAmount = -fadeAmount ; | |||
} | |||
if (brightness == 0) | |||
{ | |||
// Turn off current color, and switch to next | |||
analogWrite(activePin, 0); | |||
if (activePin == redPin) | |||
{ | |||
activePin = greenPin; | |||
activeMax = greenMax; | |||
} | |||
else if (activePin == greenPin) | |||
{ | |||
activePin = bluePin; | |||
activeMax = blueMax; | |||
} | |||
else | |||
{ | |||
activePin = redPin; | |||
activeMax = redMax; | |||
} | |||
// Reset for next color | |||
fadeAmount = -fadeAmount; | |||
brightness = int(fadeAmount); | |||
} | |||
// wait for 30 milliseconds to see the dimming effect | |||
delay(30); | |||
} | |||
</pre> | |||
[[Category:Fort]] |
Revision as of 04:35, 24 December 2013
5mm water clear LED's with 4 pins.
The 2nd pin (from the left, with the flat spot oriented to the left) is a common cathode ground.
Pin 1 is Red, Pin 3 is Blue, Pin 4 is Green, on newer versions the Blue and Green maybe switched.
Recommended current limiting of 20ma max is advized, 30ma is the noted max at 1/10th duty cycle at 10khz.
When driving at 5v, a 150 ohm resistor for Red, and a 100 ohm resistor for each Green and Blue limits current to around 20ma.
1 2 3 4 | | | | Color: R - B G Volts: 2.0 0.0 3.2 3.2
Code
Modified version of "Fade" sample code, to cycle thru fading one at a time, Reg, Green and Blue.
/* RGB Fade This example shows how to fade an LED on pin 3, 5 & 6 using the analogWrite() function. This is an extension of "Fade" example code in the public domain. This version is provided as-is for whatever purposes -thex */ int redPin = 3; // PWM out pin for Red int greenPin = 6; // PWM out pin for Green int bluePin = 5; // PWM out pin for Blue float redMax = 0.5; float greenMax = 0.8; float blueMax = 1.0; int activePin; // currently active color float activeMax; int brightness; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by void setup() { brightness = int(fadeAmount); activePin = redPin; // declare output pins to be an output: pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); } void loop() { // set the brightness of active color analogWrite(activePin, brightness * activeMax); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; if (brightness == 255) { // Reverse fade fadeAmount = -fadeAmount ; } if (brightness == 0) { // Turn off current color, and switch to next analogWrite(activePin, 0); if (activePin == redPin) { activePin = greenPin; activeMax = greenMax; } else if (activePin == greenPin) { activePin = bluePin; activeMax = blueMax; } else { activePin = redPin; activeMax = redMax; } // Reset for next color fadeAmount = -fadeAmount; brightness = int(fadeAmount); } // wait for 30 milliseconds to see the dimming effect delay(30); }