Flashing lights Try 4 not working

Posted on
  • I am following the example in Try 4. I entered

    Pin.prototype.startFlashing = function(period) {
    if (Pin.intervals===undefined) Pin.intervals = [];
    if (Pin.intervals[this]) clearInterval(Pin.intervals[this]);
      var on = false;
      var pin = this;
      Pin.intervals[this] = setInterval(function() {
      on = !on;
      digitalWrite(pin, on);
    }, period);
    };
    

    But, when I run "LED1.startFlashing(1000);" in the terminal, I get "ERROR: Invalid pin!"
    "console.log(pin);" prints "=undefined" so something is very wrong.
    However, when I run "var light = LED1;" and then "light.startFlashing(1000);" I get the expected behavior.
    What is going on here? I am a Java developer and don't use JS that much, so I may be misunderstanding something simple.

  • Hi. It seems there's a bug in the interpreter that causes this to get reset...

    It must have crept in recently because that code did work when I wrote it. I've made an issue for it and I'll try and fix it today I hope: https://github.com/espruino/Espruino/iss­ues/233

    In the mean time, a simple change of order will help to fix it:

    Pin.prototype.startFlashing = function(period) {
      console.log(this);
      var pin = this;
      if (Pin.intervals===undefined) Pin.intervals = [];
      if (Pin.intervals[pin]) clearInterval(Pin.intervals[pin]);
      var on = false; 
      Pin.intervals[pin] = setInterval(function() {
        on = !on;
        digitalWrite(pin, on);
      }, period);
    };
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Flashing lights Try 4 not working

Posted by Avatar for NightlyNexus @NightlyNexus

Actions