im trying to make a time based door lock with security code

Hi guys, Newbie here… :smiley: this is for my project, I would appreciate your help :slight_smile: sorry for my bad english

Im trying to make an automatic door lock, which will activate at a specified time and also it can be override by a security code…
example my door lock is set to open at 8:30am and will lock at 10am, but if a professor would like to enter the classroom earlier than 8:30am, he will enter a code to open it…
I already paste the security code program, I found it in the internet
so I need help in adding a time based code in my current code,
Im really really sorry for my newbieness :frowning:

(Here’s my current program for the security lock)

#include <Password.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
LiquidCrystal lcd(2,3,4,9,10,11,12);
Password password = Password( “1234” );

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’,’ ',}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {25, 24, 23, 22}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {28, 27, 26}; //connect to the column pinouts of the keypad
const int buttonPin = 7;
int buttonState = 0;

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledPin 13

void setup(){

pinMode(buttonPin, INPUT);
lcd.begin(16, 2);
digitalWrite(ledPin, LOW); // sets the LED on
Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
keypad.setDebounceTime(250);

}

void loop()

{
keypad.getKey();
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
lcd.clear();
}
}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
lcd.print(eKey);
switch (eKey){
case ’ ': guessPassword(); break;
default:
password.append(eKey);

}
}
}
void guessPassword(){
if (password.evaluate()){
digitalWrite(ledPin,HIGH); //activates garaged door relay
delay(500);
digitalWrite(ledPin,LOW); //turns off door relay after .5 sec
lcd.begin (16,2);
lcd.print("VALID PASSWORD "); //
password.reset(); //resets password after correct entry
delay(600);
lcd.begin(16, 2);

lcd.print("Welcome ");

delay(2000);
lcd.clear();
delay(2000);

lcd.print(“Hello”);
lcd.setCursor (0,1);
}

else{
digitalWrite(ledPin,LOW);
lcd.setCursor (0,1);
lcd.print("INVALID PASSWORD ");
password.reset(); //resets password after INCORRECT entry
delay(600);
lcd.clear();
lcd.print(“Hello”);
lcd.setCursor (0,1);
}

}

I already finished the security code program, I found it in the internet

"I finished my term paper by copying it off the internet" is equivalent to your claim.

int seconds()
{
    s = initialHours*3600;
    s = s+(initialMins*60);
    s = s+initialSecs;
    s = s+(millis()/1000);
    return s;
}

Why does this function need to return a global variable?

keypad.getKey();

Get the key that is pressed, if any, but ignore it. So, why bother?

case ' ': guessPassword(); break;

There's no f**king guessing going on.

so I need help in adding a time based code in my current code,

I'm sure that there is some stuff on the internet you can steal, and claim you wrote.

my apologies sir, ill correct my mistakes..

PaulS:

I already finished the security code program, I found it in the internet

"I finished my term paper by copying it off the internet" is equivalent to your claim.

case ' ': guessPassword(); break;

There's no f**king guessing going on.

so I need help in adding a time based code in my current code,

I'm sure that there is some stuff on the internet you can steal, and claim you wrote.

PaulS, you are obviously proficient in the C language. But why insult someone who is a newbie?

Most of the users on this forum have used someone else's code in their sketch at some time. I certainly have and I am most grateful to those who have made their knowledge available.

The learning in the exercise is putting all the basics together in their particular sketch and making it work.

Weedpharma

@itsmepelex,

Have a look at this Thread about Planning and Implementing a Program. It should help you to think about how you will get the different pieces to fit together.

I think you will need a Real Time Clock (RTC) module to keep track of time.

Try the different parts of what you want to do in separate short sketches until you understand exactly how each piece works. When you do, you can start to join the parts together.

...R

But why insult someone who is a newbie?

I never insult anyone who says “I found this code on the internet; it does almost what I want”. Where I get annoyed is someone doing something that is almost certainly a school project getting code from the internet and then claiming they wrote it.

Function names should CLEARLY state what the function does. There is no doubt in anyone’s mind what digitalRead(), analogRead(), or digitalWrite() does. There is some confusion about what analogWrite() does, because it doesn’t work on all digital pins, and doesn’t work on any analog pins.

guessPassword()? There is no guessing going on. The user entered the correct password or they didn’t. The function checks that AND does some other stuff. It should NOT do anything by checkPassword(). And, it should NOT set a global variable to indicate true or false. It should return a value.

PaulS: it should NOT set a global variable to indicate true or false. It should return a value.

I don't see anything wrong with using global variables to record the state of something. A more meaningful name than 's' would be a good idea, however.

But I agree that a function should not RETURN a global variable (it isn't necessary) and if the function must return a value it should return a value from a local variable.

I don't see anything wrong with using global variables to record the state of something.

I think we'll have to agree to disagree.

Should checkPassword() set a global? Or should it return a value? I don't like the use of global variables because it is too easy to inadvertently repurpose a global variable. The value that checkPassword() returns is only of interest to the function that calls it.

Now, that function might set the global variable validUser to true or false. But, checkPassword shouldn't. In my opinion.

Thank you all for the response, i appreciate it, sorry for the trouble..

itsmepelex: Thank you all for the response, i appreciate it, sorry for the trouble..

No trouble. Nothing to apologize for.

...R