Different output pico/esp8266

Posted on
  • With the same code, I have different output on pico and esp8266.

    Which one is correct?
    I think it is esp8266, but I may be wrong.

    esp8266
     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v85.1127 Copyright 2016 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 512KB:256/256, manuf 0xc8 chip 0x4013
    >echo(0);
    oldtime  1462372910.64224791526
    newtime  1462372911.01720309257
    diftime  0.37495517730
    =undefined
    >
    
     
    pico
     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v85 Copyright 2016 G.Williams
    >echo(0);
    oldtime  1462373548.05334663391
    newtime  undefined
    Uncaught ReferenceError: "newtime" is not defined
     at line 1 col 25
    console.log('newtime ', newtime);
                            ^
    diftime  NaN
    Uncaught ReferenceError: "newtime" is not defined
     at line 1 col 33
    console.log('diftime ', newtime-oldtime);
                                    ^
    =undefined
    >
    
    

    code:

    oldtime = getTime();
    setTimeout(function() {
      newtime = getTime();
    },160);
    
    console.log('oldtime ', oldtime);
    console.log('newtime ', newtime);
    console.log('diftime ', newtime-oldtime);
    
    
  • The Pico is correct :)

    On the Pico, the code executes quick enough that console.log('newtime ', newtime); is executed before the timeout created with setTimeout executes, so newtime doesn't get assigned before it is used.

    If you want this to actually work, do:

    oldtime = getTime();
    setTimeout(function() {
      newtime = getTime();
      console.log('oldtime ', oldtime);
      console.log('newtime ', newtime);
      console.log('diftime ', newtime-oldtime);
    },160);
    
  • Just to add - the ESP8266 isn't exactly wrong in this case - it's just that I imagine the upload takes longer over serial, and things won't be scheduled quite as well because you're reliant on the underlying RTOS.

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

Different output pico/esp8266

Posted by Avatar for Frida @Frida

Actions