Hey guys,
I have already posted in this forum about the same problem I had with a piece of my program which needs to send a random 4 digit otp to my mobile number and then I input the otp to my keypad and then it prints access granted or denied if entered right or wrong. Many of the users helped me with the code but then it doesn't send the random number.
Here's the code:
#include <LiquidCrystal.h>
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <Keypad.h>
const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int getFingerprintIDez();
SoftwareSerial mySerial(10, 11);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
byte rowPins[ROWS] = { 33, 32, 31, 30 };
byte colPins[COLS] = { 36, 35, 34 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char* storedPassword = "5432" ;
void setup()
{
// For Yun/Leo/Micro/Zero/...
Serial.begin(9600); // set baud rate for serial monitor
lcd.begin(16, 2);
Serial.println("Adafruit finger detect test");
// 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...");
Serial1.begin(9600);
lcd.print("Place the finger");
lcd.clear();
}
void loop()
{
getFingerprintIDez();
delay(50);
if (Serial.available()) {
Serial1.write(Serial.read());
}
if (Serial1.available()) {
Serial.write(Serial1.read());
}
if (char* enteredPassword = lookForPassword(keypad))
{
Serial.println(enteredPassword);
if (strcmp(enteredPassword, storedPassword) == 0)
{
Serial.println(F("ACCESS GRANTED"));
lcd.clear();
lcd.print(F("ACCESS GRANTED"));
}
else
{
Serial.println(F("ACCESS DENIED"));
lcd.clear();
lcd.print(F("ACCESS DENIED"));
}
}
}
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;
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
Serial1.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
Serial1.println("AT+CMGS=\"+91xxxxxxxxxx\"\r"); // Replace x with mobile number
delay(1000);
Serial1.println(storedPassword);// The SMS text you want to send
delay(100);
Serial1.println((char)26);// ASCII code of CTRL+Z
delay(1000);
Serial.println("ENTER OTP");
lcd.print(("ENTER OTP"));
return finger.fingerID;
}
char* lookForPassword(Keypad& kpd)
{
static char password[5];
static int idx = 0;
if(char key = kpd.getKey())
{
if (key == '#')
{
idx = 0;
return password;
}
if (isDigit(key))
{
password[idx++] = key;
password[idx] = '\0';
if (idx > 3)
{
idx = 0;
return password;
}
}
lcd.setCursor(1,0);
lcd.print(key);
}
return nullptr;
}
How do I put the random() and randomSeed() function in here?