you have competing/overlapping intervals / timeouts...
...give this a shot...
// show numbers from 1 through 109 with red and green flashes:
// a red flash for each tens, and a green for each ones;
// example: flashLEDs(21); // flash red twice and green once
// Note: you cannot call it more often then about every 10 seconds
function flashLEDs(intOrLED, flashesAsStr) {
if (!flashesAsStr) {
flashesAsStr = (
"RRRRRRRRRR".substr(0, Math.floor(intOrLED / 10))
+ "ggggggggg".substr(0, intOrLED % 10) );
intOrLED = null; }
if (intOrLED) {
intOrLED.reset();
if (flashesAsStr.length > 1) {
setTimeout(flashLEDs
, (flashesAsStr.charAt(0) === flashesAsStr.charAt(1)) ? 270 : 420
, null, flashesAsStr.substr(1)); } // off
} else {
if (flashesAsStr.length > 0) {
(intOrLED = (flashesAsStr.charAt(0) === "R") ? LED1 : LED2).set();
setTimeout(flashLEDs, 30, intOrLED, flashesAsStr); } // on
}
}
// read temperature and display farenheit with flashes
function showTemp() {
flashLEDs(Math.round(E.getTemperature() * 9 / 5 + 32));
}
// enter r() in console r() to run
var runningInterv = null;
function r() { // run
if (!runningInterv) {
showTemp();
runningInterv = setInterval(showTemp, 10000); }
}
// enter s() in console to stop
function s() { // stop
if (runningInterv) {
runningInterv = clearInterval(runningInterv);
}
}
// start automatically after saving the code
function onInit() {
r();
}
...and this flashLEDs() works even better... I mean, is more frugal:
// show integers from 1 through 109 with red and green flashes:
// a red flash for each tens, and a green for each ones;
// example: flashLEDs(21); // flash red twice and green once
// Note: you cannot call it more often then about every 10 seconds
function flashLEDs(i, led) {
if (led) {
led.reset();
if (i > 1) {
setTimeout( flashLEDs
, ((20 > i) && (i > 9)) ? 470 : 270
, i - ((i > 9) ? 10 : 1) ); } // off
} else {
if (i > 0) {
(led = (i > 9) ? LED1 : LED2).set();
setTimeout(flashLEDs, 30, i, led); // on
}
}
}
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.
you have competing/overlapping intervals / timeouts...
...give this a shot...
...and this
flashLEDs()
works even better... I mean, is more frugal: