• Hi, found some intermediate version of the blockly code and looked at it: in deed, it goes out of memory - for me after a while only - for multiple reasons (confirmed by the inserted console.log() statements to figure out what's going on):

    • default for repeat-option in blockly is set to true - my code expects to have it false (set to true, it adds another watch everytime it runs through the else path, calls itself, and cascades, and... in goes 'Out of memory' with a bouncy hand contact... (used C6 and a bread board wire to touch ground).
    • any setWatch() should have the debounce option set to catch jitters.... (...just a the mother ;-) of the china store...).

    With grabing what blockly uploads, then fixing it, gives the code that shold work perfectly for you (if not, just come back and give me the red card... :|):

    /* 
    Blockly uploaded code
    
    var soundOn;function onInit(){eval('pinMode(C6,"input_pullup"­);\npinMode(C7,"input_pulldown");\npinMo­de(A8,"input_pulldown");\npinMode(C9,"ou­tput");\nanalogWrite(C9,0);');soundOn=fa­lse;setWatch(function(){makeSound()},C6,­{"repeat":true,"edge":"falling"})} 
    
    function makeSound(){if(digitalRead(C7)||digitalR­ead(A8)){if(!soundOn){analogWrite(C9,.5)­;digitalWrite(LED1,1);soundOn=true}setTi­meout(function(){makeSound()},5*1E3)}els­e{if(soundOn){analogWrite(C9,0);digitalW­rite(LED1,0);soundOn=false}setWatch(func­tion(){makeSound()},C6,{"repeat":true,"e­dge":"falling"})}}; 
    
    
    Shows these issues on first C6 going low:
    
    ERROR: Out of Memory!
    WARNING: Out of memory while appending to array
    WARNING: Unable to create string as not enough memory
    at line 1 col 285
    ...eat":true,"edge":"falling"})}}
                                   ^
    in function "makeSound" called from line 1 col 12
    {makeSound()}
                ^
    in function called from system
    at line 1 col in function called from system
    at li> 
    
    */
    
    // *fixed* code - with some formatting to shorten lines
    
    var soundOn;
    
    function onInit() {
      eval( // broken up in concat'd strings for short lines
          'pinMode(C6,"input_pullup");\n'
        + 'pinMode(C7,"input_pulldown");\n'
        + 'pinMode(A8,"input_pulldown");\n'
        + 'pinMode(C9,"output");\n'
        + 'analogWrite(C9,0);'
      );
      soundOn = false;
      setWatch(function(){
          makeSound(); // 'fixed': added semicolon ;
        },C6,{repeat:false,edge:"falling",deboun­ce:100}); // 'fixed': added ; and ...
        // ...SET repeat OPTION TO false
    } 
    
    function makeSound() {
      if (digitalRead(C7) || digitalRead(A8)) {
        console.log("then - " + getTime());
        if (!soundOn) {
          analogWrite(C9,0.5); // 'fixed' warn'g: added leading 0
          digitalWrite(LED1,1);
          soundOn = true; // 'fixed': added ;
        }
        setTimeout(function(){
            makeSound(); // 'fixed': added ;
          },5*1E3); // 'fixed': added ;
      } else {
        console.log("else - " + getTime());
        if (soundOn) {
          analogWrite(C9,0);
          digitalWrite(LED1,0);
          soundOn = false; // 'fixed': added ;
        }
        setWatch(function(){
            makeSound(); // 'fixed': added ;
          },C6,{repeat:false,edge:"falling",deboun­ce:100}); // 'fixed': added ; and...
          // ...SET repeat OPTION TO false
       }
    } // 'fixed' warning: removed unnecessary semicolon ; 
    

    Btw, this eval("javascript source code string") is a very very bad thing... may be blockly has to do it (not really... since for espruinio passing just the source string would work too... (@Gordon?)), you can do it directly with:

    function onInit() {
      pinMode(C6,"input_pullup");
      pinMode(C7,"input_pulldown");
      pinMode(A8,"input_pulldown");
      pinMode(C9,"output");\n'
      analogWrite(C9,0);'
      soundOn = false;
      setWatch(function(){
          makeSound(); // 'fixed': added semicolon ;
        },C6,{repeat:false,edge:"falling",deboun­ce:100}); // 'fixed': added ;
    } 
    

    Furthermore, you can take the *pinMode()*s out of the onInit() and put it together with the declaration of the soundOn variable, because saving the code makes Espruino holding on to the pin configuration - is that right, @Gordon?

    var soundOn;
    pinMode(C6,"input_pullup");
    pinMode(C7,"input_pulldown");
    pinMode(A8,"input_pulldown");
    pinMode(C9,"output");
    
    function onInit() {
      analogWrite(C9,0);
      soundOn = false;
      setWatch(function(){
          makeSound(); // 'fixed': added semicolon ;
        },C6,{repeat:false,edge:"falling",deboun­ce:100}); 
    } 
    
About

Avatar for allObjects @allObjects started