I'm new to Arduino and I'm having a problem with Arduino...I have 2 boards. One is an Arduino UNO and one is an Arduino Mega. I run the following little program in both, the only thing it does is clean the EEPROM memory and show how it turned out. The idea is that the output should always be 0 because memory is being erased. I put the code and the different outputs.
#include <EEPROM.h>
void EEPROMWriteInt(int address, int value); //2 Bytes, LE DIGO COMO GUARDAR
int EEPROMReadInt(int address);
int32_t numberSens = 0;
int32_t numberDESP = 0;
int32_t numberGI = 0;
int32_t numberGD = 0;
void setup() {
Serial.begin(9600);
for (int nL = 0; nL < EEPROM.length(); nL++) {
EEPROM.write(nL, 0);
}
numberSens = EEPROMReadInt(1);
numberDESP = EEPROMReadInt(3);
numberGI = EEPROMReadInt(5);
numberGD = EEPROMReadInt(7);
Serial.print("numberSens ");
Serial.println(numberSens);
Serial.print("numberDESP ");
Serial.println(numberDESP);
Serial.print("numberGI ");
Serial.println(numberGI);
Serial.print("numberGD ");
Serial.println(numberGD);
}
void loop() {
}
void EEPROMWriteInt(int address, int value) {
byte hiByte = highByte(value);
byte loByte = lowByte(value);
EEPROM.write(address, hiByte);
EEPROM.write(address + 1, loByte);
}
int EEPROMReadInt(int address)
{
byte hiByte = EEPROM.read(address);
byte loByte = EEPROM.read(address + 1);
return word(hiByte, loByte);
}
So the output on Arduino UNO is:
numberSens 0
numberDESP 0
numberGI 0
numberGD 0
The output on Arduino MEGA is:
numberSens 4353
numberDESP 1408
numberGI 0
numberGD 256
Does anyone have an idea or a clue why this might be happening? I thank you in advance.
The write() method works with bytes, if you are going to use different types of variables, such as int or even data structures, use the put() and get() methods:
I haven't tried it on the mega, it should work.
your example worked fine on one and poorly on mega. BUT I found the problem(s). The first problem is that the MEGA has the bytes in front (0 to 6 approx. damaged). I realized this since the same example I do save in higher memory locations and it works correctly. The second problem was that int32_t occupies 4 bytes and in my example I am trying to write it in 2 bytes, so the first number saves it well, but from the second it starts to overlap. To solve it, I used a read method and a write method that use the number of bytes according to the data type. I leave the code in case it helps someone. Thank you all.