#include <Keypad.h>
const byte ROWS = 4; // Number of rows
const byte COLS = 4; // Number of columns
// Keypad layout
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Pin assignments for the CD74HC4067
const int S0 = 2;
const int S1 = 4;
const int S2 = 7;
const int S3 = 8;
const int SIG = A0; // Signal pin connected to multiplexer
void setup() {
Serial.begin(9600);
// Set multiplexer control pins as outputs
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
// Set multiplexer signal pin as input
pinMode(SIG, INPUT);
}
// Function to select a multiplexer channel
void selectChannel(int channel) {
digitalWrite(S0, channel & 0x01);
digitalWrite(S1, (channel >> 1) & 0x01);
digitalWrite(S2, (channel >> 2) & 0x01);
digitalWrite(S3, (channel >> 3) & 0x01);
}
void loop() {
for (int row = 0; row < 4; row++) {
// Select the row
selectChannel(row);
// Check all columns for a pressed key
for (int col = 0; col < 4; col++) {
selectChannel(col + 4);
// If the signal pin reads HIGH, a key is pressed
if (digitalRead(SIG) == HIGH) {
Serial.println(keys[row][col]); // Print the pressed key
delay(300); // Debounce delay
return; // Exit after detecting one key
}
}
}
}