#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#define ver "V1.0.0"
#define LCD_ADDRESS 0x27 // Change this address to match your specific LCD module
#define LCD_COLS 16 // Number of columns in the LCD
#define LCD_ROWS 2 // Number of rows in the LCD
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLS, LCD_ROWS);
#define LED_RED 5
#define LED_GREEN 2
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte keypadKeys[8] = {
13, 12, 14, 27,
26, 25, 33, 32
};
// Shift register pins
const int DS_PIN = 4;
const int OE_PIN = 18;
const int STCP_PIN = 19;
const int SCHP_PIN = 23;
const int MR_PIN = 15;
Keypad customKeypad(makeKeymap(keys), keypadKeys, keypadKeys + COLS, ROWS, COLS);
// Function to set the shift register to 0x88
void initShiftRegister(byte data) {
digitalWrite(MR_PIN, LOW); // Set MR pin to LOW to reset the shift register
delay(10);
digitalWrite(MR_PIN, HIGH); // Set MR pin back to HIGH to enable the shift register
// Shift out the data to the shift register (0x88)
shiftOut(DS_PIN, SCHP_PIN, MSBFIRST, data);
// Set OE pin to LOW to enable the output of the shift register
digitalWrite(OE_PIN, LOW);
// Set STCP pin to HIGH to latch the data
digitalWrite(STCP_PIN, HIGH);
delay(10);
digitalWrite(STCP_PIN, LOW);
}
void setup() {
Serial.begin(115200);
Wire.begin();
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.setBacklight(1);
char msg[128];
sprintf(msg, "DSecurity, %s", ver);
lcd.print(msg);
// setup leds
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, HIGH);
// Set shift register pins as OUTPUT
pinMode(DS_PIN, OUTPUT);
pinMode(OE_PIN, OUTPUT);
pinMode(STCP_PIN, OUTPUT);
pinMode(SCHP_PIN, OUTPUT);
pinMode(MR_PIN, OUTPUT);
// Initialize the shift register to 0x88
initShiftRegister(0b10101010);
}
byte relayPos = 0;
void loop() {
char pressedKey = customKeypad.getKey();
if (pressedKey) {
lcd.setCursor(0, 1); // Set the cursor to the second row
lcd.print("Key Pressed: ");
lcd.print(pressedKey);
Serial.print("Key Pressed: ");
Serial.println(pressedKey);
}
// Your other loop code goes here
initShiftRegister(relayPos);
relayPos++;
delay(500); // Add a small delay for stability
}