• 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

    var on = false;
    var index = 0;
    var iterate = 0;
    
    var tempF = Math.round(E.getTemperature() * 9 / 5 + 32);
    var digits = tempF.toString().split('');
    iterate = parseInt(digits[index]) * 2;  // multiply by 2 for on and off LED state
    
    // toggle LED's on and off.  repetitions - twice the number of times you want to flash
    // first flashes Red LED, then flashes Green LED.
    // Red indicates Tens and Green indicates Units for Temperature Deg F
    // Count flashes indicates the Temperature.
    // Example:  7 Red Flashes and 2 Green Flashes indicates 72 Deg F
    var flashLEDs = function setIntervalX(callback, delay, repetitions) {
        var x = 0;
        var intervalID = setInterval(function () {
            on = !on;
            if(index === 0) {
             LED1.write(on);
            } else {
             LED2.write(on);
            }
            callback();
            // terminate when repetitions reached
           if (++x === repetitions) {
               LED1.write(false);
               LED2.write(false);
               clearInterval(intervalID);
               index++;
               // start up Green Units LED, iterate repetition per index of 1 from temperature digits
               if(index === 1) {
                  iterate = parseInt(digits[index]) * 2;
                  index = 999;  // make so doesn't process any additional digits
                  // TODO process n digits of temperature
                  setTimeout(showTemp, 60000);  // every minute
                  flashLEDs( function() {
                  }, 400, iterate);
               }
           }
        }, delay);
    };
    
    // showTemp gets called every minute
    var showTemp = function() {
    	index = 0;
    	tempF = Math.round(E.getTemperature() * 9/5 + 32);
    	digits = tempF.toString().split('');
    	iterate = parseInt(digits[index]) * 2;
    	flashLEDs( function() {
    	}, 400 iterate);
    }
    
    setTimeout(showTemp, 1000);  // start up after 1 second
    
About

Avatar for user71302 @user71302 started