Advertisement
Adudewhocreatestuff

Untitled

Jan 16th, 2019
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.91 KB | None | 0 0
  1. -------Code for Arduino/Elegoo Uno R3--------
  2.  
  3. ----------------Custom Keypad-----------------
  4.  
  5. /* @file CustomKeypad.pde
  6. || @version 1.0
  7. || @author Alexander Brevig
  8. || @contact alexanderbrevig@gmail.com
  9. ||
  10. || @description
  11. || | Demonstrates changing the keypad size and key values.
  12. || #
  13. */
  14. #include <Keypad.h>
  15.  
  16. const byte ROWS = 4; //four rows
  17. const byte COLS = 4; //four columns
  18. //define the cymbols on the buttons of the keypads
  19. char hexaKeys[ROWS][COLS] = {
  20. {'1','2','3','A'},
  21. {'4','5','6','B'},
  22. {'7','8','9','C'},
  23. {'*','0','#','D'}
  24. };
  25. byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
  26. byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
  27.  
  28. //initialize an instance of class NewKeypad
  29. Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  30.  
  31. void setup(){
  32. Serial.begin(9600);
  33. }
  34.  
  35. void loop(){
  36. char customKey = customKeypad.getKey();
  37.  
  38. if (customKey){
  39. Serial.println(customKey);
  40. }
  41. }
  42. ------------------------End------------------------
  43.  
  44. ----------------------Keypad.h---------------------
  45.  
  46. /*
  47. ||
  48. || @file Keypad.h
  49. || @version 3.1
  50. || @author Mark Stanley, Alexander Brevig
  51. || @contact mstanley@technologist.com, alexanderbrevig@gmail.com
  52. ||
  53. || @description
  54. || | This library provides a simple interface for using matrix
  55. || | keypads. It supports multiple keypresses while maintaining
  56. || | backwards compatibility with the old single key library.
  57. || | It also supports user selectable pins and definable keymaps.
  58. || #
  59. ||
  60. || @license
  61. || | This library is free software; you can redistribute it and/or
  62. || | modify it under the terms of the GNU Lesser General Public
  63. || | License as published by the Free Software Foundation; version
  64. || | 2.1 of the License.
  65. || |
  66. || | This library is distributed in the hope that it will be useful,
  67. || | but WITHOUT ANY WARRANTY; without even the implied warranty of
  68. || | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  69. || | Lesser General Public License for more details.
  70. || |
  71. || | You should have received a copy of the GNU Lesser General Public
  72. || | License along with this library; if not, write to the Free Software
  73. || | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  74. || #
  75. ||
  76. */
  77.  
  78. #ifndef KEYPAD_H
  79. #define KEYPAD_H
  80.  
  81. #include "utility/Key.h"
  82.  
  83. // Arduino versioning.
  84. #if defined(ARDUINO) && ARDUINO >= 100
  85. #include "Arduino.h"
  86. #else
  87. #include "WProgram.h"
  88. #endif
  89.  
  90. // bperrybap - Thanks for a well reasoned argument and the following macro(s).
  91. // See http://arduino.cc/forum/index.php/topic,142041.msg1069480.html#msg1069480
  92. #ifndef INPUT_PULLUP
  93. #warning "Using pinMode() INPUT_PULLUP AVR emulation"
  94. #define INPUT_PULLUP 0x2
  95. #define pinMode(_pin, _mode) _mypinMode(_pin, _mode)
  96. #define _mypinMode(_pin, _mode) \
  97. do { \
  98. if(_mode == INPUT_PULLUP) \
  99. pinMode(_pin, INPUT); \
  100. digitalWrite(_pin, 1); \
  101. if(_mode != INPUT_PULLUP) \
  102. pinMode(_pin, _mode); \
  103. }while(0)
  104. #endif
  105.  
  106.  
  107. #define OPEN LOW
  108. #define CLOSED HIGH
  109.  
  110. typedef char KeypadEvent;
  111. typedef unsigned int uint;
  112. typedef unsigned long ulong;
  113.  
  114. // Made changes according to this post http://arduino.cc/forum/index.php?topic=58337.0
  115. // by Nick Gammon. Thanks for the input Nick. It actually saved 78 bytes for me. :)
  116. typedef struct {
  117. byte rows;
  118. byte columns;
  119. } KeypadSize;
  120.  
  121. #define LIST_MAX 10 // Max number of keys on the active list.
  122. #define MAPSIZE 10 // MAPSIZE is the number of rows (times 16 columns)
  123. #define makeKeymap(x) ((char*)x)
  124.  
  125.  
  126. //class Keypad : public Key, public HAL_obj {
  127. class Keypad : public Key {
  128. public:
  129.  
  130. Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols);
  131.  
  132. virtual void pin_mode(byte pinNum, byte mode) { pinMode(pinNum, mode); }
  133. virtual void pin_write(byte pinNum, boolean level) { digitalWrite(pinNum, level); }
  134. virtual int pin_read(byte pinNum) { return digitalRead(pinNum); }
  135.  
  136. uint bitMap[MAPSIZE]; // 10 row x 16 column array of bits. Except Due which has 32 columns.
  137. Key key[LIST_MAX];
  138. unsigned long holdTimer;
  139.  
  140. char getKey();
  141. bool getKeys();
  142. KeyState getState();
  143. void begin(char *userKeymap);
  144. bool isPressed(char keyChar);
  145. void setDebounceTime(uint);
  146. void setHoldTime(uint);
  147. void addEventListener(void (*listener)(char));
  148. int findInList(char keyChar);
  149. int findInList(int keyCode);
  150. char waitForKey();
  151. bool keyStateChanged();
  152. byte numKeys();
  153.  
  154. private:
  155. unsigned long startTime;
  156. char *keymap;
  157. byte *rowPins;
  158. byte *columnPins;
  159. KeypadSize sizeKpd;
  160. uint debounceTime;
  161. uint holdTime;
  162. bool single_key;
  163.  
  164. void scanKeys();
  165. bool updateList();
  166. void nextKeyState(byte n, boolean button);
  167. void transitionTo(byte n, KeyState nextState);
  168. void (*keypadEventListener)(char);
  169. };
  170.  
  171. #endif
  172.  
  173. ---------------------------End------------------------
  174.  
  175. --------------------------Key.h-----------------------
  176.  
  177. /*
  178. ||
  179. || @file Key.h
  180. || @version 1.0
  181. || @author Mark Stanley
  182. || @contact mstanley@technologist.com
  183. ||
  184. || @description
  185. || | Key class provides an abstract definition of a key or button
  186. || | and was initially designed to be used in conjunction with a
  187. || | state-machine.
  188. || #
  189. ||
  190. || @license
  191. || | This library is free software; you can redistribute it and/or
  192. || | modify it under the terms of the GNU Lesser General Public
  193. || | License as published by the Free Software Foundation; version
  194. || | 2.1 of the License.
  195. || |
  196. || | This library is distributed in the hope that it will be useful,
  197. || | but WITHOUT ANY WARRANTY; without even the implied warranty of
  198. || | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  199. || | Lesser General Public License for more details.
  200. || |
  201. || | You should have received a copy of the GNU Lesser General Public
  202. || | License along with this library; if not, write to the Free Software
  203. || | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  204. || #
  205. ||
  206. */
  207.  
  208. #ifndef KEY_H
  209. #define KEY_H
  210.  
  211. // Arduino versioning.
  212. #if defined(ARDUINO) && ARDUINO >= 100
  213. #include "Arduino.h" // for digitalRead, digitalWrite, etc
  214. #else
  215. #include "WProgram.h"
  216. #endif
  217.  
  218. #define OPEN LOW
  219. #define CLOSED HIGH
  220.  
  221. typedef unsigned int uint;
  222. typedef enum{ IDLE, PRESSED, HOLD, RELEASED } KeyState;
  223.  
  224. const char NO_KEY = '\0';
  225.  
  226. class Key {
  227. public:
  228. // members
  229. char kchar;
  230. int kcode;
  231. KeyState kstate;
  232. boolean stateChanged;
  233.  
  234. // methods
  235. Key();
  236. Key(char userKeyChar);
  237. void key_update(char userKeyChar, KeyState userState, boolean userStatus);
  238.  
  239. private:
  240.  
  241. };
  242.  
  243. #endif
  244.  
  245. ---------------------------End-------------------
  246.  
  247. -------------------------Key.cpp-----------------
  248.  
  249. /*
  250. || @file Key.cpp
  251. || @version 1.0
  252. || @author Mark Stanley
  253. || @contact mstanley@technologist.com
  254. ||
  255. || @description
  256. || | Key class provides an abstract definition of a key or button
  257. || | and was initially designed to be used in conjunction with a
  258. || | state-machine.
  259. || #
  260. ||
  261. || @license
  262. || | This library is free software; you can redistribute it and/or
  263. || | modify it under the terms of the GNU Lesser General Public
  264. || | License as published by the Free Software Foundation; version
  265. || | 2.1 of the License.
  266. || |
  267. || | This library is distributed in the hope that it will be useful,
  268. || | but WITHOUT ANY WARRANTY; without even the implied warranty of
  269. || | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  270. || | Lesser General Public License for more details.
  271. || |
  272. || | You should have received a copy of the GNU Lesser General Public
  273. || | License along with this library; if not, write to the Free Software
  274. || | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  275. || #
  276. ||
  277. */
  278. #include <Key.h>
  279.  
  280.  
  281. // default constructor
  282. Key::Key() {
  283. kchar = NO_KEY;
  284. kstate = IDLE;
  285. stateChanged = false;
  286. }
  287.  
  288. // constructor
  289. Key::Key(char userKeyChar) {
  290. kchar = userKeyChar;
  291. kcode = -1;
  292. kstate = IDLE;
  293. stateChanged = false;
  294. }
  295.  
  296.  
  297. void Key::key_update (char userKeyChar, KeyState userState, boolean userStatus) {
  298. kchar = userKeyChar;
  299. kstate = userState;
  300. stateChanged = userStatus;
  301.  
  302.  
  303. --------------------End--------------------
  304.  
  305. -----------------Keypad.cpp----------------
  306.  
  307. /*
  308. ||
  309. || @file Keypad.cpp
  310. || @version 3.1
  311. || @author Mark Stanley, Alexander Brevig
  312. || @contact mstanley@technologist.com, alexanderbrevig@gmail.com
  313. ||
  314. || @description
  315. || | This library provides a simple interface for using matrix
  316. || | keypads. It supports multiple keypresses while maintaining
  317. || | backwards compatibility with the old single key library.
  318. || | It also supports user selectable pins and definable keymaps.
  319. || #
  320. ||
  321. || @license
  322. || | This library is free software; you can redistribute it and/or
  323. || | modify it under the terms of the GNU Lesser General Public
  324. || | License as published by the Free Software Foundation; version
  325. || | 2.1 of the License.
  326. || |
  327. || | This library is distributed in the hope that it will be useful,
  328. || | but WITHOUT ANY WARRANTY; without even the implied warranty of
  329. || | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  330. || | Lesser General Public License for more details.
  331. || |
  332. || | You should have received a copy of the GNU Lesser General Public
  333. || | License along with this library; if not, write to the Free Software
  334. || | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  335. || #
  336. ||
  337. */
  338. #include <Keypad.h>
  339.  
  340. // <<constructor>> Allows custom keymap, pin configuration, and keypad sizes.
  341. Keypad::Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols) {
  342. rowPins = row;
  343. columnPins = col;
  344. sizeKpd.rows = numRows;
  345. sizeKpd.columns = numCols;
  346.  
  347. begin(userKeymap);
  348.  
  349. setDebounceTime(10);
  350. setHoldTime(500);
  351. keypadEventListener = 0;
  352.  
  353. startTime = 0;
  354. single_key = false;
  355. }
  356.  
  357. // Let the user define a keymap - assume the same row/column count as defined in constructor
  358. void Keypad::begin(char *userKeymap) {
  359. keymap = userKeymap;
  360. }
  361.  
  362. // Returns a single key only. Retained for backwards compatibility.
  363. char Keypad::getKey() {
  364. single_key = true;
  365.  
  366. if (getKeys() && key[0].stateChanged && (key[0].kstate==PRESSED))
  367. return key[0].kchar;
  368.  
  369. single_key = false;
  370.  
  371. return NO_KEY;
  372. }
  373.  
  374. // Populate the key list.
  375. bool Keypad::getKeys() {
  376. bool keyActivity = false;
  377.  
  378. // Limit how often the keypad is scanned. This makes the loop() run 10 times as fast.
  379. if ( (millis()-startTime)>debounceTime ) {
  380. scanKeys();
  381. keyActivity = updateList();
  382. startTime = millis();
  383. }
  384.  
  385. return keyActivity;
  386. }
  387.  
  388. // Private : Hardware scan
  389. void Keypad::scanKeys() {
  390. // Re-intialize the row pins. Allows sharing these pins with other hardware.
  391. for (byte r=0; r<sizeKpd.rows; r++) {
  392. pin_mode(rowPins[r],INPUT_PULLUP);
  393. }
  394.  
  395. // bitMap stores ALL the keys that are being pressed.
  396. for (byte c=0; c<sizeKpd.columns; c++) {
  397. pin_mode(columnPins[c],OUTPUT);
  398. pin_write(columnPins[c], LOW); // Begin column pulse output.
  399. for (byte r=0; r<sizeKpd.rows; r++) {
  400. bitWrite(bitMap[r], c, !pin_read(rowPins[r])); // keypress is active low so invert to high.
  401. }
  402. // Set pin to high impedance input. Effectively ends column pulse.
  403. pin_write(columnPins[c],HIGH);
  404. pin_mode(columnPins[c],INPUT);
  405. }
  406. }
  407.  
  408. // Manage the list without rearranging the keys. Returns true if any keys on the list changed state.
  409. bool Keypad::updateList() {
  410.  
  411. bool anyActivity = false;
  412.  
  413. // Delete any IDLE keys
  414. for (byte i=0; i<LIST_MAX; i++) {
  415. if (key[i].kstate==IDLE) {
  416. key[i].kchar = NO_KEY;
  417. key[i].kcode = -1;
  418. key[i].stateChanged = false;
  419. }
  420. }
  421.  
  422. // Add new keys to empty slots in the key list.
  423. for (byte r=0; r<sizeKpd.rows; r++) {
  424. for (byte c=0; c<sizeKpd.columns; c++) {
  425. boolean button = bitRead(bitMap[r],c);
  426. char keyChar = keymap[r * sizeKpd.columns + c];
  427. int keyCode = r * sizeKpd.columns + c;
  428. int idx = findInList (keyCode);
  429. // Key is already on the list so set its next state.
  430. if (idx > -1) {
  431. nextKeyState(idx, button);
  432. }
  433. // Key is NOT on the list so add it.
  434. if ((idx == -1) && button) {
  435. for (byte i=0; i<LIST_MAX; i++) {
  436. if (key[i].kchar==NO_KEY) { // Find an empty slot or don't add key to list.
  437. key[i].kchar = keyChar;
  438. key[i].kcode = keyCode;
  439. key[i].kstate = IDLE; // Keys NOT on the list have an initial state of IDLE.
  440. nextKeyState (i, button);
  441. break; // Don't fill all the empty slots with the same key.
  442. }
  443. }
  444. }
  445. }
  446. }
  447.  
  448. // Report if the user changed the state of any key.
  449. for (byte i=0; i<LIST_MAX; i++) {
  450. if (key[i].stateChanged) anyActivity = true;
  451. }
  452.  
  453. return anyActivity;
  454. }
  455.  
  456. // Private
  457. // This function is a state machine but is also used for debouncing the keys.
  458. void Keypad::nextKeyState(byte idx, boolean button) {
  459. key[idx].stateChanged = false;
  460.  
  461. switch (key[idx].kstate) {
  462. case IDLE:
  463. if (button==CLOSED) {
  464. transitionTo (idx, PRESSED);
  465. holdTimer = millis(); } // Get ready for next HOLD state.
  466. break;
  467. case PRESSED:
  468. if ((millis()-holdTimer)>holdTime) // Waiting for a key HOLD...
  469. transitionTo (idx, HOLD);
  470. else if (button==OPEN) // or for a key to be RELEASED.
  471. transitionTo (idx, RELEASED);
  472. break;
  473. case HOLD:
  474. if (button==OPEN)
  475. transitionTo (idx, RELEASED);
  476. break;
  477. case RELEASED:
  478. transitionTo (idx, IDLE);
  479. break;
  480. }
  481. }
  482.  
  483. // New in 2.1
  484. bool Keypad::isPressed(char keyChar) {
  485. for (byte i=0; i<LIST_MAX; i++) {
  486. if ( key[i].kchar == keyChar ) {
  487. if ( (key[i].kstate == PRESSED) && key[i].stateChanged )
  488. return true;
  489. }
  490. }
  491. return false; // Not pressed.
  492. }
  493.  
  494. // Search by character for a key in the list of active keys.
  495. // Returns -1 if not found or the index into the list of active keys.
  496. int Keypad::findInList (char keyChar) {
  497. for (byte i=0; i<LIST_MAX; i++) {
  498. if (key[i].kchar == keyChar) {
  499. return i;
  500. }
  501. }
  502. return -1;
  503. }
  504.  
  505. // Search by code for a key in the list of active keys.
  506. // Returns -1 if not found or the index into the list of active keys.
  507. int Keypad::findInList (int keyCode) {
  508. for (byte i=0; i<LIST_MAX; i++) {
  509. if (key[i].kcode == keyCode) {
  510. return i;
  511. }
  512. }
  513. return -1;
  514. }
  515.  
  516. // New in 2.0
  517. char Keypad::waitForKey() {
  518. char waitKey = NO_KEY;
  519. while( (waitKey = getKey()) == NO_KEY ); // Block everything while waiting for a keypress.
  520. return waitKey;
  521. }
  522.  
  523. // Backwards compatibility function.
  524. KeyState Keypad::getState() {
  525. return key[0].kstate;
  526. }
  527.  
  528. // The end user can test for any changes in state before deciding
  529. // if any variables, etc. needs to be updated in their code.
  530. bool Keypad::keyStateChanged() {
  531. return key[0].stateChanged;
  532. }
  533.  
  534. // The number of keys on the key list, key[LIST_MAX], equals the number
  535. // of bytes in the key list divided by the number of bytes in a Key object.
  536. byte Keypad::numKeys() {
  537. return sizeof(key)/sizeof(Key);
  538. }
  539.  
  540. // Minimum debounceTime is 1 mS. Any lower *will* slow down the loop().
  541. void Keypad::setDebounceTime(uint debounce) {
  542. debounce<1 ? debounceTime=1 : debounceTime=debounce;
  543. }
  544.  
  545. void Keypad::setHoldTime(uint hold) {
  546. holdTime = hold;
  547. }
  548.  
  549. void Keypad::addEventListener(void (*listener)(char)){
  550. keypadEventListener = listener;
  551. }
  552.  
  553. void Keypad::transitionTo(byte idx, KeyState nextState) {
  554. key[idx].kstate = nextState;
  555. key[idx].stateChanged = true;
  556.  
  557. // Sketch used the getKey() function.
  558. // Calls keypadEventListener only when the first key in slot 0 changes state.
  559. if (single_key) {
  560. if ( (keypadEventListener!=NULL) && (idx==0) ) {
  561. keypadEventListener(key[0].kchar);
  562. }
  563. }
  564. // Sketch used the getKeys() function.
  565. // Calls keypadEventListener on any key that changes state.
  566. else {
  567. if (keypadEventListener!=NULL) {
  568. keypadEventListener(key[idx].kchar);
  569. }
  570. }
  571. }
  572.  
  573. --------------------End--------------------
  574.  
  575. ----------------Basic Tutorial-------------
  576.  
  577. 1. Whenever you install the Arduino IDE Make sure you install everything for it.
  578. 2. Whenever your Arduino IDE is finished installing, open up the Arduino Application on your desktop.
  579. 3. Then, make sure you copy the code and paste all of the code in separate pages.
  580. 4. After you create all of the pages of code, and save the files, open up your file manager.
  581. 5. Once in your file manager, open up your drive.
  582. 6. Then go into Program Files (86x)/Arduino/libraries/
  583. 7. Then change the name of the $RJBYH3F folder to, TRJBYH3F
  584. 8. Then go inside of the folder, then line everything up like this: Create a examples and utility folder, then inside of the examples folder, make a CustomKeypad folder. After that put the CustomKeypad.ino code into it. Then go back to the TRJBYH3F folder, and go inside of the utility folder. Then, install these 2 files: http://www.filedropper.com/key_2 , and, http://www.filedropper.com/key_3. Then put those 2 files inside of your utility folder. Then install these 3 files and drop them into the TRJBYH3F folder, http://www.filedropper.com/keypad , and, http://www.filedropper.com/keywords_1 , and, http://www.filedropper.com/keypad_1 .
  585. 9. After you have done all of that, you are set to go for the coding.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement