safwan092

Project 17674

Feb 26th, 2026
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <Keypad.h>
  4.  
  5. // ── LCD ──────────────────────────────────────────────────────────────────────
  6. LiquidCrystal_I2C lcd(0x27, 16, 2); // change 0x27 to 0x3F if display is blank
  7.  
  8. // ── Keypad ───────────────────────────────────────────────────────────────────
  9. const byte ROWS = 4, COLS = 4;
  10. char keys[ROWS][COLS] = {
  11. {'1','2','3','A'},
  12. {'4','5','6','B'},
  13. {'7','8','9','C'},
  14. {'*','0','#','D'}
  15. };
  16. byte rowPins[ROWS] = {A0, 8, 7, 6};
  17. byte colPins[COLS] = {5, 4, 3, 2};
  18. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
  19.  
  20. // ── LED Pins ─────────────────────────────────────────────────────────────────
  21. const byte LED_PINS[5] = {10, 11, 12, 13, 9};
  22.  
  23. // ── Room Codes ───────────────────────────────────────────────────────────────
  24. const char* ROOM_CODES[5] = {"0050", "0053", "0084", "0085", "0086"};
  25. const byte ROOM_COUNT = 5;
  26.  
  27. // ── State ────────────────────────────────────────────────────────────────────
  28. bool ledState[5] = {false, false, false, false, false};
  29. String inputBuffer = "";
  30.  
  31. // ── Helpers ──────────────────────────────────────────────────────────────────
  32. void promptUser() {
  33. lcd.clear();
  34. lcd.setCursor(0, 0);
  35. lcd.print("Input room num:");
  36. lcd.setCursor(0, 1);
  37. lcd.print(" ");
  38. Serial.println("---------------------------");
  39. Serial.println("Prompt: Waiting for input...");
  40. }
  41.  
  42. void showInput(const String& buf) {
  43. lcd.setCursor(0, 1);
  44. lcd.print("Code: ");
  45. lcd.print(buf);
  46. for (byte i = buf.length(); i < 10; i++) lcd.print(' ');
  47.  
  48. Serial.print("Key pressed | Buffer: [");
  49. Serial.print(buf);
  50. Serial.println("]");
  51. }
  52.  
  53. void showFeedback(byte roomIndex, bool turnedOn) {
  54. lcd.clear();
  55. lcd.setCursor(0, 0);
  56. lcd.print("Room ");
  57. lcd.print(roomIndex + 1);
  58. lcd.print(turnedOn ? ": ON " : ": OFF");
  59. lcd.setCursor(0, 1);
  60. lcd.print("LED toggled!");
  61.  
  62. Serial.print("SUCCESS | Room ");
  63. Serial.print(roomIndex + 1);
  64. Serial.print(" LED -> ");
  65. Serial.println(turnedOn ? "ON" : "OFF");
  66. Serial.print("LED pin ");
  67. Serial.print(LED_PINS[roomIndex]);
  68. Serial.println(turnedOn ? " set HIGH" : " set LOW");
  69.  
  70. delay(1500);
  71. promptUser();
  72. }
  73.  
  74. void showInvalid() {
  75. lcd.clear();
  76. lcd.setCursor(0, 0);
  77. lcd.print("Invalid code!");
  78.  
  79. Serial.print("INVALID | Code entered: [");
  80. Serial.print(inputBuffer);
  81. Serial.println("] — no match found");
  82.  
  83. delay(1200);
  84. promptUser();
  85. }
  86.  
  87. int matchCode(const String& code) {
  88. Serial.print("Matching code: [");
  89. Serial.print(code);
  90. Serial.println("]");
  91. for (byte i = 0; i < ROOM_COUNT; i++) {
  92. Serial.print(" Checking room ");
  93. Serial.print(i + 1);
  94. Serial.print(" code [");
  95. Serial.print(ROOM_CODES[i]);
  96. Serial.print("] -> ");
  97. if (code == ROOM_CODES[i]) {
  98. Serial.println("MATCH!");
  99. return i;
  100. }
  101. Serial.println("no match");
  102. }
  103. return -1;
  104. }
  105.  
  106. // ── Setup ─────────────────────────────────────────────────────────────────────
  107. void setup() {
  108. Serial.begin(9600);
  109. Serial.println("=== Room LED Controller ===");
  110. Serial.println("Initializing...");
  111.  
  112. for (byte i = 0; i < ROOM_COUNT; i++) {
  113. pinMode(LED_PINS[i], OUTPUT);
  114. digitalWrite(LED_PINS[i], LOW);
  115. Serial.print("LED pin ");
  116. Serial.print(LED_PINS[i]);
  117. Serial.println(" initialized LOW");
  118. }
  119.  
  120. lcd.init();
  121. lcd.backlight();
  122. Serial.println("LCD initialized");
  123. Serial.println("System ready!");
  124. Serial.println("---------------------------");
  125.  
  126. promptUser();
  127. }
  128.  
  129. // ── Loop ──────────────────────────────────────────────────────────────────────
  130. void loop() {
  131. char key = keypad.getKey();
  132. if (!key) return;
  133.  
  134. Serial.print("Key detected: [");
  135. Serial.print(key);
  136. Serial.println("]");
  137.  
  138. // '*' clears/cancels
  139. if (key == '*') {
  140. Serial.println("CLEAR | Input buffer reset");
  141. inputBuffer = "";
  142. promptUser();
  143. return;
  144. }
  145.  
  146. // '#' confirms entry
  147. if (key == '#') {
  148. Serial.print("CONFIRM | Submitting code: [");
  149. Serial.print(inputBuffer);
  150. Serial.println("]");
  151.  
  152. if (inputBuffer.length() == 4) {
  153. int room = matchCode(inputBuffer);
  154. if (room != -1) {
  155. ledState[room] = !ledState[room];
  156. digitalWrite(LED_PINS[room], ledState[room] ? HIGH : LOW);
  157. showFeedback(room, ledState[room]);
  158. } else {
  159. showInvalid();
  160. }
  161. } else {
  162. Serial.print("INVALID | Code length is ");
  163. Serial.print(inputBuffer.length());
  164. Serial.println(", expected 4 digits");
  165. showInvalid();
  166. }
  167. inputBuffer = "";
  168. return;
  169. }
  170.  
  171. // accept only digits 0-9, max 4 characters
  172. if (isDigit(key) && inputBuffer.length() < 4) {
  173. inputBuffer += key;
  174. showInput(inputBuffer);
  175. } else if (!isDigit(key)) {
  176. Serial.print("IGNORED | Non-digit key: [");
  177. Serial.print(key);
  178. Serial.println("]");
  179. } else if (inputBuffer.length() >= 4) {
  180. Serial.println("IGNORED | Buffer already full (4 digits max)");
  181. }
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment