0

I have edited my Code and now the issue is both are working but not together

#include <Key.h>
#include <Keypad.h>
#include <Adafruit_Fingerprint.h>
#include <SD.h>
#include <SPI.h>
#include<SoftwareSerial.h>

SoftwareSerial mySerial(0, 1);
SoftwareSerial mySerials(2, 3);

int read_count = 0, tag_count = 0;
int j = 0, k = 0; // Variabvles to iterate in for loops
char data_temp, RFID_data[12], data_store[12];
boolean disp_control;
char filename[4], fileText[30], names[10];
int atm[5];
uint8_t n, num, p;
char key;
int p1, p2, p3, p4, z;
char fid;

File myFile;
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerials);


const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypads = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
  finger.begin(57600);
  mySerial.begin(9600);
  Serial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");


  Serial.println("Please Place Your Card");

}

void RecieveData()
{

  if (mySerial.available() > 0)
  {
    data_temp = mySerial.read();
    RFID_data[read_count] = data_temp;
    read_count++;
  }
}
void ReadData()
{
  if (read_count == 12) {
    disp_control = true;
    for (j = 0; j < 12; j++) {
      data_store[j] = RFID_data[j];
    }

    int x = 0;
    for (int i = 9; i < 12; i++) {
      filename[x] = data_store[i];
      x++;
    }
    filename[3] = '.';
    filename[4] = 't';
    filename[5] = 'x';
    filename[6] = 't';
    filename[7] = '\0';

    myFile = SD.open(filename, FILE_READ);
    if (myFile) {

      // read from the file until there's nothing else in it:
      while (myFile.available()) {

        for (int i = 0; i < 20; i++) {
          fileText[i] = myFile.read();
        }

        int a, n = 0;
        for (int i = 0; i < 4; i++) {
          atm[a] = fileText[i];
          a++;
        }
        for (int i = 5; fileText[i] != '\0'; i++) {
          names[n] = fileText[i];
          n++;
        }
        fid = fileText[4];

        validate();


        myFile.close();
      }
    } else {
      // if the file didn't open, print an error:
      Serial.println("error opening test.txt");
    }
    read_count = 0;
    tag_count++;

  }
}
void validate() {

  Serial.print("Hello ");
  Serial.print(names);
  Serial.println();
  Serial.println();
  Serial.println();

  pin();

}

void pin() {
  Serial.println("Please Enter Your ATM PIN");


  p1 = keypads.getKey();
  while (p1 == NO_KEY) {
    p1 = keypads.getKey(); //UPDATE VALUE

  }
  Serial.print(p1 - 48);

  p2 = keypads.getKey();
  while (p2 == NO_KEY) {
    p2 = keypads.getKey(); //UPDATE VALUE
  }
  Serial.print(p2 - 48);

  p3 = keypads.getKey(); //UPDATE VALUE
  while (p3 == NO_KEY) {
    p3 = keypads.getKey(); //UPDATE VALUE
  }
  Serial.print(p3 - 48);

  p4 = keypads.getKey(); //UPDATE VALUE
  while (p4 == NO_KEY) {
    p4 = keypads.getKey(); //UPDATE VALUE
  }
  Serial.print(p4 - 48);

  Serial.println();



  if ((p1 == fileText[0]) && (p2 == fileText[1]) && (p3 == fileText[2]) && (p4 == fileText[3])) {
    Serial.println("Please verify Fingerprint");
  }
  else {
    Serial.println("Invalid. Buzzer!!!!");
  }

}
uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

  // OK success!

  p = finger.image2Tz();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

  // OK converted!
  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }

  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID);
  Serial.print(" with confidence of "); Serial.println(finger.confidence);

  return finger.fingerID;
}


int getFingerprintIDez() {


  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.fingerFastSearch();
  if (p != FINGERPRINT_OK)  return -1;


  Serial.print("Found ID #"); Serial.print(finger.fingerID);
  Serial.print(" with confidence of "); Serial.println(finger.confidence);
  return finger.fingerID;

  // found a match!

  /*z = finger.fingerID;
    if ((fid - 48) == z) {
    Serial.println("Success");
    }
    else {
    Serial.println("Fail");
    }*/

}


void loop() {

  RecieveData();
  ReadData();
  getFingerprintIDez();
  delay(50);
}

If I initialize in this format, then fingerprint works:

mySerial.begin(9600);
  finger.begin(57600);
  Serial.begin(9600);

And if i do it this way, then RFID works:

finger.begin(57600);
  mySerial.begin(9600);
  Serial.begin(9600);

I want both of them to work i.e; I first Scan the Card, then verify pin and then it checks the validity of the fingerprint. But the issue is only one of them works at a time. If RFID works, then fingerprint doesn't even blink and if Fingerprint works, RFID doesn't read.

  • Also asked on SO – gre_gor Mar 27 '18 at 18:23
  • Why are you using software serial on hardware serial pins? – gre_gor Mar 27 '18 at 18:23
1

No surprise there. Your function says:

uint8_t p = finger.getImage();
if (p == FINGERPRINT_OK)
  z = finger.fingerID;

So what happens if p isn't FINGERPRINT_OK? This runs:

  if ((fid - 48) == z) {
    Serial.println("Success");
  }
  else {
    Serial.println("Fail");
  }

So z is uninitialized and so probably 0, fid was assigned some value along the way (and it doesn't seem to be 48). So what you get is the expression in the IF condition evaluates to false, 'Fail' is printed and the function returns.

Neglecting the poor structure of your code, magic numbers and all, you've failed to account for the case where there's no finger on the module. Aside from that, you're not even performing a fingerprint search (as I assume you intend); even when someone places their finger, all you're doing is grabbing the image and then reading the default value of fingerID which I believe is 0xffff -- no template conversion or database search. Bottom line, you need to go back and study the original example, which is well commented with self-explanatory method names, before making any changes and writing your own stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.