Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Arduino SD card help

Status
Not open for further replies.

wizpic

Advanced Member level 3
Advanced Member level 3
Joined
May 23, 2004
Messages
792
Helped
108
Reputation
216
Reaction score
72
Trophy points
1,308
Location
London
Visit site
Activity points
6,405
I've decided to take the plunge and switch to the Arduino after using PDS for many years, I want to build a data logger but stuck on a little problem
I've got this test code working and every time the arduino starts it creates a new file and logs every five seconds.
Code:
#include <SPI.h>
#include <SD.h>

//Set by default for the SD Card Library
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
//We always need to set the CS Pin
int CS_pin = 10;
int pow_pin = 8;
char filename[16];
int n = 0;
float refresh_rate = 0.0;

void setup()
{
  Serial.begin(9600);  // Open serial communications and wait for port to open:
  Serial.print("Initializing SD card...");
  pinMode(CS_pin, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(CS_pin)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
  //char filename[16]; // make it long enough to hold your longest file name, plus a null terminator
  snprintf(filename, sizeof(filename), "data%03d.txt", n); // includes a three-digit sequence number in the file name
  while (SD.exists(filename)) {
    n++;
    snprintf(filename, sizeof(filename), "data%03d.txt", n);
  }
  File dataFile = SD.open(filename, FILE_READ);
  Serial.println(n);
  Serial.println(filename);
  //now filename[] contains the name of a file that doesn't exist
  //Write Log File Header

  //Read the Configuration information (COMMANDS.txt)
  File commandFile = SD.open("COMMANDS.txt");
  if (commandFile)
  {
    Serial.println("Reading Command File");

    float decade = pow(10, (commandFile.available() - 1));
    while (commandFile.available())
    {
      float temp = (commandFile.read() - '0');
      refresh_rate = temp * decade + refresh_rate;
      decade = decade / 10;
    }
    Serial.print("Refresh Rate = ");
    Serial.print(refresh_rate);
    Serial.println("ms");
  }
  else
  {
    Serial.println("Could not read command file.");
    return;
  }

}

void loop()
{

  File dataFile = SD.open(filename, FILE_WRITE);
  if (dataFile) {
    int sensorValue = analogRead(A0);
    float voltage = sensorValue * (5.0 / 1023.0);
    Serial.println(voltage);
    dataFile.println("teste");
    dataFile.println(voltage);
    dataFile.close();
  }
  else {
    Serial.println("error");

  }
  delay(refresh_rate);
}

But I want to be able to add an header but get stuck it will not compile I get an error this is the code I'm trying to add the header as I only need it once at the top of the file
Code:
#include <SPI.h>
#include <SD.h>

//Set by default for the SD Card Library
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
//We always need to set the CS Pin
int CS_pin = 10;
int pow_pin = 8;
char filename[16];
int n = 0;
float refresh_rate = 0.0;

void setup()
{
  Serial.begin(9600);  // Open serial communications and wait for port to open:
  Serial.print("Initializing SD card...");
  pinMode(CS_pin, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(CS_pin)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");
  //char filename[16]; // make it long enough to hold your longest file name, plus a null terminator
  snprintf(filename, sizeof(filename), "data%03d.txt", n); // includes a three-digit sequence number in the file name
  while (SD.exists(filename)) {
    n++;
    snprintf(filename, sizeof(filename), "data%03d.txt", n);
  }
  File dataFile = SD.open(filename, FILE_READ);
  Serial.println(n);
  Serial.println(filename);
  //now filename[] contains the name of a file that doesn't exist
  //Write Log File Header
//Write Log File Header
  File dataFile = SD.open("LOG.csv", FILE_WRITE);
  if (dataFile)
  {
    dataFile.println(", , , ,"); //Just a leading blank line, incase there was previous data
    String header = "ID, Light, Temp, IR1, IR2";
    dataFile.println(header);
    dataFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open file");
  }
  //Read the Configuration information (COMMANDS.txt)
  File commandFile = SD.open("COMMANDS.txt");
  if (commandFile)
  {
    Serial.println("Reading Command File");

    float decade = pow(10, (commandFile.available() - 1));
    while (commandFile.available())
    {
      float temp = (commandFile.read() - '0');
      refresh_rate = temp * decade + refresh_rate;
      decade = decade / 10;
    }
    Serial.print("Refresh Rate = ");
    Serial.print(refresh_rate);
    Serial.println("ms");
  }
  else
  {
    Serial.println("Could not read command file.");
    return;
  }

}

void loop()
{

  File dataFile = SD.open(filename, FILE_WRITE);
  if (dataFile) {
    int sensorValue = analogRead(A0);
    float voltage = sensorValue * (5.0 / 1023.0);
    Serial.println(voltage);
    dataFile.println("teste");
    dataFile.println(voltage);
    dataFile.close();
  }
  else {
    Serial.println("error");

  }
  delay(refresh_rate);
}

The error I get is
Code:
sd_read_write.ino: In function 'void setup()':
sd_read_write:38: error: redeclaration of 'File dataFile'
sd_read_write:32: error: 'File dataFile' previously declared here
redeclaration of 'File dataFile'
How can I add header bearing in mind I'm new to this and still learning

Thanks
 

Code:
File dataFile = SD.open("LOG.csv", FILE_WRITE);

you are defining twice dataFile and any compiler would not like that...

the first "File dataFile" is ok, now change that single line to:

Code:
dataFile = SD.open("LOG.csv", FILE_WRITE);

now you are reusing dataFile and i think it should compile at least. (but maybe the library doesn't like to reuse things so keep that in mind)
 
  • Like
Reactions: wizpic

    wizpic

    Points: 2
    Helpful Answer Positive Rating
Has simple as that, it complies ok quickly tried it and it looks like it's working but second time only bit wrote to file header but will do more testing as its late and brain is not fully awake
Thanks for helping
 

Kurenai_ryu thanks for that, I for some unknown reason I deleted the extra bits of data from that line all working now.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top