• On an MDBT42, I'm using analogRead() in a function, called from a main function, called by onInit().
    The analogRead() works just once on the first run and the value is never updated.
    I know the pin voltage changes because it is correctly returned when I do a command line analogRead(D4) while the program is running.
    Any ideas why this might be? I am tending towards a problem with how I’m using onInit(), but even so I am a bit lost…

    Here is the code:

    function readBatt(pin) {
      v = analogRead(pin)*6.6;  // ADC * 3.3 * 2 (for Vbatt/2 divider)
      setLed();
    }
    
    function setLed() {
      if(v < 3) {
        clearInterval();
        setInterval("digitalPulse(D1, 0, [200,50,100,50]);", 10000);
      } else {
        clearInterval();
        setInterval("digitalPulse(D1, 0, [200,50]);", 10000);
      }
    }
    
    function onInit() {
      var v = 0;
      setInterval(function() {
        readBatt(D4);
      }, 10000);
    }
    

    What it does (meant to do) is flash the led twice at intervals if the voltage on D4 falls below 3, otherwise flash the led once at intervals. It's a low battery warning notification I want to add into other programs later. The led flashing functions perform as expected.
    The main loop (at onInit()) is meant to call the routine and update every 10s. The code is saved to flash from the webIDE.

    Thanks in advance for comments...

About

Avatar for docmathoc @docmathoc started