Skip to content

Instantly share code, notes, and snippets.

@nielsdoorn
Last active January 4, 2016 13:59
Show Gist options
  • Save nielsdoorn/8631319 to your computer and use it in GitHub Desktop.
Save nielsdoorn/8631319 to your computer and use it in GitHub Desktop.
Espruino fade in and out script with light string (individual addressable lights)
// change this if you use another port or if your lights work with 32 instead of 64 baud
SPI1.setup({baud:6400000, mosi:A7});
var rgb = new Uint8Array(25*3); // 25 x RGB lights
// turn off board leds
digitalWrite(LED1,0);
digitalWrite(LED2,0);
digitalWrite(LED3,0);
var numOfLights = 25;
// turn them all off
function off() {
var n = 0;
for (var i=0;i<numOfLights;i++) {
rgb[n++] = 0;
rgb[n++] = 0;
rgb[n++] = 0;
}
doLights();
}
// slowly fade in and out different random color each time
var direction = -1;
var pos = 0;
var r,g,b;
function flow() {
if (pos > 255 || pos < 1) {
direction *= -1;
}
if (pos < 1) {
r = Math.floor(Math.random() * 255);
g = Math.floor(Math.random() * 255);
b = Math.floor(Math.random() * 255);
console.log(r,g,b,direction,pos);
}
var n = 0;
for (var i = 0; i < numOfLights; i++) {
rgb[n++] = Math.ceil((Math.sin(pos/255)*r));
rgb[n++] = Math.ceil((Math.sin(pos/255)*g));
rgb[n++] = Math.ceil((Math.sin(pos/255)*b));
}
pos += direction;
doLights();
}
function doLights() {
SPI1.send4bit(rgb, 0b0001, 0b0011);
}
off();
setInterval(flow, 20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment