Writing/reading floats to EEPROM

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
bobulisk
 
Posts: 84
Joined: Tue Nov 02, 2010 7:29 am

Writing/reading floats to EEPROM

Post by bobulisk »

I'm trying to write and read floats with four decimal digits. I can get it to work with this code:

Code: Select all

#include <EEPROM.h>
#include <BANNED.h>


double testInt[12] = { 
  -12.5, -10.00, -5.7, 0, 2.45, 2.90, 3.10, 4 , 5.6, 7.9, 5.5, 4};
byte noElem = 12;
unsigned int baseAddr = 0;
unsigned int n = 0;

void setup() {
  Serial.begin(9600);

  // write data to eeprom
  for (int i=0; i <= noElem-1; i++){
    n = EEPROM_writeAnything( (i*4)+baseAddr, testInt[i]);
  }

  // read data back
  for (int i=0; i <= noElem-1; i++){
    double val;
    int addr = (i*4)+baseAddr;
    n = EEPROM_readAnything( addr, val);
    Serial.println(val);
  }
}

void loop() {
} 
How can I change this to read two more decimal digits? Should I change the address? I'm using this library: http://www.arduino.cc/playground/Code/E ... teAnything .

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Writing/reading floats to EEPROM

Post by adafruit_support_bill »

I'm not familiar with the BANNED library, but it looks like the code you have should work as-is. The "(i*4)" refers to the number of bytes in a 'double'. Not the number of significant digits.

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Writing/reading floats to EEPROM

Post by mtbf0 »

try

Code: Select all

    Serial.println(val,4);
floating point data on an arduino will have 6 or 7 digits of decimal precision.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”