Save to Arduino's EEPROM Memory

0

I want to do a Smartphone-controlled lock design. Basically, the user has the option to change the password by the Keypad, but I want the new password (String) to be saved in the Internal Memory of the Arduino (EEPROM), but I'm picking it here because it is a string ... If they have a solution or any help is enough. Follow the code below.

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

 /*-------------------------------KEYPAD---------------------------------------*/
 const byte numRows= 4; //number of rows on the keypad
 const byte numCols= 4; //number of columns on the keypad
 char keypressed;
 char keymap[numRows][numCols]=
       {
       {'1', '2', '3', 'A'},
       {'4', '5', '6', 'B'},
       {'7', '8', '9', 'C'},
       {'*', '0', '#', 'D'}
  };
 byte rowPins[numRows] = {13,12,11,10};//linhas 0 até 3
 byte colPins[numCols] = {9,8,7,6};//Columas 0 até 3 
 Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

 /*-------------------------------VARIAVEIS------------------------------------*/

 int rele = 2;
 LiquidCrystal lcd (A0,A1,A2,A3,A4,A5);
 String password="2017"; //SENHA ATUAL
 String tempPassword=""; //VARIAVEL PARA GUARDAR NOVA SENHA
 int doublecheck;    //VERIFICAR 2 VEZES A NOVA SENHA
 boolean armed = false;  //ESTADO ATUAL(ARMADA :TRUE / DESARMADA:false)
 boolean input_pass;   //SENHA (CORRETA:true / ERRADA:false)
 boolean storedPassword = true;
 boolean changedPassword = false;
 boolean checkPassword = false;
 int i = 1; //VARIAVEL PARA CONTROLE DO VETOR
 /*----------------------------------------------------------------------------*/

 void setup(){
   Serial.begin(9600);
   lcd.begin(16, 2);
   pinMode(rele, OUTPUT);
   digitalWrite(rele, LOW);
   lcd.setCursor(0,0);
   lcd.print("PROJETO PI");
   lcd.setCursor(0,1);
   lcd.print("   LOCKPICK  ");
   delay(2000);
   lcd.clear();

 }

 void loop() { //LOOP PRINCIPAL
   unlockTheDoor();
 }
      /********************************FUNCÕES*************************************/

 void unlockTheDoor(){
   lockAgain: //goto label
   tempPassword="";
   i=6;
   digitalWrite(rele, LOW);
   lcd.clear();
   while(!checkPassword){
     lcd.setCursor(0,0);
     lcd.print("ABRA A PORTA  ");
     lcd.setCursor(0,1);
     lcd.print("SENHA>");
     keypressed = myKeypad.getKey();   //Ler as teclas precionadas
     if (keypressed != NO_KEY){    //Accept only numbers and * from keypad
  if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
  keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
  keypressed == '8' || keypressed == '9' ){
         tempPassword += keypressed;
         lcd.setCursor(i,1);
         Serial.println("*");
         lcd.print("*"); 
    i++;  
  }
  else if (keypressed == 'A'){
    //Serial.println("MUDAR SENHA");
    lcd.clear();
      lcd.setCursor(0, 0);
     lcd.print("MUDAR SENHA"); 
     delay(1000);
    changePassword();
    goto lockAgain;
  }
  else if (keypressed=='#'){
    lcd.clear();
    break;
  }
  else if (keypressed == '*'){ 
    //CONFIRIR SENHA
    if (password==tempPassword){//SE FOR CORRETA...
      lcd.clear();      
      lcd.setCursor(0,0);
      lcd.print("SENHA CORRETA");
      lcd.setCursor(0,1);
      lcd.print("PORTA DESBLOQUEADA");
      Serial.println("opened");
      digitalWrite(rele, HIGH);//CODIGO DA PORTA AQUI
      delay(8000);
      goto lockAgain;
    }
    else{           //CASO FALSO, TENTE NOVAMENTE
      tempPassword="";
      Serial.println("SENHA INCORRETA");
      delay(5000);
      goto lockAgain;
    }
  }
}
   }
 }     

 //Change current password
 void changePassword(){
   retry: //label for goto
   tempPassword="";
   lcd.clear();
   i=1;
   while(!changedPassword){        //AGUARDANDO PELA SENHA
keypressed = myKeypad.getKey();
lcd.setCursor(0,0);
lcd.print("SENHA ATUAL");
lcd.setCursor(0,1);
lcd.print(">");
if (keypressed != NO_KEY){
  if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
  keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
  keypressed == '8' || keypressed == '9' ){
    tempPassword += keypressed;
    lcd.setCursor(i,1);
    lcd.print("*");
    i++;     
  }
  else if (keypressed=='#'){
    break;
  }
  else if (keypressed == '*'){
    i=1;
    if (password==tempPassword){
      lcd.clear();
      lcd.print("Senha Confirmada");
      delay(1000);
      lcd.clear();
      storedPassword=false;
      newPassword();          //Se a senha estiver certa, chama a função newPassword
      break;
    }
    else{               //tente denovo
      tempPassword="";
      goto retry;
         }
       }
     }
   }
 }
 String firstpass;
 //DENIFIR NOVA SENHA
 void newPassword(){
   tempPassword="";
   changedPassword=false;
   lcd.clear();
   i=1;
   while(!storedPassword){
     keypressed = myKeypad.getKey();   //LER AS TECLAS
     if (doublecheck==0){
       lcd.setCursor(0,0);
       lcd.print("INSIRA NOVA SENHA");
  lcd.setCursor(0,1);
  lcd.print(">");
}
else{
  lcd.setCursor(0,0);
  lcd.print("CONFIRME SUA NOVA SENHA...");
  lcd.setCursor(0,1);
  lcd.print(">");
}
if (keypressed != NO_KEY){
  if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
  keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
  keypressed == '8' || keypressed == '9' ){
    tempPassword += keypressed;
    lcd.setCursor(i,1);
    lcd.print("*");
    i++;
  }
  else if (keypressed=='#'){
    break;
  }
  else if (keypressed == '*'){
    if (doublecheck == 0){
      firstpass=tempPassword;
      doublecheck=1;
      newPassword();
    }
    if (doublecheck==1){
      doublecheck=0;
      if (firstpass==tempPassword){
        i=1;
        firstpass="";
        password = tempPassword;
        tempPassword="";//APAGAR SENHA TEMP
        lcd.setCursor(0,0);
        lcd.print("SENHA TROCADA");
        lcd.setCursor(0,1);
        lcd.print("----------------");
          storedPassword=true;
          delay(2000);
          lcd.clear();
          break;
      }
      else{
        firstpass="";
        newPassword();
           }
         }
      } 
     }
   }
 }
    
asked by anonymous 16.11.2017 / 13:47

1 answer

0

The question already has some time, but I will respond to help anyone who finds this topic.

As the amount of data is less than 1kb (limit of memory to be manipulated on the atmega328 chip), you can use the library:

 #include <EEPROM.h>

With the methods:

 EEPROM.write()
 EEPROM.read() 

Read the documentation at: link

But be aware that recording and reading is done in byte and not in string.

I found this code in: link

I did not test, but the forum says it works.

void save_string_to_eeprom(char *stringIn){

  Serial.print("stringIn length: ");
  Serial.print(strlen(stringIn));
  Serial.println();

    for(int i = 0; i < strlen(stringIn); i++){

      EEPROM.write(i, stringIn[i]);

  }

}

void read_string_from_eeprom(char *bufferIn){

  Serial.print("bufferIn sizeof: ");
  Serial.print(sizeof(bufferIn));
  Serial.println();

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

        bufferIn[i] = EEPROM.read(i);

    }

}
    
05.01.2018 / 14:56