// // Program name: keypad01 // // Purpose: // scan & read key presses on 3x4 membrane keypad // // Edit History: // rev. 01: keypad read works // clear and enter keys work // passcode and set alarm commands work // LED to blink on keypress // LEDs to indicate alarmed or not // // Program operation/flow: // A column will be set low, then each row is read to see which key is pressed // if key is pressed, accumulated string is tested against defined commands, // then string is cleared // defined commands are: set alarm, clear alarm // if key is pressed, string is cleared // any other keypress is added to accumulated string. // unless max# keys exceeded, in which case string is cleared // // Hardware configuration: // 7, 8, 9 column pins // 3, 4, 5, 6 row pins // 10 LED to blink whenever key is pressed // 11 for red LED = alarm ON // 12 for grn LED = alarm OFF // ------------------------------------------------------ // ************** INCLUDES ******************* // ---------- END includes ------------ // ************** GLOBALS ******************* int colPins[] = {7, 8, 9}; // pin designations for cols int rowPins[] = {3, 4, 5, 6}; // pin designations for rows const int numCols = 3; const int numRows = 4; const char keyMap[numRows][numCols] = { { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' }, { '*', '0', '#' } }; char keyPressed; String stringEntered; String passCode = "4732"; String alarmON = "111"; int keyPressLED = 10; int redLED = 11; int grnLED = 12; int numsMax = 5; // ---------------- END globals ----------------- // ************** SETUP ******************* void setup() { Serial.begin(9600); pinMode(keyPressLED, OUTPUT); pinMode(redLED, OUTPUT); pinMode(grnLED, OUTPUT); digitalWrite(redLED, HIGH); // set columns as outputs for (int n = 0; n < numCols; n++) { pinMode(colPins[n], OUTPUT); digitalWrite(colPins[n], HIGH); } // endfor // set rows as inputs for (int n = 0; n < numRows; n++) { pinMode(rowPins[n], INPUT_PULLUP); } // endfor } // -------------- END setup -------------- // ************** LOOP ******************* void loop() { // set col low for (int c = 0; c < numCols; c++) { digitalWrite(colPins[c], LOW); // read rows for (int r = 0; r < numRows; r++) { if (digitalRead(rowPins[r]) == LOW) { // then key is pressed // blink keyPressLED digitalWrite(keyPressLED, HIGH); delay(100); digitalWrite(keyPressLED, LOW); delay(300); keyPressed = keyMap[r][c]; if (keyPressed == '#') { // pressed // here test the captured string for validity if (stringEntered == passCode) { digitalWrite(redLED, LOW); digitalWrite(grnLED, HIGH); } // endif if (stringEntered == alarmON) { digitalWrite(redLED, HIGH); digitalWrite(grnLED, LOW); } // endif // clear captured string stringEntered = ""; break; } // endif # if (keyPressed == '*') { // pressed // clear captured string stringEntered = ""; break; } // endif # // else add keypressed to captured string stringEntered += keyPressed; break; } // endif state LOW } // endfor rows if (stringEntered.length() > numsMax) { stringEntered = ""; // clear it } digitalWrite(colPins[c], HIGH); } // endfor cols } // --------------- END loop --------------- // **************** FUNCTION DEFS ***************** // --------------- END function defs ------------ // --------------- END program ------------------