Project 1 - Light-sensitive Lights

Note: At one point this was an entry in an Instructables contest.


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 to make out all of the code.
You'll need:
  • An STM32F4DISCOVERY board
  • A USB-TTL converter - this is not vital, without it the LEDs will flicker when USB is plugged in, but once you remove it they will be fine
  • A WS2811 LED string
  • A Light Dependent Resistor (LDR) and matching normal resistor (in my case 200k)
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 WS2811 page. 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

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;
     c = (-Math.abs(i-25)*10) + light*1024 - 450;
     if (c<0) c=0; 
     if (c>255) c=255;
     c = (-Math.abs(i-25)*10) + light*1024 - 600; 
     if (c<0) c=0; 
     if (c>255) c=255;
  return cols;

function doLights() {    
  SPI1.send4bit(getPattern(), 0b0001, 0b0011);

setInterval(doLights, 50);