Pages: [1] 2   Go Down
Author Topic: Library for RGB LEDs  (Read 5413 times)
0 Members and 1 Guest are viewing this topic.
Brussels, Belgium
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

-- RGBMood Library --

Updated. V0.3
http://dl.dropbox.com/u/2155313/RGBMood.zip

--

Hi,

This is my first Library, I use it to control some RGB LEDs on a high level.
The library handles all the work for you.

Modes available:
FIX_MODE: The color doesn't change.
RANDOM_HUE_MODE: Color fades to random hue.
RAINBOW_HUE_MODE: Fading from red to red, going through all hue values.
RED_MODE: Random fade between redish colors.
BLUE_MODE: Random fade between blueish colors.
GREEN_MODE: Random fade between greenish colors.
FIRE_MODE: Fire effect. Like an electronic candle.
(More mode possible, ask for them smiley )

There are many examples file provided in the library.
Here's a basic example:

Here's an example (provided in the library), this code make the leds fade between colors automatically (handling speed and time for you).
Code:
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
#include <RGBMood.h>
RGBMood m(redPin, greenPin, bluePin);

void setup() {
  m.setMode(RGBMood::RANDOM_HUE_MODE);  // Automatic random fade.
  m.setHoldingTime(4000);  // Keep the same color for 4 seconds before fading again.
  m.setFadingSteps(150);   // Fade with 150 steps.
  m.setFadingSpeed(50);    // Each step last 50ms. A complete fade takes 50*150 = 7.5 seconds
  m.setHSB(random(359), 255, 255) // Random hue mode only change the hue so we first set the saturation and brightness to what we want.
}

void loop() {
  m.tick(); // Update the colors.
}

You can also control the speed of the fadings:
setHoldingTime(4000) // How many ms do we keep a color before fading to a new one.
setFadingSpeed(100) // How many ms between each step when fading.
setFadingSteps(50) // How many steps for fading from a color to another.

Here's the API
Code:
void setHSB(uint16_t, uint16_t, uint16_t);     // Set a fixed color from HSB color space.
void setRGB(uint16_t, uint16_t, uint16_t);     // Set a fixed color from RGB color space.
void setRGB(uint32_t); // Using Color class.
void fadeHSB(uint16_t, uint16_t, uint16_t, bool = true);    // Fade to a new color (given in HSB color space).
void fadeRGB(uint16_t, uint16_t, uint16_t);    // Fade to a new color (given in RGB color space).
void fadeRGB(uint32_t); // Using Color class.
void tick();                    // Update colors if needed. (call this in the loop function)
void hsb2rgb(uint16_t, uint16_t, uint16_t, uint16_t&, uint16_t&, uint16_t&); // Used internally to convert HSB to RGB
bool isFading() // True we are currently fading to a new color.
bool isStill()   // True if we are not fading to a new color.
void setMode(Modes m)  // Change the mode.
void setHoldingTime(uint16_t t) // How many ms do we keep a color before fading to a new one.
void setFadingSpeed(uint16_t t)  // How many ms between each step when fading.
void setFadingSteps(uint16_t t) // How many steps for fading from a color to another.
uint16_t red() // The current red color.
uint16_t green() // The current green color.
uint16_t blue() // The current blue color.

If you need more explanations, feel free to ask smiley-wink
Any remark will be very appreciated !

Pollop.
« Last Edit: February 04, 2012, 01:03:09 pm by pollop » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Thanks for sharing,

I would pass the three pins to the constructor of the lib, and let m.tick do the  analogWrite
furthermore you might use an ENUM for the modes.

something like
Code:
RGBmood M = new RGBMood(redpin, greenpin, bleupin);

void setup()
{
  m.setMode(1);           // Automatic random fade.
  m.setHoldingTime(4000); // Keep the same color for 4 seconds.
  m.setFadingSteps(100);  // Fade with 100 steps.
  m.setFadingSpeed(50);   // Each step last 50ms. A complete fade takes 50*100 = 5 seconds
}

void loop()
{
  // Update the colors.
  m.tick();
}


m.tick()
{
  // calculate new RGB values;
   // existing code here....
 
  analogWrite(_RED, _red);
  analogWrite(_GREEN, _green);
  analogWrite(_BLUE, _blue);
}

A fuction that could be interesting is one that calculates the distance between 2 colors
double distance(int R1, int G1, int B1, int R2, int G2, int B2)
{
  return Math.sqrt((R1-R2)*(R1-R2) + (G1-G2)*(G1-G2) + (B1-B2) * (B1-B2));
}

with this distance function you can estimate what a still is a smooth number of steps

my 2 cents,

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Brussels, Belgium
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot for your feedback !

I've already integrated the pins in the code as well as the analogWrite in the tick function.
I've made it optional, if you don't provide pins, it's not a problem. (so it's more flexible)

I've also made some adjustments for fading in HSB space.
For example when fading from Hue: 10 to Hue: 350, it won't go up but down.
i.e. 10->9->...->0->360->...->350 instead of 10->11->...->349->350.

What is great about this library that I forgot to mention is that there's not a single delay() call.
This mean you can use it with a code that can't be interrupted, Ethernet stuff for example.
The code uses internal counters and intervals and update when necessary.

I'm gonna look at color distance to make automatic smooth fading smiley-wink

All other feedbacks will be greatly appreciated.

Pollop.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What is great about this library that I forgot to mention is that there's not a single delay() call.
I am gratefull for that smiley

+ negative(RGB) like the old foto film.

+ setStroboscope(); do a stroboscope while morphing colors

+ enum for a set of "named colors"   Colors.Aquamarin etc smiley

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Brussels, Belgium
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks again for the feedback !

+ negative(RGB) like the old foto film.
Like setRGB but will actually do _r = 255 - r; _b = 255 - b ... ?

+ setStroboscope(); do a stroboscope while morphing colors
What do you mean by "while morphing" ?
You can already do a kind of stroboscope like this (careful if you're epileptic smiley-razz)
Code:
 m.setMode(RGBMood::RANDOM_MODE); // Yeah enum already there xD
  m.setHoldingTime(200);
  m.setFadingSteps(2);
  m.setFadingSpeed(10);

+ enum for a set of "named colors"   Colors.Aquamarin etc smiley
Working on it smiley-wink

Gimme more work smiley-grin

« Last Edit: February 04, 2012, 10:07:29 am by pollop » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Thanks again for the feedback !

Quote from: robtillaart on Today at 03:49:20 PM
+ negative(RGB) like the old foto film.
Like setRGB but will actually do _r = 255 - r; _b = 255 - b ... ?
Don't know the right formula, but it could be simple as that

Quote from: robtillaart on Today at 03:49:20 PM
+ setStroboscope(); do a stroboscope while morphing colors
What do you mean by "while morphing" ?
fading

You can already do a kind of stroboscope like this (careful if you're epileptic smiley-razz)
Code:

  m.setMode(RGBMood::RANDOM_MODE); // Yeah enum already there xD
  m.setHoldingTime(200);
  m.setFadingSteps(2);
  m.setFadingSpeed(10);
OK
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Cairns - Green Island for Winter
Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey just a small thing . I'm rusty , i grabbed your library and extracted it in my sketchbook/Libraries/ folder
Arduino V1.0 opens fine without library error but when I compile your example it does not find RGBMood.h.
I use Linux but everything else seems OK
What could cause this?
« Last Edit: February 05, 2012, 04:47:18 am by tytower » Logged

Governments should do what they were designed to do only.
Manage the larger issues best done by Government.
My Family and my Property are no

Brussels, Belgium
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Are you using other (non-official) libraries which work ?
Does those files exist in your sketchbook (at the same location) ?
Quote
libraries/RGBMood/RGBMood.h
libraries/RGBMood/RGBMood.cpp
libraries/RGBMood/examples/rainbow/rainbow.ino

Maybe rename your 'Libraries' folder in 'libraries'.
In the Arduino IDE, can you click "Sketch" -> "Importe Library", does "RGBMood" appear ?

Pollop.
Logged

Cairns - Green Island for Winter
Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you
I am Rusty
changing "Library" to "library" fixed it and a few others too !
Logged

Governments should do what they were designed to do only.
Manage the larger issues best done by Government.
My Family and my Property are no

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

LucidTronix has a RGB HSB library and a tutorial with sample code.  It has a bit packing function which is helpful for driving colors onto  16-bit screens and works just fine for RGB LEDs too.   Check it out at http://www.lucidtronix.com/tutorials/19
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lucidTronix code looks good too

however they have serial.print statements in a convertion functions and I think these should not be in there.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's really a absolute useable Library.

One feature i think is missing:

The ready-to-use Controller for RGB Stripes have a brightness control feature where you are able to choose about 5-10 different brightness steps.

Would that be easy to adapt or not?

Thanks Thomas
Logged

Ambert - 63
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot for this library. This is exacly what I was looking for.

I use it with 6 differents sets of power RGB leds and your effects are very nice!

For information, my RGB leds are not plugged on pins that provide PWM (pins 2 to 13 on my mega 2560).
So I had to include in your library an other library to make PWM using software (softPWM library http://code.google.com/p/rogue-code/wiki/SoftPWMLibraryDocumentation). I also had to change in your library the "anlogWrite" by "SoftPWMSet".
Logged

USA, FL
Offline Offline
God Member
*****
Karma: 14
Posts: 688
A life? Where can I download one of those?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In your example of "random,"  you're missing a semicolon on line 14.

Code:
m.setHSB(random(359), 255, 255);
« Last Edit: March 03, 2013, 07:38:09 pm by codlink » Logged

//LiNK

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for sharing that great library! Just what I was looking for... and more!

One thing... could setHoldingTime also work in HSB mode!!!



Code:
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
#include <RGBMood.h>
RGBMood m(redPin, greenPin, bluePin);
int step = 0;
int length = 4;
int choosenColorsHSB[] = {0, 90, 180, 270}; 

void setup() {
  m.setHoldingTime(10000);                                  // Keep the same color for 10 seconds before fading again. ***Not working here***
  m.setFadingSteps(200);                                     // Fade with 200 steps.
  m.setFadingSpeed(20);                                     // Each step last 20ms.
  length = (sizeof(choosenColorsHSB)/sizeof(int));   //length automatically adjust to the number of colors chosen
}

void loop() {
  if (not m.isFading()) {
    m.fadeHSB( choosenColorsHSB[step], 255, 255);
    step = step + 1;
    if (step >= length) step = 0;
    }
     
  m.tick();
}
Logged

Pages: [1] 2   Go Up
Jump to: