Hi,
I'm trying to create a small script that stores to and reads data from Arduino Uno's EEPROM in form of an array. So far I can only see zero when debugged with Serial print.
#include <EEPROM.h>
int address = 0;
int myArray[5];
int var1= 0;
int var2= 0;
int var3= 0;
int var4= 0;
int var5= 0;
void setup() {
//Here I'm trying to read EEPROM and write values to variables.
EEPROM.get(address,myArray[5]);
var1 = myArray[0];
var2 = myArray[1];
var3 = myArray[2];
var4 = myArray[3];
var5 = myArray[4];
}
void loop() {
// Omitted I have some codes that feed Var1 to 5 variables with integer values.
// I have a control mechanism that triggers EEPROM write sequence only if there's new information.
//And below I'm trying to write the changes to EEPROM.
myArray[0] = var1;
myArray[1] = var2;
myArray[2] = var3;
myArray[3] = var4;
myArray[4] = var5;
EEPROM.update(address, myArray[5]);
}
Is there any better way of doing this?
You could try to read and write the whole array and not the integer that happens to be located behind it.
myArray[5]
does not exist, the elements of that array are numbered between 0 and 4.
You could try (untested)
EEPROM.get(address, myArray);
Whandall:
You could try to read and write the whole array and not the integer that happens to be located behind it.
myArray[5]
does not exist, the elements of that array are numbered between 0 and 4.
You could try (untested)
EEPROM.get(address, myArray);
lol this time, I'm getting -1 instead of 0.
Please post the entire modified sketch. I don't think the EEPROM library can handle arrays - only single variables.
The default content of the EEPROM is 0xFF, hence -1 for two 0xFF interpreted as a signed int.
put() and get() should be able to handle any object that does not contain pointers.
Whandall:
The default content of the EEPROM is 0xFF, hence -1 for two 0xFF interpreted as a signed int.
put() and get() should be able to handle any object that does not contain pointers.
An array is not a singular object. "myArray" is a pointer.
#include <EEPROM.h>
char Text1[] = "6.75\r\n";
int values[] = {5, 4, 3, 2, 1, 0, -1 };
void setup() {
Serial.begin(250000);
edump(128, 32);
EEPROM.put(128, Text1);
edump(128, 32);
EEPROM.put(128, values);
edump(128, 32);
}
void edump(const void* adr, int len) {
char* ptr = (char*) adr;
for (; len > 0; len -= 0x10, ptr += 0x10) {
byte i;
for (i = 0; i < 16; i++) {
if (i < len) {
Serial.write(' ');
pHex(EEPROM.read(ptr + i));
}
}
Serial.print(F(" '"));
for (i = 0; i < 16; i++) {
if (i < len) {
byte val = EEPROM.read(ptr + i);
Serial.write(val < 0x20 ? '.' : val);
} else {
break;
}
}
Serial.println('\'');
}
}
void pHex(byte val) {
if (val < 16) {
Serial.write('0');
}
Serial.print(val, HEX);
}
void loop() {}
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 'ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ'
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 'ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ'
36 2E 37 35 0D 0A 00 FF FF FF FF FF FF FF FF FF '6.75...ÿÿÿÿÿÿÿÿÿ'
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 'ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ'
05 00 04 00 03 00 02 00 01 00 00 00 FF FF FF FF '............ÿÿÿÿ'
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 'ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ'
aarg:
An array is not a singular object. "myArray" is a pointer.
An array is a collection of objects of the same kind and it only contains pointers when the objects contain them.
The name of the collection can be used as a pointer constant.
As you can see from the above example, an array can be saved to EEPROM with put().