Avatar for nglod

nglod

Member since Jan 2014 • Last active Jan 2014
  • 1 conversations
  • 1 comments

Most recent activity

  • in JavaScript
    Avatar for nglod

    Hello,

    I just received my espruinos and started typing a few examples.
    I have upgraded the firmware as explained in the quick start guide and I am following this tutorial: http://www.espruino.com/Flashing+Lights
    The code under "try 4" is not working as expected:

    Pin.prototype.startFlashing = function(period) { 
      var on = false;
      setInterval(function() {
        on = !on;
        digitalWrite(this, on);
      }, period);
    }
    LED1.startFlashing(100);
    

    On the console I can read "ERROR: Invalid pin!". There seems to be a problem handling "this", in fact the issue seems to be the same as in this other forum post: http://forum.espruino.com/conversations/­389/

    The code works fine with the following modification:

    Pin.prototype.startFlashing = function(period) {
      var me = this;
      var on = false;
      setInterval(function() {
        on = !on;
        digitalWrite(me, on);
      }, period);
    }
    
    LED1.startFlashing(100);
    

    Just letting you know, I don't know this should be considered a bug in the code or in the documentation.

    Thanks a lot!

Actions