Random Generated Numbers as Password

I’m currently working with random generated numbers which will be sent to my number (using a gsm module) my problem is, whenever I enter the correct random number sent to me it will display invalid pin and it will direct to the first function which will generate a new random number. I hope someone can help me, many thanks :frowning:

This will be my code

#include <Keypad.h> //http://www.arduino.cc/playground/upload … Keypad.zip

#include <SoftwareSerial.h>

#include<EEPROM.h>

SoftwareSerial Sim800l(10,11);

char password[4]=“1234”;

char pass[4],pass1[4];

char customKey=0;

const byte ROWS = 4; // Four rows

const byte COLS = 4; // columns

// Define the Keymap

char hexaKeys[ROWS][COLS] = {

{‘1’,‘2’,‘3’,‘A’},

{‘4’,‘5’,‘6’,‘B’},

{‘7’,‘8’,‘9’,‘C’},

{‘*’,‘0’,‘#’,‘D’}

};

byte rowPins[ROWS] = { 23,25,27,29 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.

byte colPins[COLS] = { 22,24,26,28, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.

// Create the Keypad

Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

uint8_t id = 1;

int i=0;

int c;

int randNumber;

String str_tempPass=“”;

String str_randomPass=“”;

char* text;

char* number;

bool error; //to catch the response of sendSms

void setup(){

Serial.begin(9600);

}

void loop(){

customKey = customKeypad.getKey();

if (customKey)

{

password[i++]=customKey;

Serial.print(“*”);

}

if(i==4)

{

delay(200);

for(int j=0;j<4;j++)

pass[j]=EEPROM.read(j);

if(!(strncmp(password, pass,4)))

{

Serial.println(“Password Accepted”);

c=0;

delay(2000);

//sms sent

str_tempPass=“”;

generatePassword();

//str_tempPass=“Your PIN Code is:” + (str_tempPass);

//char sendto[21]=“+9**********, message[141]=”";

Sim800l.begin(9600);

if (Sim800l.available()){

Serial.println(" ");

Serial.write(Sim800l.read());

}

Serial.println(“Sending Text…”);

Sim800l.print(“AT+CMGF=1\r”); // Set the shield to SMS mode

delay(100);

Sim800l.print(“AT+CMGS="+9**********"\r”);

delay(200);

Sim800l.print(str_tempPass=“Your PIN Code is:” + (str_tempPass));

Sim800l.print(“\r”); //the content of the message

delay(500);

Sim800l.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)

delay(100);

Sim800l.println();

Serial.println(“Text Sent.”);

delay(500);

pCode();

}

else

{

Serial.println(“Access Denied…”);

c++;

Serial.println(c);

i=0;

}

}

}

//GENERATE PIN

void generatePassword()

{

randomSeed(analogRead(0));

for (int i=0; i < 4; i++)

{

randNumber = random(0,9);

str_tempPass = str_tempPass + String(randNumber);

}

Serial.print(str_tempPass);

}

//PINCODE

void pCode()

{

int j=0;

Serial.println(“Enter Pin Code:”);

while(j<4)

{

char key=customKeypad.getKey();

if (key == ‘A’) {

j=0;

i=0;

for(id = 1 ;id <=5; id++)

{

if(EEPROM.read(id)==0)

{

return id;

}

}

if(id>5)

{

return 0;

}

}

else if(key)

{

pass1[j++]=key;

Serial.print(“*”);

}

key=0;

}

delay(500);

if (str_tempPass==str_randomPass)

{

Serial.println(“Pin Success!”);

Serial.println(“Vault Open!”);

}

else if (!(str_tempPass==str_randomPass)){

Serial.println(“Incorrect Pin!”);

}

}

you have too many “password places” and are mixing them up on the different places in the code. Also in pcode() at the end your compare is incorrect:

if (str_tempPass==str_randomPass)
    {
        Serial.println("Pin Success!");
        Serial.println("Vault Open!");
    }
    else if (!(str_tempPass==str_randomPass)){
    Serial.println("Incorrect Pin!");
    }

As you store your answer in pass1 and both str_tempPass and pass1 are a char array you should something like

if(strncmp(str_tempPass, pass1,4) == NULL)
{
        Serial.println("Pin Success!");
        Serial.println("Vault Open!");
}
else
{
       Serial.println("Incorrect Pin!");
}

As you are not returning a value depending on the correctness of the password match AND in loop() you compare different passwords: if(!(strncmp(password, pass,4))) AND ‘i’ is still 4 you go again into generating a new password.

Consider reducing the places where you store passwords & check passwords. Also use a return value from pcode() to make the code in loop() perform the right logic.

regards,

Paul

BTW the next time when you sent code, use the full editor with preview and select the text as code (</>). That makes it much easier to help

Hi Paul!

i tried replacing it with the code you sent and I also did this before but it says that "cannot convert ‘String’ to ‘const char*’ for argument ‘1’ to ‘int strncmp(const char*, const char*, size_t)’ " :frowning:

That is because it is defined it as a string ( String str_tempPass=“”;). Easiest is to change that to a char* as you did with pass1 : char tempPass[4];

In Generatepasswd you then also have to change :

randNumber = random(0,9);
str_tempPass = str_tempPass + String(randNumber); 

to 

tempPass[i]= random(0,9);

AND in loop()

remove the line : str_tempPass="";

change :

Sim800l.print(str_tempPass="Your PIN Code is:" + (str_tempPass));

to

Sim800l.print( "Your PIN Code is: ");
Sim800l.print(str_tempPass);

I have not tested the code but hope I put you in the right direction

regards,

Paul

I could be wrong, but don’t you need to define the character array one element longer than you plan to use, to allow for a terminating null character?

char tempPass[5];

Dan, indeed when performing a print statement it is better to terminate with 0x0.

Thank you Paul and Dan for the help. :slight_smile: