To continue this series on driving RGB strips with a JeeNode, here is a sketch which allows setting the brightness levels using PWM. It’s a bit long for a weblog post, but I thought it’d be useful, since there is quite a bit of trickery in here. Notes follow below:
This code does some hefty bit manipulation. The idea is to keep an array of 256 time “slots”, with bits to indicate when an I/O pin should be toggled on or off. The loop then continuously scans these slots at the rate of ≈ 32 microseconds per slot (this corresponds to roughly 120 Hz). The 8 bits in each slot map to the JeeNode’s I/O pins: bits 0..3 = AIO1 .. AIO4 and 4..7 = DIO1 .. DIO4.
A refinement is that only the first 255 of the 256 slots are scanned. This way, 0 is 100% off and 255 is 100% on.
The static “masks” array defines which setting gets mapped to which I/O pin. It depends on the way the output driver is connected to the JeeNode.
The main PWM timing loop is done fully in software. It will run slightly irregularly due to timer interrupts and RF12 driver interrupts, but the effects aren’t noticeable.
The RGBW values are stored in EEPROM, so that the LEDs come back on with the same settings after power cycling. The settings can be adjusted by sending a packet with the new values to node 30, group 5 @ 868 MHz.
This sketch could be extended to support “animations”, i.e. ramping up/down to specific levels, mood lights, etc – I’m not interested in that, I just want to be able to trim the color of my room lighting to a pleasant level of white:
All I did to adjust the strip on the right was send the following “RGBW” command out via a JeeLink:
255,140,40,120,30s
The above sketch does PWM, but this whole thing is still being turned on and off with an old-fashioned mechanical switch … if you still remember those :)
FWIW, I’m considering making a dual-channel “MOSFET Plug” – two of those could then be used to replicate this same setup for the LED strip on the left.
Tomorrow: a little GUI front end for all this.
Your room is starting to look like Club Jee!
Do you have any details on the RGB strip you used, I’ve always fancied having such control over white balance without having to resort to coloured gels.
Why didn’t you use the Dimmer Plug in combination with the MOSFET’s? This way you could drive 5 RGB strips if I understand correctly.
I would prefer the Dimmer Plug, since this one off-loads the PWM thingy to the Dimmer Plug and keeps the JeeNode free for doing lots of other things…
On the other hand: combining this with a MOSFET plug would even be more interesting as in that case you have a dedicated working product ;-)
Good point! Would you believe that I simply forgot about the Dimmer Plug? Besides, I always wanted to try out some software PWM, so this was perhaps more an excuse to do it this way.
Hm. I could easily insert a Display Plug in there, even after the fact…
Always glad to help the author ;-)
But Display Plug? Did you mean Dimmer Plug by that, or are you thinking of the next extension to the RGB LED series??
Coincidence that the Dimmer Plug can drive 5 RGB LED strips and you did have some case with a 5-way button & LCD display sitting on it??
Yes, I meant Dimmer Plug. Hey, nice idea for the Mystery box one day – I’ll keep it in mind!