stichl.at tech at work

23Mar/130

Arduino moodlight

I needed to create a moodlight which fades between blue, green, cyan and white - a perfect job for an Arduino board I had leftover from another project 🙂

So I soldered the common anode RGB-Led to a little piece of PCB and added a 4-header.

After some research I found a really useful arduino library which is perfect for PWM-fading on any I/O pin.

Just look here: https://code.google.com/p/rogue-code/wiki/SoftPWMLibraryDocumentation

     

The sourcecode is considerably short and easy to write:

#include <SoftPWM.h>

#define PIN_G 2
#define PIN_R 3
#define PIN_B 4
#define PIN_CA 5
#define T_WAIT 10000

void setup()
{
 // Setting the common anode
 pinMode(PIN_CA,OUTPUT);
 digitalWrite(PIN_CA,1);

// Init Soft-PWM
 SoftPWMBegin(SOFTPWM_INVERTED);
 rgbPWMset(0,0,0); // black
 SoftPWMSetFadeTime(ALL, 4000, 4000);
 SoftPWMSetFadeTime(PIN_R, 4000, 3000); // because Red was too dominant when fading out
}

void loop()
{
 rgbPWMset(0,0,255); // blue
 delay(T_WAIT);

 rgbPWMset(0,255,255); // cyan
 delay(T_WAIT);

 rgbPWMset(0,255,0); // green
 delay(T_WAIT);

 rgbPWMset(255,255,255); // white
 delay(T_WAIT);
}

void rgbPWMset(int r, int g, int b)
{
 SoftPWMSet(PIN_G, g);
 SoftPWMSet(PIN_R, r);
 SoftPWMSet(PIN_B, b);
}

An example Implementation looks like this: (sorry but flickering is because of the camera...)

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.