Writing password to flash memory on Due

Posts: 26
Karma: 4  [add]
5.102.207.87

DueFlashStorage help with code
Today at 07:50 am 
Hello, i am using the DueFlashStorage library and my purpose is to change an array of 4 chars (trough keypad) and to write that to the flash memory.

Here is my code:

Code: [Select]
#include <DueFlashStorage.h>
DueFlashStorage dueFlashStorage;
#include <Keypad.h>                  
const byte numRows = 4;                           
const byte numCols = 4;       
char keymap [numRows][numCols] =
{
  {'1', '2', '3', 'A'},                              
  {'4', '5', '6', 'B'},                          
  {'7', '8', '9', 'C'},                         
  {'*', '0', '#', 'D'}                           
};
byte rowPins [numRows] = {6, 7, 8, 9};
byte colPins [numCols] = {2, 3, 4, 5};
Keypad myKeypad = Keypad (makeKeymap (keymap), rowPins, colPins, numRows, numCols);

char pass[4];
int counter =0;

void setup() {
  Serial.begin(9600);
  dueFlashStorage.write(0,pass[0]);
  dueFlashStorage.write(1,pass[1]);
  dueFlashStorage.write(2,pass[2]);
  dueFlashStorage.write(3,pass[3]);
}

void loop() {
  char keypressed = myKeypad.getKey();
  Serial.println("Your Password Is");
  Serial.print(dueFlashStorage.read(0));
  Serial.print(dueFlashStorage.read(1));  
  Serial.print(dueFlashStorage.read(2));  
  Serial.print(dueFlashStorage.read(3));
  delay(500);  
  
  if (keypressed == 'A')
  {
   Serial.println(""); 
   Serial.println("Whats the new pass?");
   while(counter < 4)
   {   
    char keypressed = myKeypad.getKey();
    if (keypressed)
    {
    pass[counter] = keypressed;
    counter++;
   }
  }
 }
}

Before i get to the "while" it says the password is "0000".
For some reason, after the "while" (after i change the pass) it still says that the password is "0000".
Can someone help me please?

You update the pass[] array inside the while() loop. But you never wrote it back to the flash storage. So when you read the flash storage, you get exactly what you wrote in setup().