You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • code is working but after setTimeout time I see both "waiting" and "CO2 measured..." commands - would like to leave only readings after 5min

    That's the way the console works... and it makes sense, becuase console is a shared resource,... and each log should be 'atomic' - a transaction. You can use print() and println(). The challenge then becomes to prevent something creating output while you are waiting for the measurement and messing it up.

    Therefore, in an event driven environment and a (single) log, you want to be each log entry / output to be as complete / comprehensive as possible. To make it more clear, I usually do it this way:

    var co2Value = 0; // globally defined for pickup by Espruino testing feature
    function co2MeasureAndLog() {
      // your measuring and calculation assigning result to co2Value
      console.log("CO2: measured: " + co2Value + " [ppm]");
    function co2Cycle(){
      console.log("CO2: waiting...");
        console.log("CO2: waiting... Done.");
      },300000); // 300'000[ms] = 5 [min]

    It creates one more line of output, but is it is very clear what is going on.

    I'm sure hat you may have different display arrangement for your final project where there is a spot reserved for every value to display... as well as an indicator (whether the particular) measuring loop is active. With dedicated spots for each measured value there is no issue. Furthermore with this kind of information it is useful to also put the time (stamp) with the measured result to give feedback about the actuality of the value.

    I suggest that you look at Espruinos Testing feature built by @JumJum - it will show you the values in a nice graph. Even though the feature it is still declared as Beta, it is fulluy functional... and there is a sequence of youtube videos
    that shows how to use it. In a nutshell:

    1. Write your program just as you did (but) with your result(s) globally accessible (see above)
    2. Run your program
    3. Switch from console to testing (click </> icon at bottom in left border of console)
    4. Add the global variable name co2Value to the list of values graphed
    5. Run your testing

    By default, testing samples / pulls the values every second. To change that, go into the settings and pick the interval suitable for your application. When everything is setup the way you want, you can save the testing with and later reload and run again. There are even more nice opitons that allow you to load the program with the testing.

    1 Attachment

    • graphing.png

Avatar for allObjects @allObjects started