• 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
    
  • 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
        }
      }
    }
    
  • 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

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Show Temperature - Why code Not Stable for extended period of time?

Posted by Avatar for user71302 @user71302

Actions