Go Down

Topic: Storing value in EEPROM in Arduino Uno (Read 506 times) previous topic - next topic

vishnu_ks

Hi I am a beginner in Arduino Uno. I wanted to store a value of 77880 in EEPROM and fetch it later. I made the code as follows to check my logic(I am using Arduino Uno) :
Code: [Select]
#include <EEPROM.h>
int addr = 0;
byte value;
void setup() {
  // put your setup code here, to run once:
  int val = 77880;
  EEPROM.write(addr, val);
  Serial.begin(57600);
}

void loop() {
  // put your main code here, to run repeatedly:
  value = EEPROM.read(addr);

  Serial.print(addr);
  Serial.print("\t");
  Serial.print(value);
  Serial.println();
  delay(1000);
}

 but I am not getting the value of 77880 when I read the EEPROM address. instead I am getting a value of  56 in my serial output. Can anybody pls explain how should I implement the same?

TheUNOGuy

#1
Oct 17, 2020, 09:33 am Last Edit: Oct 17, 2020, 09:33 am by TheUNOGuy
The value of 77880 is too big in an int. As you are using an UNO, you can store an int from the value -32,768 to 32,767...On the other hand, long can store a value from -2,147,483,648 to 2,147,483,647. Try replacing int val with:
Code: [Select]
long val = 77880;
It's better if you check the internet  ( your friend ) before posting..! :D
#Learn how to use the FORUM - https://forum.arduino.cc/index.php?topic=148850.0

noiasca

next problem is, EEPROM.write will only write ONE byte.

Just use EEPROM.put and EEPROM.get to save variables according to their size.
https://www.arduino.cc/en/Reference/EEPROMPut

how to react on postings:
- post helped: provide your final sketch, say thank you & give karma.
- post not understood: Ask as long as you understand the post
- post is off topic (or you think it is): Stay to your topic. Ask again.
- else: Ask again.

TheUNOGuy

It's better if you check the internet  ( your friend ) before posting..! :D
#Learn how to use the FORUM - https://forum.arduino.cc/index.php?topic=148850.0

Go Up