Instructables

Light Sensing Fairy Lights

video Light Sensing Fairy Lights
This is a set of lights that smoothly changes colour and pattern depending on the amount of light in the room.

Apologies for the video - you'll need to view it in HD, and even then you may not be able top make out all of the code.

You'll need:
I connected:
  • The white+red wires of the WS2811s to 0v and 5v
  • The green wire of the LDR to pin PA7
  • The LDR between ground and pin PC1
  • A 200kOhm resistor between PC3 and PC1

There's more information on controlling and wiring up the lights on the Espruino tutorial for WS2811s. The actual code you need to copy and paste in is:
function onInit() {
  SPI1.setup({baud:3200000, mosi:A7});
  C3.set(); // Pull the light sensor's potential divider up to 3.3v
}
onInit();

var light = 0.0; // an average

function getPattern() {   
  var lightInstant = analogRead(C1)*3;
  light = lightInstant*0.1 + light*0.9;
  var cols = [];
  for (var i=0;i<50;i++) {
     var c = (-Math.abs(i-25)*10) + light*1024 - 200;
     if (c<0) c=0;
     if (c>255) c=255;
     cols.push(c);
     c = (-Math.abs(i-25)*10) + light*1024 - 450;
     if (c<0) c=0;
     if (c>255) c=255;
     cols.push(c);
     c = (-Math.abs(i-25)*10) + light*1024 - 600;
     if (c<0) c=0;
     if (c>255) c=255;
     cols.push(c);
  }
  return cols;
}

function doLights() {   
  SPI1.send4bit(getPattern(), 0b0001, 0b0011);
}
setInterval(doLights, 50);
And job done! If you type 'save()' it'll keep working even after power off.