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;*
* } *
}