0

I am trying to find a straightforward way to store negative values in EEPROM, integer values ranging from -20 to 20. I have been using EEPROM.write and EEPROM.read functions to store strings one character at a time, but I am having trouble with negative numbers. I figure I only need one byte for this value.

bbglazer
  • 123
  • 3
  • 16

1 Answers1

3

It's just matter of number representation. You just have to use correct data types to print or use:

Version 1: int8_t data = EEPROM.read(addr);

Version 2:

byte data = EEPROM.read(addr);
Serial.print((int8_t)data);

EEPROM.write can be used directly with int8_t: EEPROM.write(int8_value);

Or, if you wan't int, put/get methods can be used for it (even for structs containing POD types only or so)

KIIV
  • 3,534
  • 2
  • 18
  • 23
  • Thanks! I am wondering, is there a framework/method where I can store all my saved data in a more manageable way, so I can just save the value to EEPROM based on the variable name? – bbglazer Aug 22 '16 at 08:37
  • For AVRs there is [EEMEM](https://tinkerlog.com/2007/06/16/using-progmem-and-eemem-with-avrs/) modifier (only for internal memory). So you can have automatically placed eeprom adresses. It should be possible to initialize eeprom to default values too, but it wasn't working for me in Arduino IDE, so it might not be supported. – KIIV Aug 22 '16 at 09:08
  • Thanks for the info! – bbglazer Aug 24 '16 at 07:47