following sketch will run totally fine and as expected :
#include <Wire.h>
#include "SparkFun_External_EEPROM.h"
ExternalEEPROM ExtEEPROM;
char username[33] = "";
char password[65] = "";
void setup()
{
Serial.begin(115200);
delay(1000);
Wire.begin();
#define EEPROM_ADDRESS 0b1011000 // 0x58
ExtEEPROM.setMemorySize(256000/8); // 256kbit = 32kbyte
ExtEEPROM.setPageSize(64); // 64 byte page size.
ExtEEPROM.enablePollForWriteComplete();
ExtEEPROM.setPageWriteTime(10); // max. ms for AT24C128
if (ExtEEPROM.begin(EEPROM_ADDRESS, Wire) == false)
{
Serial.println("No memory detected. Freezing.");
while (1);
}
Serial.println("Memory detected!");
String uuu = "myusername";
String ppp = "mypassword";
uuu.toCharArray(username, sizeof(username) - 1);
ppp.toCharArray(password, sizeof(password) - 1);
Serial.print("password=");Serial.println(password);
ExtEEPROM.put(0, username);
//delay(100);
ExtEEPROM.put(0 + sizeof(username), "greatnewpassword");
}
void loop() {
char xxx[33] = "";
ExtEEPROM.get(0, xxx);
Serial.print("xxx=");Serial.println(xxx); // prints the username
char yyy[33] = "";
ExtEEPROM.get(0 + sizeof(username), yyy);
Serial.print("yyy=");Serial.println(yyy); // prints the password
delay(1000);
}
However, if I replace ExtEEPROM.put(0 + sizeof(username), "greatnewpassword");
to ExtEEPROM.put(0 + sizeof(username), password);
, then yyy
prints empty in the serial monitor. Why?
any_boolean == false
, use!any_boolean
. If you think think, you should make sure that anyone understands, then I'd recommend(any_boolean == false) == true
. :-Dsizeof
is an operator, not a function, so usesizeof variable
. However, its operand can be a type, and then it needs parentheses for syntactIcal reasons. This leads to this ubiquitous bad habit to write it as a function.password
, rather than a pointer produced by its decay (likechar *foo = password; ExtEEPROM.put(..., foo);
)?