RGB LEDs are used everywhere, in many different products and devices. This Arduino library is to the control RGB LED's colour and fading between colours. Almost every setting can be controlled though this library to control the RGB LEDs. The library contains preset colours and fuctions to make it easier to construct your RGB LED project.
Any PWM pins can be used for and LED/LED color. Below is and example for pin 9 = red, pin 10 = green and pin 11 = blue.
RGB_LED LED(9,10,11);
This is the setup for the class, this is where the pins are declared for the RGB LED. pinR, pinG and pinB is where the PWM pin numbers are placed.
This function sets a RGB colour. It will stop a fade to be the set color. Rvalue, Gvalue and Bvalue are used to set the RGB colour. These RGB Byte variables are the values between 0 and 255. 0 is the LED completely off, 255 is the LED Completely on and all values between is a PWM percent.
This Functions sets the speed of fades and the speed of colour functions. The speedValue is in milliseconds and is used from the start to the finish of the fade.
This function set the where the fade is. The FadeValue is a between 0 and 1. If the FadeValue is set to be 0.5 at 1000 speed the fade would continue from 50%.
This function is used to fade from the current set colour to a RGB colour at a speed. Rvalue, Gvalue and Bvalue are used to set the RGB colour and speedValue is used to set the fade speed. These Rvalue, Gvalue and Bvalue Byte variables are the values between 0 and 255. 0 is the LED completely off, 255 is the LED Completely on and all values between is a PWM percent. The speedValue is in milliseconds and is used from the start to the finish of the fade.
This function is used to select a LED colour function. All the LED's colour functions are listed below under Colour Functions
This will make the LED fade to a random colour with a random fade speed.
This will set the LED to a selected colour. The colour can be chosen from the colours list below.
This will make the LED fade to a selected colour with a selected fade speed. The colour can be chosen from the colours list below. The speedValue is in milliseconds and is used from the start to the finish of the fade.
This will return the current set speed in milliseconds.
This will return the current fade percent of completion.
This will return the current set function.
This will return the current Red value.
This will return the current Green value.
This will return the current Blue value.
This will return the current functions step count.
This checks if the fade as finished and returns true when finished
This function is required to run as much as possible. This function runs the calculates and the controls the PWM for the LED.
This will stop all current fading and freeze the colour.
This function is used in replacement of Delay(). This delay will continue to process the run() function.
- Black
- White
- Red
- Green
- Blue
- Yellow
- Purple
- Aqua
- Random: this randomly fades betweens random colours at random speeds.
- Solid: this stays at a set colour.
- Fade: this fades betweens Red, Green and blue colours at a set speed.
- FadeRandom: this fades betweens random colours at a set speeds.
- Step1: this steps betweens Red, Green and blue colours at a set speed.
- Step2: this steps betweens all colours at a set speed.
- StepRandom: this steps betweens random colours at a set speeds.
#include <RGB_LED.h>
RGB_LED LED(9,10,11);
void setup()
{
LED.setFunction(Fade);
}
void loop()
{
LED.run();
}
#include <RGB_LED.h>
RGB_LED LED1(9,10,11);
RGB_LED LED2(3,4,5);
void setup()
{
LED1.setFunction(Fade);
LED2.setFunction(Step);
}
void loop()
{
LED1.run();
LED2.run();
}
#include <RGB_LED.h>
#define upButtonPin 4
#define downButtonPin 5
unsigned int speed = 1000;
RGB_LED LED(9,10,11);
void setup()
{
pinMode(upButtonPin,INPUT);
pinMode(downButtonPin,INPUT);
LED.setFunction(Fade);
}
void loop()
{
LED.run();
if(digitalRead(downButtonPin)==HIGH && speed<100)
{
speed--;
LED.setSpeed(speed);
LED.delay(10);
}
if(digitalRead(upButtonPin)==HIGH && speed>20000)
{
speed++;
LED.setSpeed(speed);
LED.delay(10);
}
}