Page 1 of 1

How to save ULong variable value in EEPROM?

Posted: Tue Dec 18, 2018 10:45 am
by kg300
Hi, I want to save a Ulong Variable (06 digits) in EEPROM, any example for me.?
I get a code from Arduino forum, writing in the eeprom memory locations is ok, but when i tried to get back data from these locations variable only show the value less then 65000. mean only get back Unint value from eeprom. any help pleas...

long EEPROMReadlong(long address) {
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);

return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}

void EEPROMWritelong(int address, long value) {
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);

EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}

Re: How to save ULong variable value in EEPROM?

Posted: Tue Dec 18, 2018 11:04 am
by Benj
Hello,

Maybe try this instead.

Read

Code: Select all

byte four = EEPROM.read(address);
byte three = EEPROM.read(address + 1);
byte two = EEPROM.read(address + 2);
byte one = EEPROM.read(address + 3);

return = one
return = return << 8
return = return | two
return = return << 8
return = return | three
return = return << 8
return = return | four
Write

Code: Select all

byte four = value;
value = value >> 8;
byte three = value;
value = value >> 8;
byte two = value;
value = value >> 8;
byte one = value;

EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);

Re: How to save ULong variable value in EEPROM?

Posted: Wed Dec 19, 2018 9:14 am
by kg300
Its working ok. thanks Benj :D

Re: How to save ULong variable value in EEPROM?

Posted: Wed Sep 02, 2020 1:44 pm
by Sashinda
hi,
How to write this in calculation block?

EEPROM Read;-
return = one
return = return << 8
return = return | two
return = return << 8
return = return | three
return = return << 8
return = return | four

EEPROM Write :-

byte four = value;
value = value >> 8;
byte three = value;
value = value >> 8;
byte two = value;
value = value >> 8;
byte one = value;

Please help me...

Re: How to save ULong variable value in EEPROM?

Posted: Wed Sep 02, 2020 5:11 pm
by medelec35
Hi Sashinda,
Welcome to the forums.
I have attached a flowchart that works the way Ben suggested.
I have also included two alternative ways of saving and retrieving ULongs(disabled icons).

Re: How to save ULong variable value in EEPROM?

Posted: Thu Sep 03, 2020 7:20 pm
by Sashinda
hello...Medelec35
It's working super... :D
Thanks a lot. :)
I will never forget your help...

Re: How to save ULong variable value in EEPROM?

Posted: Thu Sep 03, 2020 8:02 pm
by medelec35
Hi Sashinda ,
You're welcome.
I'm pleased you have your project working super.
Thank you for letting us know, really appreciate it.

Re: How to save ULong variable value in EEPROM?

Posted: Fri Sep 04, 2020 7:43 am
by AbhijitR
Hello! Martin

Many thanks for this post, you are too good.
medelec35 wrote: ↑
Wed Sep 02, 2020 5:11 pm
I have also included two alternative ways of saving and retrieving ULongs(disabled icons).
Finally I got the opportunity to thank you.

Abhi