1

I am looking to log analog sensor data using 3 pins, A1, A2, A3. My project requires continuous mapping of data, and hence an SD card shield was necessary. However, I need to log the incoming data into 3 different text files, and for that purpose I assumed that opening the 3 files simultaneously might or might not be possible as indicated here (I am a beginner, so I wasn't able to comprehend everything, and I am short on resources:)

So for the time being, I worked my way around with opening the three files in order. Rather than opening them all at once,writing the data on to each file and then closing them, I did all three tasks in 1) separate functions for all three files and calling them in the loop() function, 2)keeping all the stuff in the loop() function itself.The result for method-1 was no file was ever created, and that for method-2 was only the first file was created and the rest, nope. Here's the code for both the versions. What could I be doing wrong? Thanks in advance!

My board is an Arduino Uno. Yes, I have formatted the SD card with FAT32. My wiring is in place, my code is the only possible problem I see.

Method 1

#include <SD.h>
File Cu_volt;
File Gunmet_volt;
File Al_volt;
static float volt_Cu, volt_Gunmet, volt_Al;

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);

  //Initialize SD card module, CS pin set at 4
  Serial.println("INITIALIZING SD CARD...");
  //check for SD errors
  if ( !SD.begin(4)) {
    Serial.println("INITIALIZATION FAIL...");
    while (1);
    return;
  }
  Serial.println("INITIALIZATION DONE");
}

void loop() {
  // read the input on analog pin 0 copper electrode:
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 to VRef=INTERNAl)
 copperFile();
 gunmetalFile();
 aluminiumFile();
 delay(3000);

}

void copperFile() {
  Cu_volt = SD.open("Copper.txt", O_RDWR | O_APPEND);
  if (!Cu_volt) {
    Cu_volt = SD.open("Copper.txt", O_RDWR | O_CREAT);
    if (!Cu_volt) {
      Serial.println("Failed to open file at all");      
    }    
  }
  volt_Cu = analogRead(A1) * (1.1 / 1023.0);
  // print out the value read(COPPER):
  Serial.print("Copper voltage=");
  Serial.println(volt_Cu);
  if (Cu_volt) {
    Cu_volt.println(volt_Cu);
    Cu_volt.close();
  }
}

void gunmetalFile() {
  Gunmet_volt = SD.open("Gun_metal.txt", O_RDWR | O_APPEND);
  if (!Gunmet_volt) {
    Gunmet_volt = SD.open("Gun_metal.txt", O_RDWR | O_CREAT);
    if (!Gunmet_volt) {
      Serial.println("Failed to open file at all");
    }
  }
  volt_Gunmet = analogRead(A2) * (1.1 / 1023.0);
  // print out the value you read(GUNMETAL):
  Serial.print("Gun metal voltage=");
  Serial.println(volt_Gunmet);
  if (Gunmet_volt) {
    Gunmet_volt.println(volt_Gunmet);
    Gunmet_volt.close();
  }
}

void aluminiumFile(){
  //opening aluminium file
  Al_volt = SD.open("Aluminium.txt", O_RDWR | O_APPEND);
  if (!Al_volt) {
    Al_volt = SD.open("Aluminium.txt", O_RDWR | O_CREAT);
    if (!Al_volt) {
      Serial.println("Failed to open file at all");
    }
  }
   volt_Al = analogRead(A3) * (1.1 / 1023.0);
  // print out the value you read(ALUMINIUM):
  Serial.print("Aluminium voltage=");
  Serial.println(volt_Al);
  if (Al_volt) {
    Al_volt.println(volt_Al);
    Al_volt.close();
  }
} 

Method 2

#include <SD.h>
File Cu_volt;
File Gunmet_volt;
File Al_volt;
static float volt_Cu, volt_Gunmet, volt_Al;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  analogReference(INTERNAL);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  //Initialize SD card module, CS pin set at 4
  Serial.println("INITIALIZING SD CARD...");
  //check for SD errors
  if ( !SD.begin(4)) {
    Serial.println("INITIALIZATION FAIL...");
    while (1);
    return;
  }
  Serial.println("INITIALIZATION DONE");
}

void loop() {
  // read the input on analog pin 0 copper electrode:
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 to VRef=INTERNAl)
  volt_Cu = analogRead(A1) * (1.1 / 1023.0);
  // print out the value you read(COPPER):
  Serial.print("Copper voltage=");
  Serial.println(volt_Cu);
  //opening copper file
  Cu_volt = SD.open("Copper.txt", O_RDWR | O_APPEND);
  if (!Cu_volt) {
    Cu_volt = SD.open("Copper.txt", O_RDWR | O_CREAT);
    if (!Cu_volt) {
      Serial.println("Failed to open file at all");
    }
  }
  if (Cu_volt) {
    Cu_volt.println(volt_Cu);
    Cu_volt.close();
  }
  volt_Gunmet = analogRead(A2) * (1.1 / 1023.0);
  // print out the value you read(GUNMETAL):
  Serial.print("Gun metal voltage=");
  Serial.println(volt_Gunmet);
  //opening copper file
  Gunmet_volt = SD.open("Gun_metal.txt", O_RDWR | O_APPEND);
  if (!Gunmet_volt) {
    Gunmet_volt = SD.open("Gun_metal.txt", O_RDWR | O_CREAT);
    if (!Gunmet_volt) {
      Serial.println("Failed to open file at all");
    }
  }
  if (Gunmet_volt) {
    Gunmet_volt.println(volt_Gunmet);
    Gunmet_volt.close();
  }
  volt_Al = analogRead(A3) * (1.1 / 1023.0);
  // print out the value you read(ALUMINIUM):
  Serial.print("Aluminium voltage=");
  Serial.println(volt_Al);
  //opening aluminium file
  Al_volt = SD.open("Aluminium.txt", O_RDWR | O_APPEND);
  if (!Al_volt) {
    Al_volt = SD.open("Aluminium.txt", O_RDWR | O_CREAT);
    if (!Al_volt) {
      Serial.println("Failed to open file at all");
    }
  }
 if (Al_volt) {
    Al_volt.println(volt_Al);
    Al_volt.close();
  }
  delay(3000);
}
  • Does it works if you only write to one file and comment out the rest? – user31481 Dec 8 '17 at 10:26
  • Writting the three reading in the same file can be an alternative, writting as three comma separated values per line. – user31481 Dec 8 '17 at 10:29
  • @LookAlterno I'll check it out. Using the alternative looks like a plausible idea anyhow:) – Abhinav Dec 8 '17 at 10:34
1

There are several variations of the SD library - some support multiple files, others do not.

Try calling .begin (and .end) on each write loop, and add in .flush - see below.

If that works with your library then take the (.begin / .end) out of the loop and try again.

#define FILENAME "myfile.txt"

void writeToFile() {
    File f;
    // begin and end on each write 
    if (SD.begin(10, 11, 12, 13)) { // I use an older library that allows pin reassignment
        f = SD.open(FILENAME, FILE_WRITE);  // This will append and create if not exisiting
        if(f) {
            f.println("a string");  // Your write operations
        }
        f.flush();          // This ensured the write buffer actually went to file
                            // even if less than the block size (512b?)

        f.close();          
        }
    SD.end();                   // Do this after every write operation 
}
| improve this answer | |
1

For people having similar problems with creating multiple files on an SD card, it may be a memory issue. You need to have a certain amount of unused dynamic memory (about 300 bytes of free space, in my experience) in order to write to an SD card - and more if you are writing multiple files. Following the instructions on this page may help.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.