• When I try to change a pin's mode inside a setInterval after a number of loops Espruino hangs.
    For example:

    var count = 0;
    
    const onInit = () => {
      var loop = setInterval(() => {
        if (count === 1000) {
          digitalWrite(1, 0);
        }
        console.log('foo', count);
        count = count + 1;
      }, 5);
      
      setTimeout(function() {
        clearInterval(loop);
        console.log("foobar");
      }, 10000);
    };
    
    setTimeout(onInit, 1000);
    

    It will turn the pin off and log foo 999. It will never log foobar.

    The odd thing is that I can create a blink loop with setInterval like this:

    var state = 0;
    
    const onInit = () => {
      var loop = setInterval(() => {
        digitalWrite(1, state);
        console.log('foo', state);
        state = !state;
      }, 500);
    };
    
    setTimeout(onInit, 1000);
    

    Any help would be great!

  • Well... This seems to be an issue with pin 1 on the ESP8266?

  • The first code example surprise me a bit by what it should do...

    For ten seconds, every five (5) milliseconds it logs "foo ". On the 1000th time, something after five (5) seconds, it turns the pin on.

    The hanging to me means that something with the buffers goes hi wire, because of all the logging...

    What is the reason for the loop? ...are you trying to reinstate Arduino on an event driven system?

    The second piece of code does it 100 times slower: every 500 milliseconds, it does log something... and the rest of the time it is sleeping. Less than a percent - probably 1/2 a percent as busy as the first code...

    Could it be that the system crashes and reboots?... I would be surprised...

    PS: ...not Espruino hangs, but ESP8266 Wifi stack puts an end to this...?

  • It's pin 1. Check out: http://www.espruino.com/EspruinoESP8266#­pinout

    Pin 1 is D1 - which is USART1_TX. That's the pin that's used by Espruino to output the REPL.

    So if you write to it, it stops Espruino being able to print anything to the REPL any more. It's still working, it's just not printing anything.

  • ooops... checking the pin vs serial would have helped... ;/

    @Gordon, w/ your input, I conclude that second example is either not working or tricks the observer that it works only half the times, is it?

    Adding a counter and log its value would - when working - only log every other time?

  • Honestly, I'm not sure why the second would appear to be ok - I'd have thought it would cause the same problems.

  • Thanks! That is what I found out through trial and error. I forgot to look at what else that pin did.

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

Changing a pin inside a setInterval causes Espruino to hang

Posted by Avatar for calebbrewer @calebbrewer

Actions