School project please help!

Hello
im trying to make a locker system with 2 different passwords
the code works with 1 password but i doesnt seem to be working with 2 different passwords anybody can help me ?

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Servo.h>

#define Password_Length 5

Servo myservo;
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
int pos =0;

char Data[Password_Length];
char Master_1[Password_Length] ="1234";
char Master_2[Pasweord_Length] ="4321";
byte data_count = 0, master_count = 0;

bool Pass_is_good;
bool door = false;

char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char keys [ROWS][COLS] ={
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'},
};

byte rowPins[ROWS] = {0,1,2,3};
byte colPins[COLS] = {4,5,6,7};
Keypad customKeypad(makeKeymap(keys), rowPins,colPins, ROWS, COLS);

void setup() {
  myservo.attach(9, 2000, 2400);
  ServoClose();

  lcd.begin(16, 2);
  lcd.print("protected Locker");
  loading("Loading");
  //clean
  lcd.clear();
}

void loop() {
  if (door == true)
  {customKey = customKeypad.getKey();
  if (customKey == '#')
  {lcd.clear();
  ServoClose();
  lcd.print("Locker Closed");
  delay (3000);
  door = false;
    }
  }
  else open();
}

 void loading (char msg[])
 {lcd.setCursor(0, 1);
 lcd.print(msg);
 
 for (int i = 0; i<9; i++) {
  delay(1000);
  lcd.print(".");
    }
 }
 void clearData()
 {while (data_count != 0)
 {Data[data_count--] = 0;}
 return;
 }

void ServoClose()
{for (pos = 90; pos>= 0; pos -=10)
  {myservo.write(pos);}
}

void ServoOpen()
{for (pos = 0; pos <= 90; pos +=10)
  {myservo.write(pos);}
}

void open()
{
  lcd.setCursor(0, 0);
  lcd.print("Enter Password");
  customKey = customKeypad.getKey();
  if (customKey)
  {
    Data[data_count] = customKey;
    lcd.setCursor(data_count, 1);
    lcd.print(Data[data_count]);
    data_count++;
  }
  if (data_count == Password_Length - 1)
  {
    if(!strcmp(Data, Master_1) || !strcmp(Data, Master_2))
    {
      lcd.clear();
      ServoOpen();
      lcd.print("KEEP WALKING!");
      door = true;
      delay(5000);
      loading("Wait");
      lcd.clear();
      lcd.print("Time out!");
      delay(1000);
      ServoClose();
      door = false;
    }
    else
    {
      lcd.clear();
      lcd.print("Go away");
      door = false;
    }
    delay(1000);
    lcd.clear();
    clearData();
  }
}

Welcome to the forum

It is not a good idea to use pins 0 and 1 on most Arduinos because they are used by the Serial interface

Have you tried printing customKey to see which character you are actually receiving ?

The code is suspiciously similar to the code in Password protected system

Are you guys in the same class?

yes sir, it's a group project we are working on actually

it says that the second password which is char master_2 is not defined we've been trying sins morning and cant figure how to fix it

Let's do one thing that is going to help you immensely in the future. You've seen code written by the pros where the blocks all line up nice and neat and the braces are on lines by themselves. We don't do that just to make it pretty. It actually serves a purpose. It's very helpful when you are looking at code to do debugging if you can clearly see what blocks are nested inside of what other blocks.

Open your code up in the Arduino IDE. Get all the braces { and } so that they're on lines by themselves. Then hit Control-T and it will autoformat your code. You will find it easier to read and so will the folks helping you.

1 Like

sorry sir we are totally new here we wanted to delete the old topic but fore some reason we cant so please ignore the old topic

we changed some plans we this topic is the new version of the project
we are trying to add 2 different passwords instead changing it.

For me the code that you posted has an error in this line

char Master_2[Pasweord_Length] = "4321";

Compilation error: 'Pasweord_Length' was not declared in this scope

Have you posted the right code ?

IN general you should keep to one topic. It's easier if all the information and all the changes are in one place.

yes its the exact problem we are facing rn
if you delete the
´´´
char Master_2[Pasweord_Length] = "4321";
|| !strcmp(Data, Master_2))
´´´
the code should work normally

i tried to do that but it didnt change

It didn't reformat the code so it looks nicer?

You must have done it wrong. When I open the code up in Arduino and hit control-t it autoformats it and makes it all where we can read it easier.

it looks almost the same

Post it then. I think you may just not realize what a big difference those small changes make.

this is how it look like now

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Servo.h>

#define Password_Length 5

Servo myservo;
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
int pos = 0;

char Data[Password_Length];
char Master[Password_Length] = "1234";
char Master_1[Pasweord_Length] = "4321";
byte data_count = 0, master_count = 0;

bool Pass_is_good;
bool door = false;

char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' },
};

byte rowPins[ROWS] = { 0, 1, 2, 3 };
byte colPins[COLS] = { 4, 5, 6, 7 };
Keypad customKeypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  myservo.attach(9, 2000, 2400);
  ServoClose();

  lcd.begin(16, 2);
  lcd.print("protected Locker");
  loading("Loading");
  //clean
  lcd.clear();
}

void loop() {
  if (door == true) {
    customKey = customKeypad.getKey();
    if (customKey == '#') {
      lcd.clear();
      ServoClose();
      lcd.print("Locker Closed");
      delay(500);
      door = false;
    }
  } else open();
}

void loading(char msg[]) {
  lcd.setCursor(0, 1);
  lcd.print(msg);

  for (int i = 0; i < 9; i++) {
    delay(500);
    lcd.print(".");
  }
}
void clearData() {
  while (data_count != 0) { Data[data_count--] = 0; }
  return;
}

void ServoClose() {
  for (pos = 90; pos >= 0; pos -= 10) { myservo.write(pos); }
}

void ServoOpen() {
  for (pos = 0; pos <= 90; pos += 10) { myservo.write(pos); }
}

void open() {
  lcd.setCursor(0, 0);
  lcd.print("Enter Password");
  customKey = customKeypad.getKey();
  if (customKey) {
    Data[data_count] = customKey;
    lcd.setCursor(data_count, 1);
    lcd.print(Data[data_count]);
    data_count++;
  }
  if (data_count == Password_Length - 1) {
    if (!strcmp(Data, Master) || !strcmp(Data, Master_1)) {
      lcd.clear();
      ServoOpen();
      lcd.print("KEEP WALKING!");
      door = true;
      delay(5000);
      loading("Wait");
      lcd.clear();
      lcd.print("Time out!");
      delay(1000);
      ServoClose();
      door = false;
    } else {
      lcd.clear();
      lcd.print("Go_away");
      door = false;
    }
    delay(1000);
    lcd.clear();
    clearData();
  }
}

Yes, that is much easier to see. See if you can understand the difference. It will help you immensely in the future. It is SO much easier to read through code that is formatted.

For example, if I want to know what lines are controlled by this if statement then I have to hunt in there and find the braces that match.

Written this way I can easily see that it's all the indented lines and I don't have to search for the braces.

thank you very much for the tip but i still dont know how to fix my code :smiley:

char Master_1[Pasweord_Length] = "4321";`

Fix your typos so your code at least will compile.

1 Like

oh my god thank you very very very much sir i didnt realy notice that the code now is working :,)
i didnt expect that the whole problem would be a typo thank you everyone for the time.

You need to understand that not all steps lead to immediate satisfaction. In this case formatting code will help you find errors more easily. Or will help your helpers find them. It doesn't fix the problem it makes the problem easier to find.

Not everything is about getting immediate results. Have patience.

1 Like