Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Read float and long from external EEPROM chip

I am using an Arduino to read information from an EEPROM chip over I2C with the code I found here: https://playground.arduino.cc/Code/I2CEEPROM

It works great for int and char values but I also need to read short, long, and float values.

They are stored in this order: 1510543923
is stored as: 01011010 00001001 00010010 00110011

How can I read multiple bytes into a single variable of these types?

I tried the following but it gives me a 16 bit number:

i2c_eeprom_read_buffer(0x50, 25, (byte *)bytes, 4);
long j = (bytes[3] << 0) + (bytes[2] << 8) + (bytes[1] << 16) + (bytes[0] << 24);

Answer*

Draft saved
Draft discarded
Cancel
3
  • In reading the float, I correctly get 00110101-00111000-10101000-11001110 from the EEPROM but running it through the union snipit you show, gives 0.0. It should be 6.8790985E-7. Commented Dec 7, 2017 at 3:19
  • @Alphy13: I am pretty sure you got the correct number. However, Serial.println(6.8790985e-7); prints "0.00". Commented Dec 7, 2017 at 8:28
  • Thanks for pointing that out. In case anyone has a similar issue: Serial.println(x, 10); gave the precision i needed to see a result. Commented Dec 7, 2017 at 22:22

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /