I am using an Espruino to run a project with 3 buttons and an LED light strip. One of the buttons increases an led light position by flashing the next light on the strip. The other decreases an led light position by flashing the next light on the strip in an opposite direction, and the last button randomly illuminates 10 lights between the first light and the current position for a minute or two. Unless a button was recently pressed all lights should be off. The project is powered by 4x AA batteries.
And everything works great, except for some reason, the battery dies after an hour or two even if the lights are off. I'm guessing I'm failing to implement deep sleep properly. As far as I can guess I am not violating any of the 5 conditions in any way that deep sleep should not execute eventually.
So why is this happening? I've included my code below. Also deepest apologies for my shitty code. I'm not a coder or anything, I just made it work.
Goes without saying a simple on/off switch would solve the problem but I have one, and I keep forgetting to turn it off so I'd love a software solution.
var rgb = new Uint8ClampedArray(300*3);
var rando = new Uint8ClampedArray(10); //array to store 10 random numbers
var position = 200;
var k = 0;
var pos = 0;
//Add function
digitalWrite(B7,1);
pinMode(A8, "input_pulldown");
setWatch(function() {
doAdd();
}, A8, { repeat: true, debounce : 100, edge: "rising" });
//Subtract function
digitalWrite(B3,1);
pinMode(B3, "input_pulldown");
setWatch(function() {
doSub();
}, B3, { repeat: true, debounce : 100, edge: "rising" });
//Random function
digitalWrite(B4,1);
pinMode(B5, "input_pulldown");
setWatch(function() {
doRand();
}, B5, { repeat: true, debounce : 100, edge: "rising" });
function setPosition() {
pos = (position*3)+2;
rgb[pos] = 10;
}
function doAdd() {
position++;
doPosition();
setDeepSleep(1);
}
function doSub() {
if (position > 20) {
position--;
}
doPosition();
setDeepSleep(1);
}
function doPosition() {
setPosition();
require("neopixel").write(B15, rgb);
k = setTimeout (doOff,300);
}
function onInit() {
doOff();
}
function setOff() {
for (var i=0;i<rgb.length;i+=3) {
rgb[i ] = 0;
rgb[i+1] = 0;
rgb[i+2] = 0;
}
}
function doOff() {
setOff();
require("neopixel").write(B15, rgb);
setDeepSleep(1);
}
function setRand() {
setOff();
var colourio = 0;
for (var i=0;i<rando.length;i++) { //puts data into rando array
rando[i] = parseInt(Math.random()*(position+1)); //put a random number into one of the array positions
if (i>0){
for (var t=0; t<i; t++) { //checks to see if the LED has been selected randomly
if (rando[i] == rando[t]){
i=i-1;
}
}
}
}
rando.sort(function(a, b) {
return a - b;
});
console.log(rando);
console.log(position);
for (var w=0;w<rando.length;w++) {
pos = (rando[w]*3);
rgb[pos] = 10;
}
setDeepSleep(1);
}
function doRand() {
clearTimeout();
k = 0;
setRand();
require("neopixel").write(B15, rgb);
k = setTimeout (doOff,120000);
}
onInit();
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.
I am using an Espruino to run a project with 3 buttons and an LED light strip. One of the buttons increases an led light position by flashing the next light on the strip. The other decreases an led light position by flashing the next light on the strip in an opposite direction, and the last button randomly illuminates 10 lights between the first light and the current position for a minute or two. Unless a button was recently pressed all lights should be off. The project is powered by 4x AA batteries.
And everything works great, except for some reason, the battery dies after an hour or two even if the lights are off. I'm guessing I'm failing to implement deep sleep properly. As far as I can guess I am not violating any of the 5 conditions in any way that deep sleep should not execute eventually.
So why is this happening? I've included my code below. Also deepest apologies for my shitty code. I'm not a coder or anything, I just made it work.
Goes without saying a simple on/off switch would solve the problem but I have one, and I keep forgetting to turn it off so I'd love a software solution.