i am working on a fingerprint doorlock.
im using a pushbutton for in-house lock and open
im using counter to lock and open, if the finger valid or the button pressed it will increment the counter.
if even it locked, if odd its opened
- the problem is, im editing the library code from adafruit. im adding a state change detection for the push button to increment the counter.
the state change detection must be in the void loop right?
for now im able to do the counter correctly. But when i put the if-else for the even and odd (output) in the void loop, the relay loops too.
where should i put the if-else for the output in the program
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#define lock 11
#define openn 10
#define alarm 9
#define sw 7
int getFingerprintIDez();
boolean pinState;
bool executed = false;
int cnt = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino (WHITE wire)
SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
void setup()
{
while (!Serial); // For Yun/Leo/Micro/Zero/...
Serial.begin(9600);
Serial.println("Adafruit finger detect test");
pinMode(lock, OUTPUT);
digitalWrite(lock, LOW);
pinMode(openn, OUTPUT);
digitalWrite(openn, LOW);
pinMode(alarm, OUTPUT);
digitalWrite(alarm, HIGH);
pinState = digitalRead(lock);
pinMode(12, OUTPUT);
// set the data rate for the sensor serial port
finger.begin(57600);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1);
}
Serial.println("Waiting for valid finger...");
}
void loop() // run over and over again
{
getFingerprintIDez();
digitalWrite(12, HIGH);
//don't ned to run this at full speed.
{
buttonState = digitalRead(sw); //////////STATE CHANGE FOR THE PUSH BUTTON TO INCREMENT THE COUNTER
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
cnt++;
Serial.println("Tombol dalam ditekan");
Serial.print("Counter: ");
Serial.println(cnt);
}
// Delay a little bit to avoid bouncing
delay(100);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
}
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);
}
// returns -1 if failed, otherwise returns ID #
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;
cnt++; /// TO INCREMENT THE COUNTER IF FINGER IS MATCH
// found a match!
digitalWrite(12, LOW);
delay(600);
digitalWrite(12, HIGH);
if (cnt % 2 == 0) {
digitalWrite(lock, HIGH);
delay(300);
digitalWrite(lock, LOW);
}
else {
digitalWrite(openn, HIGH);
delay(300);
digitalWrite(openn, LOW);
}
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
Serial.print("Counter: "); Serial.println(cnt);
return finger.fingerID;
}