One wire keypad password, Read and string compare

Hey Guys. I am writing a code for the arduino UNO. the idea is for a set of lockers to be opened from a single 4x4 keypad using one wire. There will be three types of passwords, my own and the admin passwords which will open any locker with a single password, and then the students will each be able to assign their own password to any locker they wish and then later open it again.
Now Im not the most experienced programmer and im far from the student codes but so far having trouble reading a complete string and comparing it to my fixed password. A B C and D are used to call the desired password for example D is the one im currently struggling with for myself. B is the admin and will be much the same.

Thank you.

int KP = A0; // keypad input pin
char choice;
int option = 0; //
int number;

int check;
int L; // veriable for selecteted locker. Lock/s
int i; // for the forloop

void setup() {
Serial.begin(9600);
}

char menu() // Using Switch/case to select between myself, admin and students
{
option = analogRead(KP);
if(option>30 && option<=54) // X X X THESE VALUES HAS BEEN CONFIRMED FROM THE KEYPAD TO DETERMINE THE VALUE OF EACH OF THE SELCTION BUTTONS * , A, B, C, D. 1 2 3 …
{
return ‘S’; // Star ‘*’ was pressed, for students to login
}
if(option>800 && option<=1000)
{
return ‘A’;
}
if(option>158 && option<=190)
{
return ‘B’;
}
if(option>90 && option<=99)
{
return ‘C’;
}
if(option>63 && option<=66)
{
return ‘D’;
}

}

char numbers() // Keypad numbers and C to clear if wrong passw.
{
option = analogRead(KP);
if(option>200 && option<=245)
{
return ‘1’;
}
if(option>270 && option<=350)
{
return ‘2’;
}
if(option>400 && option<=600)
{
return ‘3’;
}
if(option>103 && option<=115)
{
return ‘4’;
}
if(option>117 && option<=130)
{
return ‘5’;
}
if(option>135 && option<=155)
{
return ‘6’;
}
if(option>68 && option<=73)
{
return ‘7’;
}
if(option>75 && option<=79)
{
return ‘8’;
}
if(option>81 && option<=88)
{
return ‘9’;
}
if(option>59 && option<=62)
{
return ‘#’;
}
if(option>90 && option<=99)
{
return ‘C’; // C is also copied here, so when C is pressed it would exit using while or if statement
}

}

void loop() {
choice = menu(); //returned symbol picked from menu()
//Serial.println(choice);
switch(choice) // There will be 2 sets of these menus later, one where the locker door is open and another for closed when a opin can not be assigned anymore.
{
case (‘S’):
check = Login();//Login returns the results of the comparison
if(check == 0)//if strcmp returned 0, meaning the comparison was true
{
Serial.println(“You called * for login”);
digitalWrite(L, HIGH); // turn the selected relay for the selected locker high to open
delay(3000);
digitalWrite(L, LOW);
}
break;
if(check != 0)//if strcmp returned 1, meaning the comparison was false
{
}
break;

case (‘A’):Assign(); // A will be used to assign by the students to assign a pin, Ill get there much later
Serial.println(“You called A”);
delay(400);
break;
case (‘B’):check = ADMIN_Login();
if(check == 0)//if strcmp returned 0, meaning the comparison was true
{
digitalWrite(L, HIGH); // turn the selected relay for the selected locker high to open
delay(3000);
digitalWrite(L, LOW);
}
break;
if(check != 0)//if strcmp returned 1, meaning the comparison was false
{
}
break;

case (‘C’):fflush(stdin);
break;
case (‘D’):
check = ME_Login();
if(check == 0)//if strcmp returned 0, meaning the comparison was true
{
Serial.println(“You got the password for D”);
digitalWrite(L, HIGH); // turn the selected relay for the selected locker high to open
delay(3000);
digitalWrite(L, LOW);
}
break;
if(check != 0)//if strcmp returned 1, meaning the comparison was false
{
}
break;

}
}

int Login() // ‘*’ was selected - Not looked into yet.
{

delay(400);
return 0;

if(choice == ‘C’)
{
fflush(stdin);
//Lcd.clear();
return 1;
}
}

int Assign() // ‘A’ was selected - Not looked into yet.
{
// write code to assign pin to any open locker, wont work if already clossed (button pressed. This part can be done with if statement.)
delay(400);
if(choice == ‘C’)
{
fflush(stdin);
//Lcd.clear();
return 1;
}
}

int ADMIN_Login() // ‘B’ was selected - Should work Same as ‘D’
{
int checkin;
char Password[8] = " ";//7 spaces
char CorrectPass[8] = “1010104”; // Admin password. Changed for every buyer
gets(Password); //means that whatever you type is saved by gets inside Password
checkin = strcmp(Password,CorrectPass);/compares the two strings and
returns 0 if they are the same or 1 or -1 for anything else
/
if(checkin==0){ // if paswd corect
return 0;
}
if(checkin!=0){ // if paswd NOT corect
return 1;
}

if(choice == ‘C’)
{
delay(400);
fflush(stdin);
//Lcd.clear();
return 1;
}

}

int ME_Login() // ‘D’ was selected Admin password
{ //*
Serial.println(“You called D”);
delay(400);
int checkin; //*
char Password[9]= " ";
char CorrectPass[9] = “12345678”; // How do I read the inputs and compare them? strcmp doesnt seem to work, neither does getch,
for (i = 1; i < 9; i++) // I also tried Serialread().
{
delay(400);
number = numbers();
//int N = analogRead(KP);
Serial.println(numbers());
//Serial.println(number);
//Serial.println(option);
Password = numbers();

  • //getchar();*
  • }*
    _ // / /_
  • int Password[9];//8 spaces*
  • int CorrectPass[9] = {7,8,7,7,2,1,0,1,}; // My password. Never to change*
  • for (i = 1; i < 9; i++) {*
  • number = numbers();*
    _ Password = number;_
    _ } // */_

* if(strcmp(Password, CorrectPass) == 0)*
* { // if paswd corect*
* return 0;*
* }*
* if(strcmp(Password, CorrectPass) == 1)*
* { // if paswd NOT corect*
* Serial.println(“wrong pass”);*
* return 1;*
* }*
* if(choice == ‘C’)*
* {*
* fflush(stdin);*
* //Lcd.clear();*
* return 1;*
* } *
}

As a teacher, you really can’t expect your students to follow instructions, if you can’t be bothered to. There are stickies at the top of the forum that you are supposed to read BEFORE you blunder in here posting code incorrectly.

if(option>30 && option<=54)   // X X X THESE VALUES HAS BEEN CONFIRMED FROM THE KEYPAD TO DETERMINE THE VALUE OF EACH OF THE SELCTION BUTTONS * , A, B, C, D.  1 2 3 ....
  {
    return 'S';      //  Star '*' was pressed, for students to login
  }
  if(option>800 && option<=1000) 
  {
    return 'A';
  }
  if(option>158 && option<=190)   
  {
    return 'B';
  }
  if(option>90 && option<=99)

How many of those if statements can evaluate to true? If the answer is one, all but the first should be else if statements.

char menu()     //  Using   Switch/case to select between myself, admin and students

This function must ALWAYS return a value, not just when you feel like it.

           case ('S'):

(What) (are) (the) (useless) (parentheses) (for) (?)

int Login()   //  '*' was selected        -      Not looked into yet.
{

    delay(400);
    return 0;


  if(choice == 'C')
  {
    fflush(stdin);
    //Lcd.clear();
    return 1;
  }
}

Stuff your thumb up your ass for a while, then end the function, returning 0. What the heck is that other code for?

fflush()? stdin?

Your PC is NOT doing anything here.

if(check == 0)//if strcmp returned 0, meaning the comparison was true

Nonsense. The function used was NOT strcmp!

break;
                    if(check != 0)//if strcmp returned 1, meaning the comparison was false
                    {
                    }

break; means go away. Stop executing code. Anything after a break statement is a sign of cluelessness.

    gets(Password);   //means that whatever you type is saved by gets inside Password

Whatever you type where? The Arduino does NOT have a keyboard to type on.

    for (i = 1; i < 9; i++)                 // I also tried Serialread(). 
    {
      delay(400);
      number = numbers();
      //int N = analogRead(KP);
      Serial.println(numbers());
      //Serial.println(number);
      //Serial.println(option);
     Password = numbers();

The function name, numbers(), IMPLIES that it will return multiple values. Why are you calling it three times?

Why does it have a plural name when it returns a single value?

Why does it not ALWAYS return a value?

You seem to think that numbers() (and menu()) will wait for a key to be pressed. Hey, I’ve got news for you…

Firstly i am not a teacher im a student myself, I volenteered to help a primary school, also you will notice there are many parts of this code i havent touched such as Login() / case (‘S’): . Assign(); / case (‘A’) or case (‘C’). The selection menu() function and switch/case by the way works just fine. Like I said im not a pro, if I was I probably wouldnt have asked your friendly help! So perhaps if you pulled your thumb out of your own arse you would see I only require help with D “D is the one im currently struggling with” and the problem im having “so far having trouble reading a complete string and comparing it to my fixed password”. Which I didnt just put in there for decoration. Ive added the complete code but here is a summary

I need this to “Read and string compare” and return a 0 if the password is correct.

int ME_Login() // ‘D’ was selected Admin password
{ //*
Serial.println(“You called D”);
delay(400);
int checkin; //*
char Password[9]= " ";
char CorrectPass[9] = “12345678”; // How do I read the inputs and compare them? strcmp doesnt seem to work, neither does getch,
for (i = 1; i < 9; i++) // I also tried Serialread().
{
delay(400);
number = numbers();
//int N = analogRead(KP);
Serial.println(numbers());
//Serial.println(number);
//Serial.println(option);
Password = numbers();

  • //getchar();*

  • }*

  • if(strcmp(Password, CorrectPass) == 0)*

  • { // if paswd corect*

  • return 0;*

  • }*

  • if(strcmp(Password, CorrectPass) == 1)*

  • { // if paswd NOT corect*

  • Serial.println(“wrong pass”);*

  • return 1;*

  • }*

  • if(choice == ‘C’)*

  • {*

  • fflush(stdin);*

  • //Lcd.clear();*

  • return 1;*

  • } *
    }

I need this to "Read and string compare" and return a 0 if the password is correct.

Your (still) improperly posted code makes some unreasonable assumptions.

First, it assumes that the password will always be 8 characters. That is not necessarily so.

Second, it assumes that each time it calls numbers() that it will get a unique keypress. That is NOT so. It MIGHT be if numbers() blocked until there was a key press and then returned that value.

Third, it assumes that array indices start at 1. The do NOT.

Fourth, it assumes that calling numbers() over and over is OK. Since numbers() does not block, that assumption is currently OK. But, three calls could possibly result in three different values, while the code assumes that three calls will always return the same value.

When you fix numbers() so that it blocks until a key is pressed, calling it and storing one out of three key presses is going to lead to disappointment.

The kids won't be using your locker system.

Fifth, the code assumes that strcmp() returns 1 if the strings do not match. It could return -1 or -13 or 22, depending on what the strings all. All that you can reasonably conclude from the value returned by strcmp() is that 0 means that they match and that ANY other value means that they didn't.

Fix these erroneous assumptions, and post your code IN CODE TAGS, and we can talk.

Ok then thank you very much, that clears up a lot already.