9 years, 10 months ago.

Saving Data to mbed

I have learned and tried to write data into local memory mbed and save it in txt format. The data is ADCdata. But I do not know how to save it?

  1. include "mbed.h"

Serial pc(USBTX,USBRX); AnalogIn Ain(p20); AnalogOut Aout(p18);

float ADCdata; int main() { pc.printf(" ADC Data Values... \n\r"); while(1) { ADCdata=Ain*3.3; } }

Please help me, thank you!

1 Answer

9 years, 10 months ago.

On an LPC1768 you can save to LocalFilesystem. (see http://developer.mbed.org/handbook/LocalFileSystem )

On other boards you'll need an SD card socket, if your board doesn't have one you'd need to add it. You can then use SDFileSystem (see http://developer.mbed.org/cookbook/SD-Card-File-System ) to save files to the SD card.

For either option the method is the same.

#include "SDFileSystem.h"

// create the file system object on a path called "sd", this line is the only difference for LocalFileSystem
SDFileSystem sdfilesys(..pins, "sd"); 

// create an empty file pointer.
FILE *myLogFile = NULL;
Serial pc(USBTX,USBRX);

main () {
  myLogFile = fopen("/sd/log.txt", "w"); // open log.txt as a write only file (any existing file will be overwritten). Replace "w" with "a" to append
                                      // the "sd" part of the name must match the name given when creating the file system above.

  if (myLogFile == NULL) {  // check the file is open.
    pc.printf("Couldn't open the log file.);
    while (1) {
      wait(100);
    }
  }

  // the rest of your main goes here.

  // to write to the file use fprintf, syntax is the same as printf only with the file pointer at the start. e.g.:
  fprintf(myLogFile, "The number is %.4f\n\r", aFloatingPointNumber); 

  fclose( myLogFile); // call this at the end of your program or the file won't get written correctly to the card.
}

Also in the future please use <<code>> and <</code>> to make code format correctly on this board.

Accepted Answer