• It is starting to make sense. Thanks again!

    Currently, the Espruino IDE is unhappy with the word, "function," as a 'reserved word,' and is giving red flags there and at the 100ms specification. I shall read up on setInterval and tinker more...

    Here are some bold (untested) modifications for my project, in case anyone wishes to see:

    I2C1.setup({scl:B8,sda:B9});
    I2C2.setup({scl:B10,sda:B11});
    
    var lastKeyPressed = 0;
    var lastBankSelected = 0;
    
    var mpr1 = require("MPR121").connect(I2C1, {
      //device 1 (12-key musical keyboard) is ready
      setInterval(function() {
        var keys = mpr1.touched();
        if (keys!=lastKeyPressed){
          for (var i=0;i<12;i++){
            var kbit = 1<<i;
            if ((keys&kbit) && !(lastKeyPressed&kbit))
              //do something, for example:
              console.log("Key: " + i);
          }
        }
        lastKeyPressed = keys;
      }, 100);
    });
                                         
    var mpr2 = require("MPR121").connect(I2C2, {
      //device 2 (12-position rotary switch) is ready
      setInterval(function() {
        var banks = mpr2.touched();
        if (banks!=lastBankSelected){
          for (var j=0;j<12;j++){
            var bbit = 1<<j;
            if ((banks&bbit) && !(lastBankSelected&bbit))
              //do something, for example:
              console.log("Bank: " + j);
          }
        }
        lastBankSelected = banks;
      }, 100);
    });
    
  • @bertjerred,

    two (2) issues there:

    • the .connect() arguments are not as expected - function object is expected - and
    • malformed themselves - not a proper object, that's why the syntax complain: no object property can be named function, because of function being a reserved word.

    The second argument of .connect() has to be a function() object and the code passes a malformed object { setInterval(... }.

    Small change fixes this.

    I2C1.setup({scl:B8,sda:B9});
    I2C2.setup({scl:B10,sda:B11});
    var mpr1;
    var mpr2;
    var lastKeyPressed = 0;
    var lastBankSelected = 0;
    
    function keyDev() {
      // device 1 (12-key musical keyboard) is ready
      setInterval(function() {
        var keys = mpr1.touched();
        if (keys!=lastKeyPressed){
          for (var i=0;i<12;i++){
            var kbit = 1<<i;
            if ((keys&kbit) && !(lastKeyPressed&kbit))
              // do something, for example:
              console.log("Key: " + i);
          }
        }
        lastKeyPressed = keys;
      }, 100);
    };
    
    funciton bankDev() {
      // device 2 (12-position rotary switch) is ready
      setInterval(function() {
        var banks = mpr2.touched();
        if (banks!=lastBankSelected){
          for (var j=0;j<12;j++){
            var bbit = 1<<j;
            if ((banks&bbit) && !(lastBankSelected&bbit))
              // do something, for example:
              console.log("Bank: " + j);
          }
        }
        lastBankSelected = banks;
      }, 100);
    };
    
    function onInit() {
      mpr1 = require("MPR121").connect(I2C1, keyDev);
      mpr2 = require("MPR121").connect(I2C2, bankDev);
    }
    
    setTimeout(onInit,999); // for dev; comment before save()
    

    If you do not like the extra ('external', non-anonymous, named) keyDev(){...} and bankDev(){}...} functions, you can define them inline as anonymous, just like in the setInterval(function(){...},...); function:

    I2C1.setup({scl:B8,sda:B9});
    I2C2.setup({scl:B10,sda:B11});
    var mpr1;
    var mpr2;
    var lastKeyPressed = 0;
    var lastBankSelected = 0;
    
    function onInit() {
      mpr1 = require("MPR121").connect(I2C1, function(){
        //device 1 (12-key musical keyboard) is ready
        setInterval(function() {
          var keys = mpr1.touched();
          if (keys!=lastKeyPressed){
            for (var i=0;i<12;i++){
              var kbit = 1<<i;
              if ((keys&kbit) && !(lastKeyPressed&kbit))
                //do something, for example:
                console.log("Key: " + i);
            }
          }
          lastKeyPressed = keys;
        }, 100);
      });
    
      mpr2 = require("MPR121").connect(I2C2, function() {
        //device 2 (12-position rotary switch) is ready
        setInterval(function() {
          var banks = mpr2.touched();
          if (banks!=lastBankSelected){
            for (var j=0;j<12;j++){
              var bbit = 1<<j;
              if ((banks&bbit) && !(lastBankSelected&bbit))
                //do something, for example:
                console.log("Bank: " + j);
            }
          }
          lastBankSelected = banks;
        }, 100);
      });
    }
    
    setTimeout(onInit,999); // for dev; comment before save()
    
  • I really am amazed at how much time you just saved me. Thank you so much. Later tonight, I'll be able to hook up my device and send this to it.

    As you may have guessed, I am at a very beginner level of understanding - yet I understand and appreciate your clear explanation. The fact that you handed it to me on a silver platter is just amazing. Thank you so much.

    Once I have my beep-boop-beep DIY "synth" making sounds, I will show the results.

    Lastly, I've never experienced a more positive helpful forum anywhere on the Internet. So, thank you @Gordon @allObjects @Robin

About

Avatar for allObjects @allObjects started