I am using a fingerprint sensor and GSM module on Arduino MEGA 2560. I want a fingerprint to be scanned every minute, if a minute elapses with no finger scanned, then the next code for the GSM to send an SMS must be executed enter image description here
const unsigned long timeLimit = 60000;
unsigned long prevTime;
unsigned long currTime;
void setup() {
prevTime = millis();
}
void loop() {
unsigned long currTime = millis();
getFingerprintID(); //fingerprint func
if (currTime - prevTime <= timeLimit) {
if (finger.fingerSearch() == FINGERPRINT_OK) { //if the fingerprint matches
prevTime = currTime;
}
} else {
Serial.print("MAXIMUM TIME OF ");
Serial.print(currTime / 1000);
Serial.print(" SECONDS HAS LAPSED\n");
}
}
-
2Consider looking at this forum.arduino.cc/t/using-millis-for-timing-a-beginners-guide/… What you need to do is create a timer that every 60 seconds executes the code that you want and sets a flag if it was successful and based on that you can do whatever you need with the GSM.Coder_fox– Coder_fox2021年11月14日 13:13:57 +00:00Commented Nov 14, 2021 at 13:13
-
do something like this ... modify the millis code in the comment from @Coder_fox to increment a counter every second ... the millis code does nothing else ...... check for fingerprint, if detected then clear the counter to zero, do nothing else ...... check the counter, if 60 then send SMSjsotola– jsotola2021年11月14日 19:02:01 +00:00Commented Nov 14, 2021 at 19:02
-
@Coder_fox the issue i have now is that the timer starts when i run the code and lapses within 60 seconds regardless of how many times the fingerprint was scanned within the time periodMtho– Mtho2021年11月14日 22:09:57 +00:00Commented Nov 14, 2021 at 22:09
1 Answer 1
i cleaned up your version. make sure to update the prevTime
variable.
const unsigned long timeLimit = 60000;
unsigned long prevTime ;
unsigned long currTime;
unsigned long elapsedTime;
int id;
void setup() {
prevTime=millis();
}
void loop() {
if (fingerprint()){
currTime = millis();
elapsedTime = currTime- prevTime;
if (elapsedTime < 60*1000) {
prevTime = currTime;
} else {
sendSms();
}
}
}
void sendSms() {
Serial.print("MAXIMUM TIME OF ");
Serial.print(currTime/1000);
Serial.print(" SECONDS HAS LAPSED\n");
}
bool fingerprint(){
bool fingerPrintAccepted = false;
if (finger.fingerSearch()==FINGERPRINT_OK){
fingerPrintAccepted = true;
}
return fingerPrintAccepted;
}