Pages: [1]   Go Down
Author Topic: Read float from SD card with Fat16 Lib  (Read 744 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 77
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello guys, I Need your help !
I use the fat16 lib to store float values (temperatures) to a SD Card. One per line and with 2 fractional digits.
Now I want to read them out again and store them into float variables.
I use this code to read out data in general:

Code:
/*
 * This sketch reads and prints the file
 * PRINT00.TXT created by fat16print.pde or
 * WRITE00.TXT created by fat16write.pde
 */
#include <Fat16.h>
#include <Fat16util.h> // use functions to print strings from flash memory

SdCard card;
Fat16 file;

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))

void error_P(const char*  str) {
  PgmPrint("error: ");
  SerialPrintln_P(str);
  if (card.errorCode) {
    PgmPrint("SD error: ");
    Serial.println(card.errorCode, HEX);
  }
  while(1);
}

void setup(void) {
  Serial.begin(9600);
  Serial.println();
  PgmPrintln("type any character to start");
  while (!Serial.available());
  Serial.println();
 
  // initialize the SD card
  if (!card.init()) error("card.init");
 
  // initialize a FAT16 volume
  if (!Fat16::init(&card)) error("Fat16::init");
 
  // open a file
  if (file.open("PRINT00.TXT", O_READ)) {
    PgmPrintln("Opened PRINT00.TXT");
  } else if (file.open("WRITE00.TXT", O_READ)) {
    PgmPrintln("Opened WRITE00.TXT");   
  } else{
    error("file.open");
  }
  Serial.println();
 
  // copy file to serial port
  int16_t n;
  uint8_t buf[7];// nothing special about 7, just a lucky number.
  while ((n = file.read(buf, sizeof(buf))) > 0) {
    for (uint8_t i = 0; i < n; i++) Serial.write(buf[i]);
  }
   PgmPrintln("\nDone");
}

void loop(void) {}

So how can I convert the uint8_t Array to a float variable ?
Thanks for your help smiley
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 29
Posts: 1667
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

store float values (temperatures) to a SD Card. One per line and with 2 fractional digits.
Now I want to read them out again

Code:
/*
  Serial.println();
  // copy file to serial port
 /code]

So how can I convert the uint8_t Array to a float variable ?

[/quote]

Why is there an array? You should be able to simply write the floats to the card and then read them straight back.

[code]//  Read temps off SD.
 
#include <SD.h>
File myFile;

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
   pinMode(53, OUTPUT);// pin 10 on Uno
   
  if (!SD.begin(4)) {
    Serial.print("initialization failed!");
    return;
  }
  Serial.println(" done.");

  myFile = SD.open("log.txt", FILE_WRITE);
  if (myFile) {
    Serial.print("Checking the file, no write...");
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening");
  }
 
  myFile = SD.open("log.txt");// re-open the file for reading:
  if (myFile) {
    Serial.println("log.txt:");   
    while (myFile.available()) {// read until EOF
    Serial.write(myFile.read());
    }
    myFile.close();
  } else {
    Serial.println("error opening log.txt");
  }
}

void loop()
{
}
[/code]
Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 77
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

store float values (temperatures) to a SD Card. One per line and with 2 fractional digits.
Now I want to read them out again

Code:
/*
  Serial.println();
  // copy file to serial port
 /code]

So how can I convert the uint8_t Array to a float variable ?

[/quote]

Why is there an array? You should be able to simply write the floats to the card and then read them straight back.

[code]//  Read temps off SD.
 
#include <SD.h>
File myFile;

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
   pinMode(53, OUTPUT);// pin 10 on Uno
   
  if (!SD.begin(4)) {
    Serial.print("initialization failed!");
    return;
  }
  Serial.println(" done.");

  myFile = SD.open("log.txt", FILE_WRITE);
  if (myFile) {
    Serial.print("Checking the file, no write...");
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening");
  }
 
  myFile = SD.open("log.txt");// re-open the file for reading:
  if (myFile) {
    Serial.println("log.txt:");   
    while (myFile.available()) {// read until EOF
    Serial.write(myFile.read());
    }
    myFile.close();
  } else {
    Serial.println("error opening log.txt");
  }
}

void loop()
{
}
[/code]

Thanks Nick_Pyner, but I finally managed it on my own.
Just a single conversion to double was required. I am not using the official SD lib because the Fat16lib is much smaller and for my Project, code size is a very important aspect.

Code:
while((file.read(buf, sizeof(buf)) >0) && (count <= 3071)){
    double temp = atof((char*)buf);
  }   
Logged

Pages: [1]   Go Up
Jump to: