{"id":2053,"date":"2015-01-25T23:17:35","date_gmt":"2015-01-25T23:17:35","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=2053"},"modified":"2015-01-25T23:17:35","modified_gmt":"2015-01-25T23:17:35","slug":"simple-arduino-code-read-keypad","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/01\/simple-arduino-code-read-keypad\/","title":{"rendered":"Some simple Arduino code to read from a keypad"},"content":{"rendered":"<p><a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/arduino_keypad-e1422227239397.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/arduino_keypad-e1422227239397-936x1024.jpg\" alt=\"arduino_keypad\" width=\"700\" height=\"766\" class=\"aligncenter size-large wp-image-2054\" srcset=\"https:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/arduino_keypad-e1422227239397-936x1024.jpg 936w, https:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/arduino_keypad-e1422227239397-274x300.jpg 274w, https:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/arduino_keypad-e1422227239397.jpg 1044w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p>Wired up a cheap keypad to an Arduino. These keypads were a few cents each on ebay and look suitable for a project I&#8217;m working on. This is the same keypad as used <a href=\"http:\/\/www.instructables.com\/id\/Arduino-Resistor-less-keypad-access-control\/\">here<\/a> and their code is worth referring to.<\/p>\n<p>The following reads presses from the keypad and prints them over serial. It only returns a valid keypress when a different key is pressed, not when the key is held down.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#define rows 4\r\n#define cols 4\r\n\r\nconst char keymap&#x5B;rows]&#x5B;cols] = {\r\n    { 1, 2, 3, 10 } ,\r\n    { 4, 5, 6, 11 } ,\r\n    { 7, 8, 9, 12 } ,\r\n    {13, 0,14, 15 }\r\n};\r\n\r\nconst int row_pins&#x5B;rows] = { 2, 3, 4, 5 };\r\nconst int col_pins&#x5B;cols] = { 6, 7, 8, 9 };\r\n\r\nvoid setup() {\r\n  Serial.begin(9600);\r\n\r\n  for (int r=0;r&lt;rows;r++) {\r\n    pinMode(row_pins&#x5B;r],INPUT);\r\n    digitalWrite(row_pins&#x5B;r],HIGH);\r\n  }\r\n\r\n  for (int c=0;c&lt;cols;c++) {\r\n    pinMode(col_pins&#x5B;c],OUTPUT);\r\n    digitalWrite(col_pins&#x5B;c],HIGH);\r\n  }\r\n}\r\n\r\nint get_key() {\r\n  int key=-1;\r\n  for(int c=0;c&lt;cols;c++) {\r\n    digitalWrite(col_pins&#x5B;c],LOW);\r\n    for(int r=0;r&lt;rows;r++) {\r\n      if(digitalRead(row_pins&#x5B;r]) == LOW) {\r\n        key = keymap&#x5B;r]&#x5B;c];\r\n        digitalWrite(col_pins&#x5B;c],HIGH);\r\n        return key;\r\n      }\r\n    }\r\n    digitalWrite(col_pins&#x5B;c],HIGH);\r\n  }\r\n  return key;\r\n}\r\n\r\nint last_key=0;\r\n\r\nint get_key_db() {\r\n  int k1 = get_key();\r\n  delay(10);\r\n  int k2 = get_key();\r\n\r\n  if((k1 == k2) &amp;&amp; (k2 != last_key)) {last_key=k2; return k2;}\r\n          else return -1;\r\n}\r\n\r\nvoid loop() {\r\n  int key = get_key_db();\r\n  if(key != -1) {\r\n    Serial.print(&quot;key: &quot;);\r\n    Serial.print(key);\r\n    Serial.print(&quot;\\n&quot;);\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Wired up a cheap keypad to an Arduino. These keypads were a few cents each on ebay and look suitable for a project I&#8217;m working on. This is the same keypad as used here and their code is worth referring to. The following reads presses from the keypad and prints them over serial. It only [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-2053","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-x7","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=2053"}],"version-history":[{"count":2,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2053\/revisions"}],"predecessor-version":[{"id":2057,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/2053\/revisions\/2057"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=2053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=2053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=2053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}