• Using your keypad, you can give code below (as well attached) a shot. In your case, the use of pins for driving and sensing a 3x2 = max 6 buttons and using only 4 of the 6 options while consuming 5 GPIO pins is not the most effective use of anyway already sparce resources... to say the least. You may consider an SPI or I2C driven port extender that uses max 3 GPIO pins for 8, 16, 24, 32,... estended pins...

    // Modue for Drive/Sense Multiplexed Keypad
    //
    // - supports any number of driving pins (rows)
    // - supports any number of sensing pins (cols)
    // - has enable(true/false) for controlled actions
    // - callback called w/ button # 0,1,2,3,... and time
    // - optional:
    //   - start disabled vs. default enabled (eval boolean)
    //   - driving high/watch_rising vs. default low/falling
    //   - custom debounce vs. 200[ms] default (and minimum)
    //
    //
    // Note: momentary-on push switches connect rows w/ cols
    //
    // Example: Keypad controlling AC (pins Espruino Pico)
    //
    // \    sense 
    //   \  pins: pin B1   pin B10
    //     \       |        |        
    // drive \     |        |
    // pins:   \   |        |
    //           \ |        |
    // pin B13 ----0:pwr----1:------ pwr on/off(toggle), N/C
    //             |        |
    // pin B14 ----2:tDwn---3:tUp--- target temp down  , up
    //             |        |
    // pin B15 ----4:fan----5:------ fan on/off(toggle), N/C
    //             |        |
    
    var keypadModule = (function(){
    
      var Keypad = function(dps,sps,cb,e,p,d) {
        this.dps = dps;
        this.sps = sps;
        this.cb = cb;
        this.p = p;
        this.d = d;
        this.e = e;
        this.pl = (p === "up") ? 1 : 0;
        this.np = 0;
        this.ws = null;
      }, p = Keypad.prototype;
    
      p.init = function(_this) {
        this.sps.forEach(function(sp,i){
          pinMode(sp,"input_pull" + _this.p);
        });
        this.drive(this);
        this.watch(this);
      };
      p.drive = function(_this){
        this.dps.forEach(function(dp){
          pinMode(dp,"output");
          digitalWrite(dp,1 - _this.p);
        });
      };
      p.watch = function(_this){
        this.ws = [];
        this.sps.forEach(function(sp,i){ var si = i;
          _this.ws.push( setWatch( 
                function(ep){ _this.scan(si,ep.time); }
              , sp
              , { repeat: false
                , edge: (_this.pl) ? "falling" : "rising"
                , debounce: _this.d } ) );
        });
        this.w = true;
      };
      p.scan = function(si,tp) {
        if (dbg) log(tp - this.np);
        this.ws.forEach(function(w,i){ clearWatch(w); });
        if (this.e && (tp > this.np)) {
          this.dps.forEach(function(dp){ pinMode(dp,"input"); });
          var b = -1, di, dl = this.dps.length, dp;
          var sp = this.sps[si], s = 1 - this.pl;
          for (di = 0; di < dl; di++) { 
            if (dbg) log("scan si: ",si," - di: ", di, " - s: ", s); 
            pinMode(dp = this.dps[di],"output");
            if (digitalRead(sp) === s) {
              b = di * this.sps.length + si; dl = 0;
            } pinMode(dp,"input");
          } this.drive(this); this.watch(this);
          this.np = getTime() + (this.d / 100);
          if (b >= 0) { this.cb(b,tp); } 
        } else { this.watch(this); }
      };
    
      p.enable = function(e) { this.e = !!e; };
    
      return ({connect:function(
         drivePins  // array of drive pins 
        ,sensePins  // array of sense pins;
        ,callback   // callback acceptin button # 0,1,2,...
        ,enabled    // optional, default = true
        ,pull       // optional, default = "up"
        ,debounce   // optional, default = 200, min = 200, [ms]
        ){
          var kp = new Keypad(drivePins,sensePins,callback
            , !enabled
            , ((pull === "up") ? pull : (pull === "down") ? pull : "up")
            , (    (isNaN(debounce)
                || ((debounce * 1) < 200)
              ) ? 200 : debounce * 1) );
           kp.init(kp);
           return kp;
      }});
    })();
    
    var dbg = true;
    var log = function() { console.log(arguments.join("")); };
    
    // var keypad = require("KeypadMDS").connect(
    var keypad = keypadModule.connect(
        [B13,B14,B15], [B1,B10], function(btn,tme){ // 0,1,2,3...
          if (dbg) log("" + tme + ": Button # ",btn," pressed: "
              , [ "pwr toggle","N/C"
                , "temp down" ,"temp up"
                , "fan toggle","N/C"
                ][btn] );
      }); 
    

    1 Attachment

About

Avatar for allObjects @allObjects started