Pages: [1]   Go Down
Author Topic: Swapping between two SD files  (Read 686 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am a beginner, hence poor at understanding C++
I want to record 2 data sets on the SD card, one initiated by a regular alarm and the other by an interrupt.
So I would like to use an IF statement to close a file (if it is open) so that I can open the other file. And vice versa.

My first ignorant attempt won't verify.
I would appreciate a code suggestion to achieve this aim.
I am using Tom Igoe's SD example code to learn.  1st line shown here is my error.
Code:
if (dataFile){
  Serial.print("OK");
}
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  } 
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}
Logged

Offline Offline
Edison Member
*
Karma: 35
Posts: 1429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
1st line shown here is my error
When I was at uni, I failed Clairvoyance 101 very badly so I have no idea what that error message was and the code fragment that you've allowed us to see is not sufficient to know what is wrong anyway.

Pete
Logged

Where are the Nick Gammons of yesteryear?

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 30
Posts: 1706
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I want to record 2 data sets on the SD card, one initiated by a regular alarm and the other by an interrupt.
So I would like to use an IF statement to close a file (if it is open) s

I don't see why you need to complicate things by using an IF. You can use the approach that files are normally closed, i.e.  open/write/close, do something else. That way there is no need to ask the question, or worry about it.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am SO sorry! Don't you hate people like that?  I had thought that the fault might be obvious for someone who was fluent in C++  -  but I do now see the point. Here is the whole of the code with my 2 added lines.  And these are the fault indications.
Code:
Datalogger.ino: In function 'void loop()':
Datalogger:71: error: 'dataFile' was not declared in this scope
Datalogger:86: error: expected `}' at end of input

Code:
/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors
 to an SD card using the SD library.
 
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 
 */

#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
 
  if (dataFile){
  Serial.print("OK");
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  } 
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}
Logged

Offline Offline
Edison Member
*
Karma: 35
Posts: 1429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you use autoformatting in the IDE: Tools|Auto Format
it tells you that there are "too many left curly braces".
And the compiler error:
Quote
Datalogger:86: error: expected `}' at end of input
says the same thing in a different way.
Focus on this piece of your code:
Code:
  if (dataFile){
  Serial.print("OK");
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  } 
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }

You will then run into a different problem because you are using the variable "datafile" before you declare it. It needs to be global.

Pete
Logged

Where are the Nick Gammons of yesteryear?

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Nick and Pete.
The interrupts may occur at varying frequencies, perhaps as fast as just over 1 per second for perhaps 30 minutes and then not for many days. I had envisaged leaving the file open while that activity was ongoing, but the loop would also be receiving an alarm at 20 minutes intervals, so I thought I should have a method of closing the interrupt file so that the alarm file could be opened, followed swiftly by re-opening the interrupt file. I will look at your suggestion. Thank you.
I am on holiday away at the moment so the lack of curly brackets may have been an additional writing error here; I do not have all the files on this notebook. But, thank you I will ensure I use the autoformat facility. It was my inability to see how the Datafile declaration was done in the original program that was bothering me.
Logged

Pages: [1]   Go Up
Jump to: