Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // v 0.1
- #include <Keypad.h>
- #include <EEPROM.h>
- const byte password_length = 4; // max. 511!
- int relay_pin = 13;
- int setkey_pin = 12;
- int tone_pin = A0;
- int time_over = 3000;
- char password[password_length];
- char initial_password[password_length];
- int i=0;
- long defTime;
- char key_pressed=0;
- bool f1=false;
- const byte rows = 4;
- const byte columns = 3;
- char Keys[rows][columns] = {
- {'1','2','3'},
- {'4','5','6'},
- {'7','8','9'},
- {'*','0','#'}
- };
- byte row_pins[rows] = {5,6,7,8};
- byte column_pins[columns] = {2,3,4};
- Keypad keypad_key = Keypad( makeKeymap(Keys), row_pins, column_pins, rows, columns);
- void setup() {
- Serial.begin(115200);
- Serial.println("Start ...");
- if (password_length<2 or password_length>511) {
- Serial.println("Wrong Password length! Terminated program");
- while(1);
- }
- pinMode(tone_pin, OUTPUT);
- pinMode(relay_pin, OUTPUT);
- digitalWrite(relay_pin, LOW);
- pinMode(setkey_pin, INPUT_PULLUP);
- delay(500);
- Serial.println("Enter Password");
- //initialPassword(); // If not remember the password, run this once!
- readPassword();
- tone(tone_pin, 500, 200);
- delay(100);
- tone(tone_pin, 700, 200);
- delay(100);
- tone(tone_pin, 900, 200);
- }
- void loop() {
- if (defTime+time_over<millis() and f1==true) {
- f1=false;
- i=0;
- Serial.println("Time over");
- tone(tone_pin, 400, 200);
- delay(300);
- tone(tone_pin, 400, 200);
- delay(300);
- tone(tone_pin, 400, 200);
- Serial.println("Enter Password");
- }
- key_pressed = keypad_key.getKey();
- if(!digitalRead(setkey_pin)) change();
- if (key_pressed) {
- tone(tone_pin, 1000, 100);
- Serial.println(key_pressed);
- defTime=millis();
- f1=true;
- password[i++]=key_pressed;
- }
- if(i==password_length) {
- f1=false;
- delay(200);
- if(!(strncmp(password, initial_password,4))) {
- Serial.println("Password OK");
- tone(tone_pin, 500, 200);
- delay(100);
- tone(tone_pin, 700, 200);
- delay(100);
- tone(tone_pin, 900, 200);
- delay(100);
- tone(tone_pin, 700, 200);
- delay(100);
- tone(tone_pin, 500, 200);
- digitalWrite(relay_pin, HIGH);
- delay(1000);
- digitalWrite(relay_pin, LOW);
- Serial.println("Enter Password");
- i=0;
- }
- else
- {
- Serial.println("Password Fail");
- tone(tone_pin, 300, 500);
- delay(1000);
- Serial.println("Enter Password");
- i=0;
- }
- }
- }
- void change() {
- Serial.println("Change Password");
- delay(200);
- int j=0;
- while(!digitalRead(setkey_pin)) {
- char key_pressed=keypad_key.getKey();
- if (key_pressed) {
- tone(tone_pin, 1000, 100);
- Serial.println(key_pressed);
- password[i++]=key_pressed;
- if (i==password_length) {
- writePassword();
- i=0;
- Serial.println("Password done.");
- tone(tone_pin, 500, 200);
- delay(100);
- tone(tone_pin, 700, 200);
- delay(100);
- tone(tone_pin, 900, 200);
- }
- }
- }
- Serial.println("While end");
- }
- void initialPassword(){
- for(int j=0;j<password_length;j++) {
- EEPROM.write(j, 1); // The new password many 1 number. e.a. if length=4, the pass=1111.
- }
- }
- void readPassword(){
- for(int j=0;j<password_length;j++) {
- initial_password[j]=EEPROM.read(j);
- }
- }
- void writePassword(){
- for(int j=0;j<password_length;j++) {
- EEPROM.write(j, password[j]);
- initial_password[j]=password[j];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement