Advertisement
Brandan

Intel Galileo LCD Sainsmart 2004 and 4x4 Matrix keypad.

Aug 28th, 2014
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.67 KB | None | 0 0
  1. /* Brandan Tyler Lasley @Brandantl 2014 */
  2. /* Last Modifed: Thursday Augest 28 00:32:00
  3.  
  4.  
  5. Pin Specifications:
  6.  
  7. ===== LCD =====
  8. SCL -> ANALOG 5
  9. SDA -> ANALOG 4
  10. VCC -> 5V OUTPUT
  11. GND -> 5V GROUND
  12.  
  13. ==== Matrix Keypad ===
  14. With the keypad face up, the wire nearest to the star key is the first wire then (from left to right) second, third, forth etc.
  15.  
  16. The first wire goes in Digital 9, then the second in 8 and all the way to the 2nd digital pin.
  17. So there should be 8 toal wires connected.
  18.  
  19. Keypad Pin  Connects to Arduino Pin...
  20. 1           D9
  21. 2           D8
  22. 3           D7
  23. 4           D6
  24. 5           D5
  25. 6           D4
  26. 7           D3
  27. 8           D2
  28.  
  29. Now I'm going to explain what each wire repersents so if you wish to free up some digital pins by remving some colums.
  30.  
  31. Wires 1-4 are the rows and 5-8 are the columns. So I'm guessing if you remove Wire #4 and wire #8
  32. then it should disabled A-D and * - D. Haven't tested that though.
  33.  
  34.  
  35. The current code below represents a key lock system where a specific password needed to continue.
  36.  
  37. Almost every key is used, with the exception of ABC.
  38.  
  39. STAR = System Clear
  40. Pound = Enter
  41. D = LCD backlight on/off
  42.  
  43. Modify the code to suit your .need.
  44.  
  45. The libaries required DO NOT come with intels Arudino IDE. You'll need to download Keypad.h and LiquidCrytal-I2C seperatly.
  46. LiquidCrystal_I2C for Sainsmart LCD 2004
  47. http://www.dfrobot.com/wiki/index.php?title=I2C/TWI_LCD1602_Module_(SKU:_DFR0063)
  48. http://playground.arduino.cc/code/Keypad
  49.  
  50. To import these libaries dont unzip them,simpily go to Sketch -> Import Libary and select the zip.
  51.  
  52. Also remember arrays start on 0.
  53. */
  54.  
  55. #include <Keypad.h>
  56. #include <Wire.h>
  57. #include <LiquidCrystal_I2C.h>
  58.  
  59. // Variables
  60. const int  R_Keypad1 = 9; // Row
  61. const int  R_Keypad2 = 8; // Row
  62. const int  R_Keypad3 = 7; // Row
  63. const int  R_Keypad4 = 6; // Row
  64. const int  C_Keypad5 = 5; // Col
  65. const int  C_Keypad6 = 4; // Col
  66. const int  C_Keypad7 = 3; // Col
  67. const int  C_Keypad8 = 2; // Col
  68.  
  69. int LCD_Address = 0x3F;
  70.  
  71. const byte numRows= 4; //number of rows on the keypad
  72. const byte numCols= 4; //number of columns on the keypad
  73.  
  74. //keymap defines the key pressed according to the row and columns just as appears on the keypad
  75. char keymap[numRows][numCols]=
  76. {
  77.   {
  78.     '1', '2', '3', 'A'                  }
  79.   ,
  80.   {
  81.     '4', '5', '6', 'B'                  }
  82.   ,
  83.   {
  84.     '7', '8', '9', 'C'                  }
  85.   ,
  86.   {
  87.     '*', '0', '#', 'D'                  }
  88. };
  89.  
  90. LiquidCrystal_I2C lcd(LCD_Address,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  91. //LiquidCrystal_I2C lcd(i2c_addr, en, rw, rs, d4, d5, d6, d7, BACKLIGHT_PIN, POSITIVE); // Set the LCD I2C address
  92.  
  93. //Code that shows the the keypad connections to the arduino terminals
  94. byte rowPins[numRows] = {
  95.   R_Keypad1,R_Keypad2,R_Keypad3,R_Keypad4}; //Rows 0 to 3
  96. byte colPins[numCols]= {
  97.   C_Keypad5,C_Keypad6,C_Keypad7,C_Keypad8}; //Columns 0 to 3
  98. // Notice, if you're going to remove a wire, remove it from here too and change the numRows and numCols to the appropiate setting.
  99. // You may also want to edit the key map too, test it first though.
  100.  
  101. //initializes an instance of the Keypad class
  102. Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
  103. String password = "";
  104. boolean syslock = false; // Lets us know if the password accecpted/deny screen is active.
  105. boolean lighton = true; // Lets us know the state of our LCD screen backlight on/off. by default program load its on.
  106.  
  107. void setup()
  108. {
  109.   lcd.init();                      // initialize the lcd
  110.   lcd.init();
  111.   lcd.backlight();                // turns on LCD backlight on program boot
  112.   lcd.setCursor(0,1);            // Moves cursor to  position 0 row 1.
  113.   lcd.print ("Pass: ");          // Prints out "Pass: " on position 0, row 1.
  114. }
  115.  
  116. void loop() // You can also do this a more efficent way using event handlers.
  117. {
  118.   char keypressed = myKeypad.getKey(); // Dunno how this works, assume its magic.
  119.  
  120.   if (keypressed == '*') { // If key * is pressed execute the reset system function.
  121.     reset_system();
  122.   }
  123.   else if (keypressed == '#') { // If # is pressed then execute the pound key function.
  124.     pound_key();
  125.   }  
  126.   else if (keypressed == 'D') { // If D turn off/on the LCD backlight.
  127.     if (lighton) {
  128.       lcd.noBacklight(); // Off
  129.       lighton = false;  // Off
  130.     }
  131.     else {
  132.       lighton = true; // On
  133.       lcd.backlight(); // On
  134.     }
  135.   }
  136.   else if (keypressed != NO_KEY) { // If neither of the keys above are pressed, and there is a key pressed.
  137.     if (!syslock) { // Checks if there is an active system lock enganged.
  138.       if (password.length() < 14) { // Makes sure the password is less than 14 chars (LCD screen is small)
  139.         lcd.print('*'); // Print *'s instead of of whats entered.
  140.         password += keypressed; // add the key to the password string.
  141.       }
  142.       else {
  143.         pound_key();// if password exceeds 14 chars then execute the pound key function, which checks and validates the password.
  144.       }
  145.     }
  146.   }
  147. }
  148.  
  149. void pound_key() {
  150.   if (check_password(password)) {
  151.     lcd.clear();                     // Clear LCD screen
  152.     lcd.setCursor(0, 1);             // Set cursor to position 0, line 1
  153.     lcd.print("Password Accecpted!");// Print Text to line 1, position 0.
  154.     lcd.setCursor(0, 2);             // Set cursor to line 2, position 0.
  155.     lcd.print("Press * to reset.");  // Print Text to line 2, position 0.
  156.     syslock = true;                  // engage system lock, this prevents text from being entered on accept/deny scren.
  157.   }
  158.   else {
  159.     lcd.clear();                    // Clear LCD screen
  160.     lcd.setCursor(0, 1);            // Set Cursor to line 1 (starts at 0)
  161.     lcd.print("Access Denied!");    // Print text at position 0, line 1.
  162.     lcd.setCursor(0, 2);            // Set Cursor to line 2
  163.     lcd.print("Press * to reset."); // Print text to line 2 position 0
  164.     syslock = true;                 // engage system lock, this prevents text from being entered on accept/deny scren.
  165.   }
  166. }
  167.  
  168. void reset_system() {
  169.   lcd.setCursor(0,1);         // Set Cursor to line 1 (starts at 0)
  170.   lcd.clear();                // Clear LCD screen
  171.   lcd.print ("Pass: ");       // Print "Pass: " with space starting at position 0 line 1.
  172.   password = "";              // Clear the passwored entered.
  173.   syslock = false;            // Clear Syslock, this prevents text from being entered on the password accept/deny screen.
  174. }
  175.  
  176. boolean check_password(String password) {
  177.   if (password == "6666") {  // check password
  178.     return true;             // If password matches return true.
  179.   }
  180.   else {
  181.     return false;            // else if it doesn't return false.
  182.   }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement