-
• #2
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 } } }
-
• #3
Thanks. I knew it was probably because of my code as I'm new to JavaScript and only able to access my Puck.js with my Android Phone. Tough writing code on a small phone screen. Was very surprised that my Mac didn't have BLE and my iPad either. Still trying to get operational on my a Linux BBB but saw other were using on PIE so should be possible.
Thanks for sharing your code and showing me other ways to accomplish the display of temperature. I also wanted to be able to handle three digits and possible use the amber led. I want to expand this project to monitor and set alarm thresholds but for now the display is a great start to learn JavaScript and Puck.js.
Again Thanks,
John
I'm sure there are better ways to do this but I wanted to visually show the temperature on the Puck.js.
I setup to flash lights for temperature digits. First digit flash Red LED number of times to indicate Ten's, and flash Green LED number of time to represent temperature units digit.
Why does the following code fail after running for a period of time?
Lights go crazy and doesn't represent temperature