40. Password Protected Locker System

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

 

Code

#include <EEPROM.h>

#define EEPROM_SIZE 12

const int Add_init_flag=0;
const int add_master_pin=1;
const int add_user_pin=5;

const String defaultm ="1234";

String currentM="";
String currentU="";

String docserial(){
  while(Serial.available()==0){
    delay(10);
  }
  String input=Serial.readStringUntil('\n');
  input.trim();
  Serial.println(input);
  return input;
}
String docmatkhau(int diachi){
  String matkhau="";
  for(int i=0;i<4;i++){
    int chuso=EEPROM.read(i+diachi);
    matkhau+=String(chuso);
  }
  return matkhau;
}

void matkhaumoi(int diachi,String pinmoi){
  for(int i=0;i<4;i++){
    int chuso=pinmoi.charAt(i)-'0';
    EEPROM.write(i+diachi,chuso);
  }
  EEPROM.commit();
}

void setup() {
  Serial.begin(115200);
  delay(1000);
  if(!EEPROM.begin(EEPROM_SIZE)){
    Serial.println("Failed to initialise EEPROM ");
    return;
  }
  Serial.println("password-protection locker system started!");
  byte initFlag=EEPROM.read(Add_init_flag);
  if(initFlag!=0xAA){
    matkhaumoi(add_master_pin,defaultm);
    matkhaumoi(add_user_pin,"1111");
    EEPROM.write(Add_init_flag,0xAA);
    EEPROM.commit();
  }
  Serial.println("USer PIN already set. Enter your PIN to access");
  currentM=docmatkhau(add_master_pin);
    currentU=docmatkhau(add_user_pin);
}

void loop() {
  Serial.println("menu:");
  Serial.println("1. Enter PIN to Unlock");
  Serial.println("2. Update User PIN");
  Serial.println("3. Reset PIN with Master PIN");
  Serial.print("Choose an option (1-3): ");
  String choice = docserial();
  if(choice=="1"){
    Serial.println(" Enter your 4 digit PIN to unlock: ");
    String enterPIN= docserial();
   if(enterPIN==currentU){
    Serial.println("access granted!");
   }else{
    Serial.println("who are you mf");
   }
  }else if(choice=="2"){
    Serial.print("enter your current PIN: ");
    String currentverify=docserial();
    if(currentverify==currentU){
      Serial.print("enter your new PIN: ");
      String newpin=docserial();
      matkhaumoi(add_user_pin,newpin);
      currentU=newpin;
      Serial.println("user pin updated successfully");

    }else{
      Serial.println("who tf are you?");
    }
  }else if(choice=="3"){
    Serial.println("Enter master pin to reset user pin: ");
    String enterm=docserial();
    if(enterm==currentM){
      Serial.print("enter a new 4 digit PIN: ");
      String newpin=docserial();

      matkhaumoi(add_user_pin,newpin);
      currentU=newpin;
      Serial.println("your PIN has changed successfully!");

    }else{
      Serial.println("Wrong PIN bro!");
    }

  }
  else{
    Serial.println("You don't have choices");
  }
  delay(500);

}

 

Output

Video

Add a video of the output (know more)

 

 

#include <EEPROM.h>


 

#define EEPROM_SIZE 12


 

const int Add_init_flag=0;

const int add_master_pin=1;

const int add_user_pin=5;


 

const String defaultm ="1234";


 

String currentM="";

String currentU="";


 

String docserial(){

  while(Serial.available()==0){

    delay(10);

  }

  String input=Serial.readStringUntil('\n');

  input.trim();

  Serial.println(input);

  return input;

}

String docmatkhau(int diachi){

  String matkhau="";

  for(int i=0;i<4;i++){

    int chuso=EEPROM.read(i+diachi);

    matkhau+=String(chuso);

  }

  return matkhau;

}


 

void matkhaumoi(int diachi,String pinmoi){

  for(int i=0;i<4;i++){

    int chuso=pinmoi.charAt(i)-'0';

    EEPROM.write(i+diachi,chuso);

  }

  EEPROM.commit();

}


 

void setup() {

  Serial.begin(115200);

  delay(1000);

  if(!EEPROM.begin(EEPROM_SIZE)){

    Serial.println("Failed to initialise EEPROM ");

    return;

  }

  Serial.println("password-protection locker system started!");

  byte initFlag=EEPROM.read(Add_init_flag);

  if(initFlag!=0xAA){

    matkhaumoi(add_master_pin,defaultm);

    matkhaumoi(add_user_pin,"1111");

    EEPROM.write(Add_init_flag,0xAA);

    EEPROM.commit();

  }

  Serial.println("USer PIN already set. Enter your PIN to access");

  currentM=docmatkhau(add_master_pin);

    currentU=docmatkhau(add_user_pin);

}


 

void loop() {

  Serial.println("menu:");

  Serial.println("1. Enter PIN to Unlock");

  Serial.println("2. Update User PIN");

  Serial.println("3. Reset PIN with Master PIN");

  Serial.print("Choose an option (1-3): ");

  String choice = docserial();

  if(choice=="1"){

    Serial.println(" Enter your 4 digit PIN to unlock: ");

    String enterPIN= docserial();

   if(enterPIN==currentU){

    Serial.println("access granted!");

   }else{

    Serial.println("who are you mf");

   }

  }else if(choice=="2"){

    Serial.print("enter your current PIN: ");

    String currentverify=docserial();

    if(currentverify==currentU){

      Serial.print("enter your new PIN: ");

      String newpin=docserial();

      matkhaumoi(add_user_pin,newpin);

      currentU=newpin;

      Serial.println("user pin updated successfully");


 

    }else{

      Serial.println("who tf are you?");

    }

  }else if(choice=="3"){

    Serial.println("Enter master pin to reset user pin: ");

    String enterm=docserial();

    if(enterm==currentM){

      Serial.print("enter a new 4 digit PIN: ");

      String newpin=docserial();


 

      matkhaumoi(add_user_pin,newpin);

      currentU=newpin;

      Serial.println("your PIN has changed successfully!");


 

    }else{

      Serial.println("Wrong PIN bro!");

    }


 

  }

  else{

    Serial.println("You don't have choices");

  }

  delay(500);


 

}


 

 

 

Photo of Output

Add a photo of your hardware showing the output.

 

 

 

 

 

Screenshot of serial Terminal 

Add a Screenshot of the serial terminal showing the output.


 

Was this helpful?
Upvote
Downvote