Help with SD Card.

Status
Not open for further replies.

BriComp

Well-known member
Code:
#include "Arduino.h"
#include <SD.h>
#include <SPI.h>

const int chipSelect = BUILTIN_SDCARD;

File f1, f2;

void setup()
{
    Serial.begin(9600);
    while (!Serial) {
        ; // wait for serial port to connect.
    }

    Serial.print("Initializing SD card...");

    // 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;
    } else {
        Serial.println();
    }
    f1 = SD.open("File1", FILE_WRITE);
    f2 = SD.open("File2", FILE_WRITE);
    Serial.print("File1 = ");  Serial.println(f1);
    Serial.print("File2 = ");  Serial.println(f2);

    f1.close();
    f2.close();
}

// Add the main program code into the continuous loop() function
void loop()
{

}
I have been doing some work with Records on SD Card but was getting peculiar results.
I thought that it was possible to have more than one SD card file open at the same time but indications indicated that that might not be so.
As a quick test I wrote the above small program and the file identifiers f1 and f1 are returning the same value.
What might be going wrong? I am using Teensyduino 1.54b11.

Below is the output from the above sketch. Both files are created on the SD cards, but with the same identifiers I can only access 1.
Initializing SD Card...
File1 = 1
File2 = 1
PS I should have added that I am using a Teensy 3.5
 
Last edited:
AFAIK, the return value of SD.open is a class (check File in FS.h), which inside contains as private data
Code:
	union {
		// instances of base File class use this pointer
		File *f;
		// instances of derived classes (which actually access media)
		// use this reference count which is managed by the base class
		unsigned int refcount;
	};
so, your printout says only that your refcounts are 1 for both files (they are open)
Don't ask me for more explanation, someone else may be able to explain
 
Yea it seems I might have got it all wrong. Thanks for support.
I guess I must continue trying to find my other similar problem with SD usage.
Thanks again.
 
Status
Not open for further replies.
Back
Top