Arduino programming help needed

Discussion in 'Programmer's Corner' started by Eim, Jun 4, 2013.

  1. Eim

    Thread Starter New Member

    Dec 28, 2012
    5
    0
    Hi all.
    I am almost finished a project with an Arduino Uno interfacing a fingerprint sensor, an LCD and a keypad. When a fingerprint is detected the LCD displays the message access granted and turns on a green led. When a correct password is entered the lcd displays the same message and turns the green led on. If an incorrect password is entered the message access denied is displayed and the red led stays on. Here is my problem. I need the lcd to display a message such as "Enter password or fingerprint" before either the password is entered or before a finger is placed on the sensor. I can get the message to appear on the lcd by using this:

    Code ( (Unknown Language)):
    1.  
    2. lcd.setCursor(0,0);
    3. lcd.print("Enter Password");
    4. lcd.setCursor(0,1);
    5. lcd.print("Or Fingerprint");
    6.  
    What I need is for this message to be displayed, but then the lcd to clear once I go to enter the password or place a finger on the sensor. And also for when I am finished entering either the password or the fingerprint for the original message to appear again. I have tried many different ways but to no avail. Any help it would be much appreciated.
    Here is the code I am using so far.

    Code ( (Unknown Language)):
    1.  
    2. #include <Adafruit_Fingerprint.h>
    3. #include <Password.h>
    4. #include <LiquidCrystal.h>
    5. #include <Keypad.h>
    6. #if ARDUINO >= 100
    7. #include <SoftwareSerial.h>
    8. #else
    9. #include <NewSoftSerial.h>
    10. #endif
    11. LiquidCrystal lcd(10,9,8,7,6,5);
    12.  
    13. int getFingerprintIDez();
    14.  
    15.  
    16. #if ARDUINO >= 100
    17. SoftwareSerial mySerial(2, 3);
    18. #else
    19. NewSoftSerial mySerial(2, 3);
    20. #endif
    21.  
    22. Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
    23.  
    24.  
    25. Password password = Password( "147*" );
    26. const byte ROWS = 4;
    27. const byte COLS = 3;
    28.  
    29. char keys[ROWS][COLS] = {
    30. {'1','2','3'},
    31. {'4','5','6'},
    32. {'7','8','9'},
    33. {'*','0',' '}
    34. };
    35.  
    36. byte rowPins[ROWS] = {13,A0,A1,A2};
    37. byte colPins[COLS] = {A5,A4,A3};
    38. const int buttonPin = 52;
    39. int buttonState = 0;
    40.  
    41.  
    42. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    43.  
    44. #define redLedPin 11
    45. #define greenLedPin 12
    46.  
    47. void setup()
    48. {
    49. Serial.begin(9600);
    50. Serial.println("fingertest");
    51.  
    52.  
    53.  
    54. finger.begin(57600);
    55.  
    56. if (finger.verifyPassword()) {
    57. Serial.println("Found fingerprint sensor!");
    58. } else {
    59. Serial.println("Did not find fingerprint sensor ");
    60. while (1);
    61. }
    62. Serial.println("Waiting for valid finger...");
    63. pinMode(redLedPin, OUTPUT);
    64. pinMode(greenLedPin, OUTPUT);
    65. digitalWrite(redLedPin, LOW);
    66. digitalWrite(greenLedPin, HIGH);
    67.  
    68. lcd.begin(16, 2);
    69. Serial.begin(9600);
    70. keypad.addEventListener(keypadEvent);
    71. keypad.setDebounceTime(250);
    72. }
    73.  
    74. void loop()
    75. {
    76. getFingerprintIDez();
    77. keypad.getKey();
    78. buttonState = digitalRead(buttonPin);
    79. if (buttonState == HIGH) {
    80. lcd.clear();
    81. lcd.setCursor(0, 1);
    82. keypad.getKey();
    83.  
    84. }
    85.  
    86. }
    87.  
    88. uint8_t getFingerprintID() {
    89. uint8_t p = finger.getImage();
    90. switch (p) {
    91. case FINGERPRINT_OK:
    92. Serial.println("Image taken");
    93. break;
    94. case FINGERPRINT_NOFINGER:
    95. Serial.println("No finger detected");
    96. return p;
    97. case FINGERPRINT_PACKETRECIEVEERR:
    98. Serial.println("Communication error");
    99. return p;
    100. case FINGERPRINT_IMAGEFAIL:
    101. Serial.println("Imaging error");
    102. return p;
    103. default:
    104. Serial.println("Unknown error");
    105. return p;
    106. }
    107.  
    108.  
    109.  
    110. p = finger.image2Tz();
    111. switch (p) {
    112. case FINGERPRINT_OK:
    113. Serial.println("Image converted");
    114. break;
    115. case FINGERPRINT_IMAGEMESS:
    116. Serial.println("Image too messy");
    117. return p;
    118. case FINGERPRINT_PACKETRECIEVEERR:
    119. Serial.println("Communication error");
    120. return p;
    121. case FINGERPRINT_FEATUREFAIL:
    122. Serial.println("Could not find fingerprint features");
    123. return p;
    124. case FINGERPRINT_INVALIDIMAGE:
    125. Serial.println("Could not find fingerprint features");
    126. return p;
    127. default:
    128. Serial.println("Unknown error");
    129. return p;
    130. }
    131.  
    132.  
    133. p = finger.fingerFastSearch();
    134. if (p == FINGERPRINT_OK) {
    135. Serial.println("Found a print match!");
    136. } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    137. Serial.println("Communication error");
    138. return p;
    139. } else if (p == FINGERPRINT_NOTFOUND) {
    140. Serial.println("Did not find a match");
    141. return p;
    142. } else {
    143. Serial.println("Unknown error");
    144. return p;
    145. }
    146.  
    147.  
    148. Serial.print("Found ID #"); Serial.print(finger.fingerID);
    149. Serial.print(" with confidence of "); Serial.println(finger.confidence);
    150. }
    151.  
    152.  
    153. int getFingerprintIDez() {
    154. uint8_t p = finger.getImage();
    155. if (p != FINGERPRINT_OK) return -1;
    156.  
    157. p = finger.image2Tz();
    158. if (p != FINGERPRINT_OK) return -1;
    159.  
    160. p = finger.fingerFastSearch();
    161. if (p != FINGERPRINT_OK) return -1;
    162.  
    163.  
    164. digitalWrite(redLedPin, HIGH);
    165. digitalWrite(greenLedPin, LOW);
    166. lcd.print("Access Granted");
    167. delay(3000);
    168. lcd.clear();
    169. digitalWrite(greenLedPin, HIGH);
    170. delay(1000);
    171. digitalWrite(redLedPin, LOW);
    172. Serial.print("Found ID #"); Serial.print(finger.fingerID);
    173. Serial.print(" with confidence of "); Serial.println(finger.confidence);
    174. return finger.fingerID;
    175. }
    176. void keypadEvent(KeypadEvent eKey){
    177. switch (keypad.getState()){
    178. case PRESSED:
    179. lcd.print(eKey);
    180. switch (eKey){
    181. case ' ': guessPassword(); break;
    182. default:
    183. password.append(eKey);
    184. }
    185. }}
    186.  
    187. void guessPassword(){
    188. if (password.evaluate()){
    189. digitalWrite(redLedPin,HIGH);
    190. digitalWrite(greenLedPin,LOW);
    191. lcd.setCursor(0,1);
    192. lcd.print("Access Granted");
    193. password.reset();
    194. delay(2000);
    195.  
    196.  
    197. digitalWrite(greenLedPin, HIGH);
    198. delay(1000);
    199. digitalWrite(redLedPin, LOW);
    200.  
    201. lcd.clear();
    202. }
    203.  
    204.  
    205. else{
    206. digitalWrite(redLedPin,LOW);
    207. lcd.setCursor(0,1);
    208. lcd.print("ACCESS DENIED ");
    209. password.reset();
    210. delay(3000);
    211. lcd.clear();
    212. }
    213. }
    214.  
    Thanks very much.
     
  2. Georacer

    Moderator

    Nov 25, 2009
    5,142
    1,266
    Your loop() function looks pretty empty and illogical.

    You wait for a finger scan and you write the result on the lcd.
    Then you wait for a keystroke. Then you wait for a button press. If you get one, then you wait for another keystroke.

    Moreover, I can't see where you print the initial message on the lcd.

    Also, please use proper code identation when publishing your work. It makes reading it much easier.

    Posting the question in the arduino forum might be a better idea, since more people are familiar with the libraries used. I have to read up Keypad.h in order to give you a definitive answer.
     
  3. Eim

    Thread Starter New Member

    Dec 28, 2012
    5
    0
    Thanks for your advice Georacer. I hadn't added the lcd print message in the code yet. That is why you couldn't see it. Thanks again.
     
Loading...