https://www.youtube.com/watch?v=NBf4JKflnyA
var distance = 0; var running; var on = 1; var global = [0,0,0,0,0,0,0,0,0,0,0,150,1,0]; var sensor = require("HC-SR04").connect(A0,A1,function(dist) { distance = dist; }); SPI2.setup({baud:3200000, mosi:B15}); function runIt(){ sensor.trigger(); if (distance > 9.5){ noMail(); }else{ gotMail(); } } function switchit(){ if (on){ clearInterval(running); on = 0; } else { global[11] = Math.floor((Math.random() * 100) + 50); running = setInterval(runIt, global[11]); on = 1; } } function gotMail(){ if(global[1] > 255){ global[1] = 0; } if(global[2] > 255){ global[2] = 0; } if(global[4] > 50){ global[4] = 11; } switch(global[4]) { case 0: blackOut(); break; case 1: SPI2.send4bit([0,0,0, 0,0,0, 0,0,0, 0,0,0, 50,0,0, 255,255,255], 0b0001, 0b0011); break; case 2: SPI2.send4bit([0,0,0, 0,0,0, 0,0,0, 50,0,0, 100,0,0, 255,255,255], 0b0001, 0b0011); break; case 3: SPI2.send4bit([0,0,0, 0,0,0, 50,0,0, 100,0,0, 155,50,0, 255,255,255], 0b0001, 0b0011); break; case 4: SPI2.send4bit([0,0,0, 50,0,0, 100,0,0, 155,50,0, 255,150,0, 255,255,255], 0b0001, 0b0011); break; case 5: SPI2.send4bit([50,0,0, 100,0,0, 55,50,0, 255,150,0, 255,255,255, 255,255,255], 0b0001, 0b0011); break; case 6: SPI2.send4bit([100,0,0, 55,50,0, 255,255,255, 255,255,255, 255,255,255, 255,255,255], 0b0001, 0b0011); break; case 7: SPI2.send4bit([255,255,255, 255,255,255, 255,255,255, 255,255,255, 255,255,255, 255,255,255], 0b0001, 0b0011); break; case 8: SPI2.send4bit([255,255,255, 255,255,255, 255,255,255, 255,255,255, 255,255,255, 255,255,255], 0b0001, 0b0011); break; case 9: SPI2.send4bit([255,255,255, 255,255,255, 255,255,255, 255,255,255, 255,255,255, 255,255,255], 0b0001, 0b0011); break; case 10: blackOut(); break; case 11: SPI2.send4bit([2,0,0, 2,0,0, 2,0,0, 2,0,0, 2,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 12: SPI2.send4bit([4,0,0, 4,0,0, 4,0,0, 4,0,0, 4,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 13: SPI2.send4bit([6,0,0, 6,0,0, 6,0,0, 6,0,0, 6,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 14: SPI2.send4bit([8,0,0, 8,0,0, 8,0,0, 8,0,0, 8,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 15: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 16: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 17: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 18: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 19: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 20: SPI2.send4bit([30,0,0, 20,0,0, 20,0,0, 20,0,0, 20,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 21: SPI2.send4bit([155,50,0, 30,0,0, 20,0,0, 20,0,0, 20,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 22: SPI2.send4bit([30,0,0, 155,50,0, 30,0,0, 20,0,0, 20,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 23: SPI2.send4bit([20,0,0, 30,0,0, 155,50,0, 30,0,0, 20,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 24: SPI2.send4bit([20,0,0, 20,0,0, 30,0,0, 155,50,0, 30,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 25: SPI2.send4bit([20,0,0, 20,0,0, 20,0,0, 30,0,0, 155,50,0, 0,255,0], 0b0001, 0b0011); // white break; case 26: SPI2.send4bit([20,0,0, 20,0,0, 20,0,0, 20,0,0, 30,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 27: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 28: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 29: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 30: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 31: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 32: SPI2.send4bit([30,0,0, 20,0,0, 20,0,0, 20,0,0, 20,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 33: SPI2.send4bit([155,50,0, 30,0,0, 20,0,0, 20,0,0, 20,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 34: SPI2.send4bit([30,0,0, 155,50,0, 30,0,0, 20,0,0, 20,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 35: SPI2.send4bit([20,0,0, 30,0,0, 155,50,0, 30,0,0, 20,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 36: SPI2.send4bit([20,0,0, 20,0,0, 30,0,0, 155,50,0, 30,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 37: SPI2.send4bit([20,0,0, 20,0,0, 20,0,0, 30,0,0, 155,50,0, 0,255,0], 0b0001, 0b0011); // white break; case 38: SPI2.send4bit([20,0,0, 20,0,0, 20,0,0, 20,0,0, 30,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 39: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 40: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 41: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 42: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 43: SPI2.send4bit([10,0,0, 10,0,0, 10,0,0, 10,0,0, 10,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 44: SPI2.send4bit([50,0,0, 100,0,0, 150,0,0, 200,0,0, 50,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 45: SPI2.send4bit([155,50,0, 100,0,0, 150,0,0, 200,0,0, 50,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 46: SPI2.send4bit([50,0,0, 155,50,0, 150,0,0, 200,0,0, 50,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 47: SPI2.send4bit([50,0,0, 100,0,0, 155,50,0, 200,0,0, 50,0,0, 0,255,0], 0b0001, 0b0011); // white break; case 48: SPI2.send4bit([50,0,0, 100,0,0, 150,0,0, 155,50,0, 50,0,0, 255,255,255], 0b0001, 0b0011); // white break; case 49: SPI2.send4bit([50,0,0, 100,0,0, 150,0,0, 200,0,0, 155,50,0, 0,255,0], 0b0001, 0b0011); // white break; case 50: SPI2.send4bit([50,0,0, 100,0,0, 150,0,0, 200,0,0, 50,0,0, 255,255,255], 0b0001, 0b0011); // white break; default: SPI2.send4bit([50,0,0, 100,0,0, 150,0,0, 200,0,0, 50,0,0, 0,255,0], 0b0001, 0b0011); // white } global[1] += 5; global[2] += 2; global[4] += 1; } function noMail(){ if(global[3] > 5){ global[3] = 0; } if (global[4] !== 0){ switchit(); global[4] = 0; switchit(); } if(global[1] > 255){ global[8] = 0; global[1] = 255; } if(global[1] < 0){ global[8] = 1; global[1] = 0; } if(global[5] > 255){ global[6] = 0; global[5] = 255; } if(global[5] < 0){ global[6] = 1; global[5] = 0; } if(global[2] > 255){ global[7] = 0; global[2] = 255; } if(global[2] < 0){ global[7] = 1; global[2] = 0; } if(global[9] > 100){ global[10] = 0; global[9] = 100; } if(global[9] < 0){ global[10] = 1; global[9] = 0; } if(global[13] > 1000){ global[13] = 0; if (global[12] === 0){ global[12] = 1; } else { global[12] = 0; } } if (global[12] === 0){ switch(global[3]) { case 0: SPI2.send4bit([global[2],global[1],global[5], 0,0,global[9], 0,0,global[1], 0,0,global[9], 0,0,global[1], global[1],0,0], 0b0001, 0b0011); break; case 1: SPI2.send4bit([25,25,global[1], global[2],global[1],global[5], 0,0,global[1], 0,0,global[9], 0,0,global[1], global[1],0,0], 0b0001, 0b0011); break; case 2: SPI2.send4bit([0,0,global[1], 25,25,global[9], global[2],global[1],global[5], 0,0,global[1], 0,0,global[1], global[1],0,0], 0b0001, 0b0011); break; case 3: SPI2.send4bit([0,0,global[1], 0,0,global[9], 25,25,50, global[2],global[1],global[5], 0,0,global[1], global[1],0,0], 0b0001, 0b0011); break; case 4: SPI2.send4bit([0,0,global[1], 0,0,global[9], 0,0,global[1], 25,25,global[9], global[2],global[1],global[5], global[1],0,0], 0b0001, 0b0011); break; default: SPI2.send4bit([0,0,global[1], 0,0,global[9], 0,0,global[1], 0,0,global[9], 25,25,global[1], global[1],0,0], 0b0001, 0b0011); } global[3] += 1; } else { SPI2.send4bit([global[1],global[5],global[9],global[5],global[2],global[1],global[5],global[1],global[2],global[5],global[2],global[1],global[1],global[5],global[9],global[9],0,0], 0b0001, 0b0011); } global[13] += 1; if (global[6] == 1){ global[5] += 1; } else{ global[5] -= 1; } if (global[7] == 1){ global[2] += 2; } else { global[2] -= 2; } if (global[8] == 1){ global[1] += 5; } else { global[1] -= 5; } if (global[10] == 1){ global[9] += 10; } else { global[9] -= 10; } } function blackOut(){SPI2.send4bit([0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0], 0b0001, 0b0011);} running = setInterval(runIt, 200); setWatch(switchit, BTN1, {repeat:true, edge:"rising"});
@Rek started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
https://www.youtube.com/watch?v=NBf4JKflnyA