Halloween Lightbox


Using the Dimmer Plug and a Jeenode to create a box of LEDs that blinks when tilted, with a brightness control (and future wireless capability…)

Check the PCA9635 data sheet for programming details.

Took me a bit to figure out how to vary the brightness of each LED individually, and to turn them on in the first place (have to send the right message to each LED bus).

Yeah, it’s a bit dodgy looking. Just needed to whip up a quick box out of foam board, will do a nice laser-cut encased version at some point, time-permitting.

Code for Arduino:


// Dimmer with accelerometer attached to port 4 analog in and
// standard potentiometer attached to port 3.
// 2010-10-30 <evan@openlabworkshops.org> http://opensource.org/licenses/mit-license.php

#include <Ports.h>
#include <RF12.h> // needed to avoid a linker error :(

Port three (3);
Port four (4);
PortI2C myBus (1);

DimmerPlug dimmer (myBus, 0x40);

// variables:
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value
int level;
byte frame = 0;

// leds are connected to pins 2 (DIO) and 3 (GND) with a series resistor

void setup() {

 Serial.begin(57600);

 dimmer.setReg(DimmerPlug::MODE1, 0x00);     // normal
 dimmer.setReg(DimmerPlug::MODE2, 0x20);     // blink
 dimmer.setReg(DimmerPlug::GRPPWM, 0x40 );    // duty cycle 25%
 dimmer.setReg(DimmerPlug::GRPFREQ, 3);      // blink 4x per sec
 dimmer.setReg(DimmerPlug::LEDOUT0, 0xFF);   // LED 0 blinks (all 4 - 3 bits on for each)
 dimmer.setReg(DimmerPlug::LEDOUT1, 0x03);   // LED 1 blinks (only the 1st)
 dimmer.setReg(DimmerPlug::LEDOUT2, 0x00);   // LED 2 off
 dimmer.setReg(DimmerPlug::LEDOUT3, 0x00);   // LED 3 off

 dimmer.setReg(DimmerPlug::PWM0, 0xFF);  // initial brightness to full for each individual LED
 dimmer.setReg(DimmerPlug::PWM1, 0xFF);
 dimmer.setReg(DimmerPlug::PWM2, 0xFF);
 dimmer.setReg(DimmerPlug::PWM3, 0xFF);
 dimmer.setReg(DimmerPlug::PWM4, 0xFF);

 three.mode(INPUT);
 four.mode(INPUT);
}

void loop() {

 int val = four.anaRead();
 int val2 = three.anaRead();

 // calibrate during the first five seconds
 if (millis() < 6000) {

 // record the maximum sensor value
 if (val > sensorMax) {
 sensorMax = val;
 }

 // record the minimum sensor value
 if (val < sensorMin) {
 sensorMin = val;
 }
 }

 else
 {
 int sensorValue = map(val, sensorMin, sensorMax, 2, 18);

 //++level;
 ++frame;
 byte frameOdd = frame % 2;  // just add some "spice" to the blinking

 dimmer.setReg(DimmerPlug::PWM0, val2* frameOdd);
 dimmer.setReg(DimmerPlug::PWM1, val2);
 dimmer.setReg(DimmerPlug::PWM2, val2* frameOdd);
 dimmer.setReg(DimmerPlug::PWM3, val2);
 dimmer.setReg(DimmerPlug::PWM4, val2* frameOdd);

//     dimmer.setReg(DimmerPlug::GRPPWM, 0x30 );

 // vary the blink rate with the tilt sensor value (y direction, in this case)
 dimmer.setReg(DimmerPlug::GRPFREQ, sensorValue);

// delay - we don't need to do this all the time, the LED driver does most of the work
 delay(100);

 //Serial.print(sensorMin);
//    Serial.print(":");
//    Serial.print(sensorMax);
//    Serial.print(":");
//    Serial.println(sensorValue);
 }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.