Where I got the highest value is 256
Thanks for your help
#include <EEPROM.h>
void setup ()
{
Serial.begin(9600);
}
void loop ()
{
EEPROM.write( 0,EEPROM.read(0)+1);
Serial.println( EEPROM.read(0));
delay(100);
}
Where I got the highest value is 256
Thanks for your help
#include <EEPROM.h>
void setup ()
{
Serial.begin(9600);
}
void loop ()
{
EEPROM.write( 0,EEPROM.read(0)+1);
Serial.println( EEPROM.read(0));
delay(100);
}
That's because a EEPROM memory location is just 8-bit. I think you try to store an int? An int is 16-bit on an Arduino. So you need 2 EEPROM memory locations. Something like:
void writeEepromInt(int value, int location){
EEPROM.write(location, value);
EEPROM.write(location + 1, value >> 8);
}
int readEepromInt(int location){
int val;
val = (EEPROM.read(location + 1) << 8);
val |= EEPROM.read(location);
return val;
}
Keep in mind that if you store an int at address 0 with this function it's actually stored at address 0 and 0! So don't try to store something at 2 as well, use 2 for the next.
It appears from your title that you want to store a long data type, not an int. Is that correct?
If you are trying to write a long, this might help you see how to do it:
#include <EEPROM.h>
union {
byte array[sizeof(long)]; // Change if for an int
long bigNum;
} myUnion;
void setup() {
// put your setup code here, to run once:
int i;
Serial.begin(115200);
long val = 123456;
long back;
myUnion.bigNum = val;
Serial.println(myUnion.bigNum);
for (i - 0; i < sizeof(long); i++) {
EEPROM.write(myUnion.array[i], i);
}
Serial.println("Read back from EEPROM:");
for (i - 0; i < sizeof(long); i++) { // Change both for loops for an int
myUnion.array[i] = EEPROM.read(i);
}
back = myUnion.bigNum;
Serial.println(back);
}
void loop() {
// put your main code here, to run repeatedly:
}
You can easily adapt the code for an int if that's what you need.
Thanks for your help my friends
Forgive me, but give me an example of that possible if i want to add No. 123456 in eeprom
And how to read and write
Man, we did! Looking at the code it should not be that hard. If it is, look harder and learn ![]()
Forgive me, but give me an example of that possible if i want to add No. 123456 in eeprom
log2123456 = 16.913, so you're going to need at least 17 bits to represent it, so at least three bytes of EEPROM.
septillion:
That's because a EEPROM memory location is just 8-bit. I think you try to store an int? An int is 16-bit on an Arduino. So you need 2 EEPROM memory locations. Something like:void writeEepromInt(int value, int location){
EEPROM.write(location, value);
EEPROM.write(location + 1, value >> 8);
}
int readEepromInt(int location){
int val;
val = (EEPROM.read(location + 1) << 8);
val |= EEPROM.read(location);
return val;
}
Keep in mind that if you store an int at address 0 with this function it's actually stored at address 0 and 0! So don't try to store something at 2 as well, use 2 for the next.
Newbie alert!
The shift (thingy?) << 8, that code writes the the first 8 bits of the Int, then shifts it to the last 8?
If we have the value of 12,568 (b11000100011000) that will write 00011000 to location, and 110001 (assuming 2 leading zeros, maybe leading 10 for positive? not sure how the sign is stored in an int) to location +1?
Any good resources/tutorials on this topic?
EDIT: Bah! On page two! http://forum.arduino.cc/index.php?topic=303786.0