Controlling access to the relay

Now that your fingerprint is stored in the sensor, you can build your first application using the hardware that we previously built. We are going to open or close the relay every time the sensor recognizes our fingerprint. The following is the complete code for this part, excluding the details about recognition functions:

// Libraries
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>

// Function to get fingerprint
int getFingerprintIDez();

// Init Software Serial
SoftwareSerial mySerial(2, 3);

// Fingerprint sensor instance
Adafruit_Fingerprint finger = Adafruit_Fingerprint(andmySerial);

// Relay parameters
int relayPin = 7;
bool relayState = false;

// Your stored finger ID
int fingerID = 0;

// Counters
int activationCounter = 0;
int lastActivation = 0;

void setup() 
{

  // Start Serial
  Serial.begin(9600);

  // Set the data rate for the sensor serial port
  finger.begin(57600);

  // Check if sensor is present
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1);
  }
  Serial.println("Waiting for valid finger...");

  // Set relay as output
  pinMode(relayPin, OUTPUT);
}

void loop()
{
  // Get fingerprint # ID
  int fingerprintID = getFingerprintIDez();

  // Activation ?
  if ( (activationCounter - lastActivation) > 2000) {

  if (fingerprintID == fingerID && relayState == false) {
    relayState = true;
    digitalWrite(relayPin, HIGH);
    lastActivation = millis();
  }
  else if (fingerprintID == fingerID && relayState == true) {
    relayState = false;
    digitalWrite(relayPin, LOW);
    lastActivation = millis();
  }
 
  }
  activationCounter = millis();
  delay(50);           
}

As you can see, many elements are common with the sketch that we saw in the previous section. We are only going to see those elements which are important and are added to this new sketch.

We have to define on which pin the relay is connected and also state that the relay is off by default:

int relayPin = 7;
bool relayState = false;

Then, we will define the ID under which we stored the fingerprint in the previous section. I used 0 here as I stored my fingerprint with the ID number 0:

int fingerID = 0;

Also, we don't want the relay to continuously switch state when we have our finger on the sensor. Therefore, we need the following two variables to count 2 seconds before the state of the relay can be changed again:

int activationCounter = 0;
int lastActivation = 0;

We will then set the relay pin as an output:

pinMode(relayPin, OUTPUT);

Then, in the loop() function of the sketch, we check whether the sensor is reading any fingerprint ID that is already stored in the sensor:

int fingerprintID = getFingerprintIDez();

The following is the check for the activation period:

if ( (activationCounter - lastActivation) > 2000) {

We will then check whether the ID corresponds to the ID that we defined earlier and also check the state of the relay. If the ID corresponds to the ID that we entered in the sketch, we switch the state of the relay:

if (fingerprintID == fingerID && relayState == false) {
     relayState = true;
  digitalWrite(relayPin, HIGH);
  lastActivation = millis();
   }
else if (fingerprintID == fingerID && relayState == true) {
  relayState = false;
  digitalWrite(relayPin, LOW);
  lastActivation = millis();
}

Finally, we will refresh the activation counter and wait 50 milliseconds until the next read:

activationCounter = millis();
delay(50);

It's now time to test the sketch. Get all the code, for example, from the GitHub repository of the book and then upload it to the board. Make sure that you change the ID in the sketch, corresponding to the fingerprint that you stored earlier.

Then, open the serial monitor with the speed of 9600 baud. Place the finger that you recorded previously on the sensor. You should immediately see the following in the serial monitor:

Controlling access to the relay

You should also hear the relay click, meaning that it just changed its state. You can now do the same operation after some seconds; you should hear the relay switch back to its initial state.

You can try with another finger or ask someone else to try the project; nothing will happen at all.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.124