I would suggest state change detection for the button and use millis() for the 5 second timeout. I felt generous and coded for you but I am not at a place where I can compile and test. Here are references for state change detection and millis() timing.
State Change Detection
Example-code for timing based on millis()
#include "FPS_GT511C3.h"
#include "SoftwareSerial.h"
FPS_GT511C3 fps(D5, D6);
const int TOUCH_BUTTON_PIN = D1; // Input pin for touch state
const int buzzerPin = D2; // Pin number for buzzer
const int Magnet = D7; // Pin number for magnet
void setup()
{
Serial.begin(9600);
delay(100);
fps.Open();
fps.SetLED(false); //turn on LED so fps can see fingerprint
pinMode(TOUCH_BUTTON_PIN, INPUT); // Configure button pin as input
pinMode(buzzerPin, OUTPUT); // Configure button pin as output
}
void loop()
{
static unsigned long prevFingerprintMs;
static bool waitingForFingerprint = false;
static int prevButtonState = LOW;
int buttonState = digitalRead(TOUCH_BUTTON_PIN); // If a touch is detected, turn on the LED
// Check for button press
if (buttonState != prevButtonState)
{
if (buttonState == HIGH)
{
// Button has been pressed. Start 5 second timer, turn on LED, and indicate we are
// waiting for a fingerprint.
Serial.print("Touching");
prevFingerprintMs = millis();
fps.SetLED(true);
waitingForFingerprint = true;
}
prevButtonState = buttonState;
}
if (waitingForFingerprint)
{
if (millis() - prevFingerprintMs < 5000)
{
// Identify fingerprint test
if (fps.IsPressFinger())
{
fps.CaptureFinger(false);
int id = fps.Identify1_N();
if (id < 200) //<- change id value depending model you are using
{ //if the fingerprint matches, provide the matching template ID
Serial.print("Verified ID:");
Serial.println(id);
digitalWrite(Magnet, HIGH); // turn the magnet on
tone(buzzerPin, 262, 1000);
delay(5000); // wait for 5 seconds
digitalWrite(Magnet, LOW); // turn the magnet off
}
else
{ //if unable to recognize
Serial.println("Finger not found");
}
fps.SetLED(false);
waitingForFingerprint = false;
}
else
{
Serial.println("Please press finger");
}
delay(100);
}
else
{
// Timed out waiting for fingerprint
fps.SetLED(false);
waitingForFingerprint = false;
}
}
}