8 years, 5 months ago.

data storage

Hi every one please I need help with the program code to store information in the SD card. The code i wrote below writes and store data in the sd card. The data is from the analog input at pin 18 in terms of voltage. But I wish to program it such that it reads the analog input with respect time. lets say about 2 minutes and store the data in terms of "volts" and "sec". The data can be used to plot a graph.Thanks

  1. include "mbed.h"
  2. include "TextLCD.h"
  3. include "SDFileSystem.h"

SDFileSystem sd(p11, p12, p13, p9, "sd"); the pinout on the mbed Cool Components workshop board TextLCD lcd(p19, p20, p21, p22, p23, p24); rs, e, d4-d7 AnalogIn lightin(p18); defines Analog input for light at pin 18 float light; defines the light data as type float int main() { lcd.printf("light in lux!\n"); light = lightin; wait(0.5);

FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); if(fp == NULL) { error("Could not open file for write\n"); } else{ lcd.printf("SD card file successfully opened\n"); } fprintf(fp, "light:%2.1f", "time:%2.1f%%", light , time); fclose(fp);

}

Question relating to:

-

posted by Nirvana Jay 03 Nov 2015

1 Answer

8 years, 5 months ago.

Here is something I use to record temperature and time.

Records the data as a .TXT file on the SD card separated by a ',' that can be imported into Excel.

edit..

This is very basic but may be more relevant to your application and will need adapting/adding to what you want.

You need to declare the 'buffer' with enough characters to contain your time string. Every time you call Datarecord(), a new record will be added to the SD file.

You can call SDreset() to clear the SD Datalogger.txt file.

You may need to format the SD card on your PC if the data gets corrupted.

SD record light and time snip untested

#include "mbed.h"
#include "SDFileSystem.h" 

AnalogIn light(p18); 

char buffer[42];
int err;
float light;

struct tm t;

void SDreset(), Datarecord();

int main()
{ 
    sd.disk_initialize();
    FILE *fp = fopen("/sd/Datalogger.txt", "r");  // check, does Datalogger.txt exist, if not call SDreset to create a file.
    if(fp == NULL) {err=1;}
    if (err==1) {SDreset();} 

// ...... you will need to set time first!

    if (time(NULL) < 1396310400) {//set_time if not set, less than 2015);} 
       t.tm_sec = 0;     // 0-59
       t.tm_min = 45;    // 0-59
       t.tm_hour = 9;    // 0-23
       t.tm_mday = 30;   // 1-31
       t.tm_mon = 9;     // 0-11
       t.tm_year = 115;  // year since 1900
       set_time(mktime(&t));
    }

while(1){

// your program here to call Datarecord() when you want to add another record,
// either by timer or external interrupt in.
// SDreset() to erase and create an empty file.

}

}

void Datarecord()
{
    time_t seconds = time(NULL);
    strftime(buffer, 40, "%d/%m/%y-%T", localtime(&seconds));
    FILE *fp = fopen("/sd/Datalogger.txt", "a");   // the 'a' appends data to the existing file.
    fprintf (fp,"%3.3f,",light);
    fprintf (fp,"%s,\r",buffer);        
    fclose(fp);
 }

void SDreset()
{
    FILE *fp = fopen("/sd/Datalogger.txt", "w");  // creates an empty file Datalogger.txt on the SD card, old data will be lost.
    fclose(fp);     
}

Paul thanks for the suggestion but I try to run the code it didnt recognised the declaration "buffer". how do I declare it.

posted by Prosper C 30 Oct 2015

char buffer[42];

posted by Andy A 02 Nov 2015