Can you make a matrix keypad light an LED - but ONLY while a key is pressed?

I’m trying to use a MEGA to take keypress info from a three-column, four-row matrix keyboard (12 keys) and output to a series of corresponding LEDs using the ‘Keypad’ library. But I need the LEDs to light ONLY while a key is pressed, and to stay off otherwise. (The eventual goal is to expand keypad and supply control voltages to an analog synth via a resistor array)

I can make LEDs go off after a set time using delay; I can make one LED go off after another key is pressed, turning another on; BUT I can’t seem to get LEDs to stay on ONLY while a key is pressed; I tried the ‘while’ statement but so far to no avail. (I could employ a separate “enable” switch on the circuit that responds to any key press if that’s easier.)

Code posted below. Any ideas??? I vastly appreciate the forum’s help.

#include <Keypad.h>

const byte ROWS = 3; // Four rows
const byte COLS = 4; // Three columns

char keys[ROWS][COLS] = { // Define the Keymap
  {'1','2','3','4'},
  {'5','6','7','8'},
  {'A','B','C','D'},
};

byte rowPins[ROWS] = { 2, 3, 4, };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.

byte colPins[COLS] = { 5, 6, 7, 8 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.


Keypad kbrd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );// Create the Keypad


#define ledpin1 A1
#define ledpin2 A2
#define ledpin3 A3
#define ledpin4 A4
#define ledpin5 A5
#define ledpin6 A6
#define ledpin7 A7
#define ledpin8 A8

void setup()
{

   pinMode(ledpin1,OUTPUT);
   pinMode(ledpin2,OUTPUT);
   pinMode(ledpin3,OUTPUT);
   pinMode(ledpin4,OUTPUT);
   pinMode(ledpin5,OUTPUT);
   pinMode(ledpin6,OUTPUT);
   pinMode(ledpin7,OUTPUT);
   pinMode(ledpin8,OUTPUT);

  Serial.begin(9600);
}

void loop()

{
  char key = kbrd.getKey();
  
  //while(key)  // This lights LED of first key pressed and stays on forever
  
  if (key)
  {
    switch (key)
    {
      case '1':
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
       digitalWrite(ledpin4, LOW);
       digitalWrite(ledpin5, LOW);
       digitalWrite(ledpin6, LOW);
       digitalWrite(ledpin7, LOW);
       digitalWrite(ledpin8, LOW);
       digitalWrite(ledpin4, HIGH);
       break;
        
      case '2':
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
       digitalWrite(ledpin4, LOW);
       digitalWrite(ledpin5, LOW);
       digitalWrite(ledpin6, LOW);
       digitalWrite(ledpin7, LOW);
       digitalWrite(ledpin8, LOW);
       digitalWrite(ledpin3, HIGH);
       break;
        
      case '3':
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
       digitalWrite(ledpin4, LOW);
       digitalWrite(ledpin5, LOW);
       digitalWrite(ledpin6, LOW);
       digitalWrite(ledpin7, LOW);
       digitalWrite(ledpin8, LOW);
       digitalWrite(ledpin2, HIGH);
       break;
        
      case '4':
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
       digitalWrite(ledpin4, LOW);
       digitalWrite(ledpin5, LOW);
       digitalWrite(ledpin6, LOW);
       digitalWrite(ledpin7, LOW);
       digitalWrite(ledpin8, LOW);
       digitalWrite(ledpin1, HIGH);
       break;
        
      case '5':
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
       digitalWrite(ledpin4, LOW);
       digitalWrite(ledpin5, LOW);
       digitalWrite(ledpin6, LOW);
       digitalWrite(ledpin7, LOW);
       digitalWrite(ledpin8, LOW);
       digitalWrite(ledpin8, HIGH);
       break;
        
      case '6':
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
       digitalWrite(ledpin4, LOW);
       digitalWrite(ledpin5, LOW);
       digitalWrite(ledpin6, LOW);
       digitalWrite(ledpin7, LOW);
       digitalWrite(ledpin8, LOW);
       digitalWrite(ledpin7, HIGH);
       break;
        
      case '7':
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
       digitalWrite(ledpin4, LOW);
       digitalWrite(ledpin5, LOW);
       digitalWrite(ledpin6, LOW);
       digitalWrite(ledpin7, LOW);
       digitalWrite(ledpin8, LOW);
       digitalWrite(ledpin6, HIGH);
       break;
        
      case '8':
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
       digitalWrite(ledpin4, LOW);
       digitalWrite(ledpin5, LOW);
       digitalWrite(ledpin6, LOW);
       digitalWrite(ledpin7, LOW);
       digitalWrite(ledpin8, LOW);
       digitalWrite(ledpin5, HIGH);
       break;
        
      case 'A':
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
       digitalWrite(ledpin4, LOW);
       digitalWrite(ledpin5, LOW);
       digitalWrite(ledpin6, LOW);
       digitalWrite(ledpin7, LOW);
       digitalWrite(ledpin8, LOW);
       digitalWrite(ledpin8, HIGH);
       digitalWrite(ledpin4, HIGH);
       break;

      case 'B':
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
       digitalWrite(ledpin4, LOW);
       digitalWrite(ledpin5, LOW);
       digitalWrite(ledpin6, LOW);
       digitalWrite(ledpin7, LOW);
       digitalWrite(ledpin8, LOW);
       digitalWrite(ledpin7, HIGH);
       digitalWrite(ledpin3, HIGH);
       break;

   
      case 'C':
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
       digitalWrite(ledpin4, LOW);
       digitalWrite(ledpin5, LOW);
       digitalWrite(ledpin6, LOW);
       digitalWrite(ledpin7, LOW);
       digitalWrite(ledpin8, LOW);
       digitalWrite(ledpin6, HIGH);
       digitalWrite(ledpin2, HIGH);
       break;     

      case 'D':
       digitalWrite(ledpin1, LOW);
       digitalWrite(ledpin2, LOW);
       digitalWrite(ledpin3, LOW);
       digitalWrite(ledpin4, LOW);
       digitalWrite(ledpin5, LOW);
       digitalWrite(ledpin6, LOW);
       digitalWrite(ledpin7, LOW);
       digitalWrite(ledpin8, LOW);
       digitalWrite(ledpin5, HIGH);
       digitalWrite(ledpin1, HIGH);
       break;   
        
      default:
      Serial.println(key);
    }
  }
}

try looking at this

@BulldogLowell
thanks - will do :slight_smile:

The 'Hold' function seems to be about pressing a button for a set time until it 'activates'
It looks like the KeyStaye getState() function might be useful...

But I haven't seen it used in an actual sketch. Any examples?

OK, turns out there’s an easy way to make action happen only while a key is being pressed, using built-in Keypad methods:

  1. Use getKey() method to find which key is pressed and take specific action, then
  2. Use getState() method to find when a key is RELEASED, and stop action

Here’s the working test program if anyone is interested

#include <Keypad.h>

const byte ROWS = 3; // Four rows
const byte COLS = 4; // Three columns

char keys[ROWS][COLS] = { // Define the Keymap
  {'1','2','3','4'},
  {'5','6','7','8'},
  {'A','B','C','D'},
};

byte rowPins[ROWS] = { 2, 3, 4, };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.

byte colPins[COLS] = { 5, 6, 7, 8 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.


Keypad kbrd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );// Create the Keypad


#define ledpin1 A1
#define ledpin2 A2
#define ledpin3 A3
#define ledpin4 A4
#define ledpin5 A5
#define ledpin6 A6
#define ledpin7 A7
#define ledpin8 A8
void setup()
{

   pinMode(ledpin1,OUTPUT);
   pinMode(ledpin2,OUTPUT);
   pinMode(ledpin3,OUTPUT);
   pinMode(ledpin4,OUTPUT);
   pinMode(ledpin5,OUTPUT);
   pinMode(ledpin6,OUTPUT);
   pinMode(ledpin7,OUTPUT);
   pinMode(ledpin8,OUTPUT);

  Serial.begin(9600);
}

void loop()
{
   char key = kbrd.getKey();        //this gets which key
   KeyState btn = kbrd.getState();  //this gets current STATE of key
   
   if (key == '1'){
      digitalWrite(ledpin4, HIGH);  
      }
   if (key == '2'){
      digitalWrite(ledpin3, HIGH);  
      }
   if (key == '3'){
      digitalWrite(ledpin2, HIGH);  
      }
   if (key == '4'){
      digitalWrite(ledpin1, HIGH);  
      }
   if (key == '5'){
      digitalWrite(ledpin8, HIGH);  
      }
   if (key == '6'){
      digitalWrite(ledpin7, HIGH);  
      }
   if (key == '7'){
      digitalWrite(ledpin6, HIGH);  
      }
   if (key == '8'){
      digitalWrite(ledpin5, HIGH);  
      }
   if (key == 'A'){
      digitalWrite(ledpin8, HIGH);
      digitalWrite(ledpin4, HIGH);  
      }
   if (key == 'B'){
      digitalWrite(ledpin7, HIGH);
      digitalWrite(ledpin3, HIGH); 
      }
   if (key == 'C'){
      digitalWrite(ledpin6, HIGH);
      digitalWrite(ledpin2, HIGH);  
      }
   if (key == 'D'){
      digitalWrite(ledpin5, HIGH);
      digitalWrite(ledpin1, HIGH);  
      }

    if (btn == RELEASED){
      digitalWrite(ledpin1, LOW); //LED turns off when button released
      digitalWrite(ledpin2, LOW);
      digitalWrite(ledpin3, LOW);
      digitalWrite(ledpin4, LOW);
      digitalWrite(ledpin5, LOW); 
      digitalWrite(ledpin6, LOW);
      digitalWrite(ledpin7, LOW);
      digitalWrite(ledpin8, LOW);
      }
 
}

b_orka:
OK, turns out there's an easy way to make action happen only while a key is being pressed, using built-in Keypad methods:

  1. Use getKey() method to find which key is pressed and take specific action, then
  2. Use getState() method to find when a key is RELEASED, and stop action

Here's the working test program if anyone is interested

k++

:slight_smile: