Go Down

Topic: Putting a String datatype in EEPROM Arduino UNO ISSUE! (Read 131 times) previous topic - next topic

fi8er1

Making a RFID reader door buzzer and putting a String datatype in EEPROM for comparison upon power loss as well.

Code: [Select]

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#include <EEPROM.h>

#define PN532_SCK  (2)
#define PN532_MOSI (3)
#define PN532_SS   (4)
#define PN532_MISO (5)

#define LEDRED (9)
#define LEDGREEN (10)
#define BUZZER (11)
#define DOOR (12)

Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

String registeredCodes;
void setup(void) { 
 
 
  Serial.begin(115200);
  Serial.println("Hello!");

  pinMode(LEDRED, OUTPUT);
  pinMode(LEDGREEN, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(DOOR, OUTPUT);   
 
  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
     
     
 
 
  // configure board to read RFID tags
  nfc.SAMConfig();
   
  delay(500);
 
  Serial.println("Waiting for a Card ...");
}

 
void loop(void) {
 
  registeredCodes = EEPROM.get(0, registeredCodes); 
  Serial.println("RegisteredCodes:" + registeredCodes);
 
  uint8_t readSuccess;
  uint8_t unregisteredReadSuccess; 
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
   
  readSuccess = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
 
  if(readSuccess) {
    String uidChar ="";
      for ( int i = 0; i < 3; i++ ) {
        uidChar +=  uid[i];
       }
       Serial.println("ID:"+uidChar);       
    // Card successfully read indication.
     for(int i=0; i<3; i = i+1) {
      digitalWrite(LEDGREEN, HIGH);
      digitalWrite(LEDRED, HIGH);
      digitalWrite(BUZZER, HIGH);
      delay(150);   
      digitalWrite(LEDGREEN, LOW);
      digitalWrite(LEDRED, LOW);   
      digitalWrite(BUZZER, LOW);
      delay(150);
    }
   
    if (uidChar == "53197251") {
       Serial.println("Mastercard found");
       digitalWrite(LEDGREEN, HIGH);
       digitalWrite(LEDRED, HIGH);
       digitalWrite(BUZZER, HIGH);
       digitalWrite(DOOR, HIGH);
       delay(250);
       digitalWrite(DOOR, LOW);       
       Serial.println("Door Unlocked");
           delay(1000);
           Serial.println("Waiting for a new card to be programmed...");
           delay(500);
           uint8_t uidTwo[] = { 0, 0, 0, 0, 0, 0, 0 };
           uint8_t uidLengthTwo;
           unregisteredReadSuccess = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uidTwo, &uidLengthTwo);
           
           if(unregisteredReadSuccess){                   
             digitalWrite(LEDGREEN, LOW);
             digitalWrite(LEDRED, LOW);
             digitalWrite(BUZZER, LOW);             
                String uidCharTwo ="";           
                  for ( int i = 0; i < 3; i++ ) {
                    uidCharTwo +=  uidTwo[i];           
                   }             
             Serial.println("ID:"+uidCharTwo);
             if(uidCharTwo == "53197251"){               
                for(int i=0; i<6; i = i+1){
                    digitalWrite(LEDGREEN, HIGH);
                    digitalWrite(LEDRED, HIGH);
                    digitalWrite(BUZZER, HIGH);
                    delay(100);   
                    digitalWrite(LEDGREEN, LOW);
                    digitalWrite(LEDRED, LOW);   
                    digitalWrite(BUZZER, LOW);
                    delay(100);
                      }
                      delay(500);
                      registeredCodes = "";                     
                      EEPROM.put(0, registeredCodes);
                    digitalWrite(LEDGREEN, HIGH);                     
                    digitalWrite(LEDRED, HIGH);
                    digitalWrite(BUZZER, HIGH);
                    delay(1500);
                    digitalWrite(LEDGREEN, LOW);
                    digitalWrite(LEDRED, LOW);
                    digitalWrite(BUZZER, LOW);     
                    Serial.println("MASTER-RESET Completed!");               
                 } else {     
                    delay(500);                   
                    digitalWrite(LEDGREEN, HIGH);
                    digitalWrite(LEDRED, HIGH);
                    digitalWrite(BUZZER, HIGH);
                    delay(250);
                    digitalWrite(LEDGREEN, LOW);
                    digitalWrite(LEDRED, LOW);
                    digitalWrite(BUZZER, LOW);
                    delay(500);
                    registeredCodes += uidCharTwo;
//                    registeredCodes += ", ";
                    EEPROM.put(0, registeredCodes);
                    digitalWrite(LEDGREEN, HIGH);
                    digitalWrite(BUZZER, HIGH);
                    delay(1500);
                    digitalWrite(LEDGREEN, LOW);
                    digitalWrite(BUZZER, LOW);
                    Serial.println("New card registered!");
                 }
           }                     
           delay(500);             
         
         Serial.println("MastercardFunctionEnded");
    } else if (uidChar == registeredCodes) {   
    // ACCESS GRANTED! Unlocking the door...
      digitalWrite(LEDGREEN, HIGH);   
    Serial.println("ACCESS GRANTED");   
      delay(300);       
      digitalWrite(DOOR, HIGH);     
      delay(250);
    Serial.println("Door Unlocked!");
      digitalWrite(DOOR, LOW);
      delay(300);
      digitalWrite(LEDGREEN, LOW);
    }   
    else {     
    // ACCESS DENIED!
    Serial.println("ACCESS DENIED");
      digitalWrite(LEDRED, HIGH);
      digitalWrite(BUZZER, HIGH);
      delay(1500);
      digitalWrite(LEDRED, LOW);
      digitalWrite(BUZZER, LOW);
    }       
    // delay till the next read   
    delay(3000);   
  }
}


The issue is EEPROM.get messes with the String. See attached photo for serial out.

AWOL

Now do you see why we ask you to use code tags when posting code?

Please edit the post to add the code tags.
Here, I've got a spare pair - you can use these [code][/code]

BTW, your thread title says "String", but your description says "string" - there's a big difference.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

kowalski

If you check the EEPROM class you will see that it does not understand the type String. It only understands "byte". You need to write/read character by character to/from the EEPROM to the String. Cheers!

fi8er1

could you please provide me a working example looking at my code I'm new to this stuff and a little help will be appreciated.
Thanks

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy