Press and Hold button to change led toggle state, loops with NO_KEY

I am having difficulty getting an led toggle state to work with NO_KEY instead of digitalRead. Currently I am toggling an led on and off by pressing and holding a button for 3 seconds to change the state of the led. That is working correctly, but when I want to drive it using any button from my matrix keypad via a shift register, NO_KEY just loops the led on and off.

Any help would be appreciated.

#include <Keypad_MC17.h>
#include <Keypad.h>        // GDY120705
#include <Wire.h>

#define I2CADDR 0x20

#include "LedControl.h" //  need the library
LedControl lc = LedControl(12, 11, 10, 1); //LedControl(int dataPin, int clkPin, int csPin, int numDevices=1);

byte val[8][8] = {0};

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'0', '1', '2', '3'},
  {'4', '5', '6', '7'},
  {'8', '9', 'A', 'B'},
  {'C', 'D', 'E', 'F',}
};


byte rowPins[ROWS] = {0, 1, 2, 3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 5, 6, 7}; //connect to the column pinouts of the keypad

byte row;
byte col;
byte matchCol;
byte matchRow;

//initialize an instance of class NewKeypad
Keypad_MC17 customKeypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS, I2CADDR);


const int pinLED = 13;
const int pinSwitch = 2;

byte currSwitch, lastSwitch;
unsigned long timeStart;
bool bCheckingSwitch;
bool ledState;

void setup() {
  //  Wire.begin( );
  customKeypad.begin( );        // GDY120705
  Serial.begin(9600);

  lc.shutdown(0, false); // turn off power saving, enables display
  lc.setIntensity(0, 8); // sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen


  pinMode( pinLED, OUTPUT );
  digitalWrite( pinLED, LOW );
  pinMode( pinSwitch, INPUT_PULLUP );
  lastSwitch = digitalRead( pinSwitch );
  bCheckingSwitch = false;
  ledState = false;

}

void loop() {



  char customKey = customKeypad.getKey();

  for (row = 0; row < 4; row++)
  {
    for (col = 0; col < 4; col++)
    {
      if (hexaKeys[row][col] == customKey)
      {
        matchCol = col;
        matchRow = row;
      }
    }
  }



  //read the switch (for simplicity, no debouncing done)
  //currSwitch = customKeypad.getKey();
  currSwitch = digitalRead( pinSwitch );
  //if the switch is low now and doesn't match its
  //previous reading, it means the switch has changed
  //either unpressed-to-pressed or pressed-to-unpressed
  if ( currSwitch != lastSwitch )
  {
    //if low now, the change was unpressed to pressed
    if ( currSwitch == LOW )
    {
      //in that case, get the time now to start the
      //5-sec delay...
      timeStart = millis();
      //...indicate we're now timing a press...
      bCheckingSwitch = true;
      //ledState = false;

    }//if
    else
    {
      //was a rising edge (button being released)
      //cancel checking the switch timing now since
      //it's not being held hown
      bCheckingSwitch = false;
      //ledState = false;


    }//else

    //and save the
    lastSwitch = currSwitch;

  }//if

  //if we haven't seen a rising edge, keep checking the
  //timing
  if ( bCheckingSwitch )
  {
    //if the millis count now is 5000 or more higher
    //than it was when the press was detected (timeStart),
    //turn on the LED
    if ((millis() - timeStart ) >= 3000 ) {
      if (ledState == false) {
        digitalWrite( pinLED, HIGH );
        ledState = true;
        delay(3000);
      }
      else {
        digitalWrite( pinLED, LOW );
        ledState = false;
        delay(3000);
      }

    }
  }//if


  if (customKey != NO_KEY) {

    Serial.println(matchRow);
    Serial.println(matchCol);

  }
  if (customKey != NO_KEY && val[matchRow][matchCol] == 0)  {
    val[matchRow][matchCol] = 1;
    lc.setLed(0, matchRow, matchCol, true);
    customKey = NO_KEY;
    delay(200);
  }

  if (customKey != NO_KEY && val[matchRow][matchCol] == 1) {
    val[matchRow][matchCol] = 0;
    lc.setLed(0, matchRow, matchCol, false);
    customKey = NO_KEY;
    delay(200);
  }

}
byte rowPins[ROWS] = {0, 1, 2, 3}; //connect to the row pinouts of the keypad

It is generally not a good idea to use pins 0 and 1 as general IO pins as they are used by the Serial interface of the Arduino

Those are the pins of the shift register MCP23017.

getKey() will only return a value on the first call when a key is pressed.

Look at the EventKeypad example in the IDE for how to detect RELEASE and HOLD.

Thank you, I did not know HOLD was built into the libary. My next question is how do I adjust the time for HOLD?

Thanks everyone! I was able to get my code to work the way I wanted.

#include <Keypad_MC17.h>
#include <Keypad.h>        // GDY120705
#include <Wire.h>

#define I2CADDR 0x20

#include "LedControl.h" //  need the library
LedControl lc = LedControl(12, 11, 10, 1); //LedControl(int dataPin, int clkPin, int csPin, int numDevices=1);

byte val[8][8] = {0};

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'0', '1', '2', '3'},
  {'4', '5', '6', '7'},
  {'8', '9', 'A', 'B'},
  {'C', 'D', 'E', 'F',}
};


byte rowPins[ROWS] = {0, 1, 2, 3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 5, 6, 7}; //connect to the column pinouts of the keypad

byte row;
byte col;
byte matchCol;
byte matchRow;

//initialize an instance of class NewKeypad
Keypad_MC17 customKeypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS, I2CADDR);

bool bCheckingSwitch;

void setup() {
  //  Wire.begin( );
  customKeypad.begin( );        // GDY120705
  Serial.begin(9600);

  lc.shutdown(0, false); // turn off power saving, enables display
  lc.setIntensity(0, 8); // sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen

  customKeypad.addEventListener(keypadEvent); //add an event listener for this keypad
  customKeypad.setHoldTime( 3000 ); // 3 sec hold time

}

void loop() {



  char customKey = customKeypad.getKey();

  for (row = 0; row < 4; row++)
  {
    for (col = 0; col < 4; col++)
    {
      if (hexaKeys[row][col] == customKey)
      {
        matchCol = col;
        matchRow = row;
      }
    }
  }




  if (customKey != NO_KEY) {

    Serial.println(matchRow);
    Serial.println(matchCol);

  }
  if (bCheckingSwitch == true && val[matchRow][matchCol] == 0)  {
    val[matchRow][matchCol] = 1;
    lc.setLed(0, matchRow, matchCol, true);
    bCheckingSwitch = false;
    delay(200);
  }

  if (bCheckingSwitch == true && val[matchRow][matchCol] == 1) {
    val[matchRow][matchCol] = 0;
    lc.setLed(0, matchRow, matchCol, false);
    bCheckingSwitch = false;
    delay(200);
  }
}

void keypadEvent(KeypadEvent customKey){
  switch (customKeypad.getState()){

    case HOLD: Serial.print( "Hello" ); bCheckingSwitch = true  ; break;
      }
 // break;
  }