Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // matrix keypad reader demo
- // John Ridley 2015-03-09
- // published on DragonflyDIY.com as part of the Arduino Blocks project
- // released CC0 public domain dedication
- int col1 = 4;
- int col2 = 3;
- int col3 = 2;
- int row1 = 8;
- int row2 = 7;
- int row3 = 6;
- int row4 = 5;
- void setup()
- {
- Serial.begin(9600);
- }
- void loop()
- {
- char lastkey = 0;
- while (1)
- {
- char curkey = readkey();
- if (curkey != lastkey)
- {
- if (curkey != 0)
- {
- Serial.println(curkey);
- }
- lastkey = curkey;
- }
- delay(10); // debouncing
- }
- }
- char readkey()
- {
- int rows[4] = {row1, row2, row3, row4};
- int cols[4] = {col1, col2, col3};
- char chars[4][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'}};
- // set EVERYTHING to input - we only want to DRIVE one row at a time (either high OR low)
- // turn on pull-up resistors on the columns
- pinMode(col1, INPUT_PULLUP);
- pinMode(col2, INPUT_PULLUP);
- pinMode(col3, INPUT_PULLUP);
- // reset all rows to input to start with
- for (int currow = 0; currow < 4; currow++)
- {
- pinMode(rows[currow], INPUT);
- }
- for (int currow = 0; currow < 4; currow++)
- {
- // turn this row to output and drive it low
- pinMode(rows[currow], OUTPUT);
- digitalWrite(rows[currow], 0);
- // scan all the columns, look for a low. If it's low it must be because
- // the button is being pressed (since we have the pull-ups activated on the columns)
- for (int curcol = 0; curcol < 3; curcol++)
- {
- int val = digitalRead(cols[curcol]);
- if (val == 0)
- return chars[currow][curcol];
- }
- pinMode(rows[currow], INPUT);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement