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);
SPI1.send4bit([255,0,0], 0b0001, 0b0011);
SPI1.send4bit([0,255,0], 0b0001, 0b0011); // green
SPI1.send4bit([0,0,255], 0b0001, 0b0011); // blue
SPI1.send4bit([255,255,255], 0b0001, 0b0011); // white
function getPattern() {
var cols = [];
for (var i=0;i<50;i++) {
cols.push(i*5);
cols.push(i*5);
cols.push(i*5);
}
return cols;
}
function doLights() {
SPI1.send4bit(getPattern(), 0b0001, 0b0011);
}
doLights();
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:
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);