‘LED Control via Shift Register with 4x4 Matrix Button

Suggested Forum Message:

Hello everyone,

I have the following code that currently works in my setup. When I press a button on the matrix keypad, it toggles a corresponding LED connected via a shift register. However, what I really want is for the LED to turn on only while I’m pressing the button and then turn off as soon as I release it, rather than toggling.

Right now, pressing a button changes the LED state (on/off), and it stays that way until I press the same button again. How can I modify the code so that the LED is lit only when the button is held down and turns off immediately when I let go of it?

Thanks in advance for any help or suggestions! #include <Keypad.h>

// Shift Register Pin Definitions
const int dataPin = 4; // DS pin (Data input)
const int latchPin = 5; // ST_CP pin (Latch pin)
const int clockPin = 6; // SH_CP pin (Clock pin)

// Matrix Keypad Settings
const byte ROWS = 4; // Number of rows
const byte COLS = 4; // Number of columns

// Row and column pins
byte rowPins[ROWS] = {A0, A1, A2, A3}; // Row pins
byte colPins[COLS] = {7, 8, 9, 10}; // Column pins

// Matrix keypad layout
char keys[ROWS][COLS] = {
{'1', '2', '3', '4'},
{'5', '6', '7', '8'},
{'9', 'A', 'B', 'C'},
{'D', 'E', 'F', 'G'}
};

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

// 16-bit LED state for Shift Register control
uint16_t ledState = 0b0000000000000000; // 16-bit LED state

// Function prototypes
void updateShiftRegister();

void setup() {
// Set up shift register pins
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);

// Start Serial Monitor
Serial.begin(9600);
updateShiftRegister(); // Update the LEDs to the initial state
}

void loop() {
char key = keypad.getKey(); // Read the pressed key

if (key) { // If a key was pressed
Serial.print("Pressed Key: ");
Serial.println(key);

// Control the LEDs based on the pressed key
int ledIndex = -1;

if (key >= '1' && key <= '8') { // Is the key between 1-8?
  ledIndex = key - '1';         // Calculate LED index (0-7)
} else if (key >= '9' && key <= 'G') { // Is the key between 9 and G?
  ledIndex = key - '9' + 8;            // Calculate LED index (8-15)
}

if (ledIndex >= 0 && ledIndex < 16) {
  // Toggle the corresponding LED
  ledState ^= (1 << ledIndex);
  updateShiftRegister(); // Update the shift register
}

}
}

// Function to send data to the Shift Register
void updateShiftRegister() {
digitalWrite(latchPin, LOW); // Set latch pin LOW
shiftOut(dataPin, clockPin, MSBFIRST, highByte(ledState)); // Send high byte (first 8 bits)
shiftOut(dataPin, clockPin, MSBFIRST, lowByte(ledState)); // Send low byte (last 8 bits)
digitalWrite(latchPin, HIGH); // Set latch pin HIGH
}

Topic moved. Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Please edit your post and apply code tags to your code; see https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code-and-common-code-problems.

This makes your code easier to read, easier to copy and the forum software will display it correctly.

The Keypad library seems to debounce the keys (thank them for their hard work!)... so that stops you from seeing the keypress ON and OFF. The scan code looks like it might repeat, but it would be easier to read how the library does it, rather than searching through this 100 lines of not-yet-a-pattern scan code (eight bits are rows/columns)...

00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
11111011
11111011
11110110
11110011
11110011
11110101
11110101
11110111
11110111
11111011
11111011
11110011
11110011
11111100
11111100
11110000
11110000
11111011
11111011
11110001
11110001
11110001
11110001
11110110
11110110
11111100
11111100
11110010
11110010
11111100
11111100
11111010
11111010
11110001
11110001
11110111
11111110
11111110
11110111
11110111
11111100
11111100
11110111
11110111
11111011
11111011
11111100
11111100
11111100
11111100
11110010
11110010
11110011
11110011
11111100
11111100
11110101
11110101
11110010
11110010
11111010
11111010
11110011
11110011
11111000
11111000
11110110
11110110
11110011
11110011
11111100
11111100
11111111
11111111
11110110
11110110
11110001
11110001
11110100
11110100
11110000
11110001
11110001
11111010
11111010
11111100
11111100
11110111
11110111
11111110
11111110
11111110
11111110
11110110
11110110
11110011
11110011
11111110
11111110
11111011
11111011
11110101
11110101
11110111
11110111
11110001
11110001
11111101
11111101
11110101
11110101
11111010
11111010
11111111
11111111
11110011

It seems that you haven’t taken the time to read the forum guidelines, which have been shared previously. I value my time as much as you do yours, and I cannot provide assistance without the necessary information in the proper format. Please refer to the guidelines and post your information accordingly. How to get the best out of this forum I may check back in a few days to see if the required details are posted correctly. Until then, good luck with your project!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.