Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <Keypad.h>
- // ── LCD ──────────────────────────────────────────────────────────────────────
- LiquidCrystal_I2C lcd(0x27, 16, 2); // change 0x27 to 0x3F if display is blank
- // ── Keypad ───────────────────────────────────────────────────────────────────
- const byte ROWS = 4, COLS = 4;
- char keys[ROWS][COLS] = {
- {'1','2','3','A'},
- {'4','5','6','B'},
- {'7','8','9','C'},
- {'*','0','#','D'}
- };
- byte rowPins[ROWS] = {A0, 8, 7, 6};
- byte colPins[COLS] = {5, 4, 3, 2};
- Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
- // ── LED Pins ─────────────────────────────────────────────────────────────────
- const byte LED_PINS[5] = {10, 11, 12, 13, 9};
- // ── Room Codes ───────────────────────────────────────────────────────────────
- const char* ROOM_CODES[5] = {"0050", "0053", "0084", "0085", "0086"};
- const byte ROOM_COUNT = 5;
- // ── State ────────────────────────────────────────────────────────────────────
- bool ledState[5] = {false, false, false, false, false};
- String inputBuffer = "";
- // ── Helpers ──────────────────────────────────────────────────────────────────
- void promptUser() {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Input room num:");
- lcd.setCursor(0, 1);
- lcd.print(" ");
- Serial.println("---------------------------");
- Serial.println("Prompt: Waiting for input...");
- }
- void showInput(const String& buf) {
- lcd.setCursor(0, 1);
- lcd.print("Code: ");
- lcd.print(buf);
- for (byte i = buf.length(); i < 10; i++) lcd.print(' ');
- Serial.print("Key pressed | Buffer: [");
- Serial.print(buf);
- Serial.println("]");
- }
- void showFeedback(byte roomIndex, bool turnedOn) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Room ");
- lcd.print(roomIndex + 1);
- lcd.print(turnedOn ? ": ON " : ": OFF");
- lcd.setCursor(0, 1);
- lcd.print("LED toggled!");
- Serial.print("SUCCESS | Room ");
- Serial.print(roomIndex + 1);
- Serial.print(" LED -> ");
- Serial.println(turnedOn ? "ON" : "OFF");
- Serial.print("LED pin ");
- Serial.print(LED_PINS[roomIndex]);
- Serial.println(turnedOn ? " set HIGH" : " set LOW");
- delay(1500);
- promptUser();
- }
- void showInvalid() {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Invalid code!");
- Serial.print("INVALID | Code entered: [");
- Serial.print(inputBuffer);
- Serial.println("] — no match found");
- delay(1200);
- promptUser();
- }
- int matchCode(const String& code) {
- Serial.print("Matching code: [");
- Serial.print(code);
- Serial.println("]");
- for (byte i = 0; i < ROOM_COUNT; i++) {
- Serial.print(" Checking room ");
- Serial.print(i + 1);
- Serial.print(" code [");
- Serial.print(ROOM_CODES[i]);
- Serial.print("] -> ");
- if (code == ROOM_CODES[i]) {
- Serial.println("MATCH!");
- return i;
- }
- Serial.println("no match");
- }
- return -1;
- }
- // ── Setup ─────────────────────────────────────────────────────────────────────
- void setup() {
- Serial.begin(9600);
- Serial.println("=== Room LED Controller ===");
- Serial.println("Initializing...");
- for (byte i = 0; i < ROOM_COUNT; i++) {
- pinMode(LED_PINS[i], OUTPUT);
- digitalWrite(LED_PINS[i], LOW);
- Serial.print("LED pin ");
- Serial.print(LED_PINS[i]);
- Serial.println(" initialized LOW");
- }
- lcd.init();
- lcd.backlight();
- Serial.println("LCD initialized");
- Serial.println("System ready!");
- Serial.println("---------------------------");
- promptUser();
- }
- // ── Loop ──────────────────────────────────────────────────────────────────────
- void loop() {
- char key = keypad.getKey();
- if (!key) return;
- Serial.print("Key detected: [");
- Serial.print(key);
- Serial.println("]");
- // '*' clears/cancels
- if (key == '*') {
- Serial.println("CLEAR | Input buffer reset");
- inputBuffer = "";
- promptUser();
- return;
- }
- // '#' confirms entry
- if (key == '#') {
- Serial.print("CONFIRM | Submitting code: [");
- Serial.print(inputBuffer);
- Serial.println("]");
- if (inputBuffer.length() == 4) {
- int room = matchCode(inputBuffer);
- if (room != -1) {
- ledState[room] = !ledState[room];
- digitalWrite(LED_PINS[room], ledState[room] ? HIGH : LOW);
- showFeedback(room, ledState[room]);
- } else {
- showInvalid();
- }
- } else {
- Serial.print("INVALID | Code length is ");
- Serial.print(inputBuffer.length());
- Serial.println(", expected 4 digits");
- showInvalid();
- }
- inputBuffer = "";
- return;
- }
- // accept only digits 0-9, max 4 characters
- if (isDigit(key) && inputBuffer.length() < 4) {
- inputBuffer += key;
- showInput(inputBuffer);
- } else if (!isDigit(key)) {
- Serial.print("IGNORED | Non-digit key: [");
- Serial.print(key);
- Serial.println("]");
- } else if (inputBuffer.length() >= 4) {
- Serial.println("IGNORED | Buffer already full (4 digits max)");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment