My setup code for my project won't correctly open the file when I'm asking it too in the for loop. when I print the value of "myFile" it is zero right after I called for the command to open it. I used this code in a test program and it worked properly. Ill post both below.
I'm using the Adafruit feather M0.
Setup code with issues:
#include <SPI.h>
#include <SD.h>
File myFile;
bool startState = 0;
float A0_Val;
float A1_Val;
float A2_Val;
float A3_Val;
float A4_Val;
float A5_Val;
//Chip Select pin on the Feather is relegated to pin 4, no need to assign it
String command = "";
char myFileName[] = "DATALOG00.CSV";
int Points = 20;
void setup() {
Serial.begin(9600);
SD.begin();
SPI.begin();
while (!Serial);
//SD.remove(myFileName);
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
//SD.remove(myFileName);
for (uint8_t i = 0; i < 100; i++){
myFileName[7] = i/10 + '0';
myFileName[8] = i%10 + '0';
if (! SD.exists(myFileName)) { //only open the new file if it doesn't exist
myFile = SD.open(myFileName, FILE_WRITE);
Serial.println("writing to the file");
Serial.println(myFileName);
Serial.println(myFile);
break;
}
}
if (! myFile) {
Serial.print("Couldn't create the file ");
Serial.println(myFileName);
}
}
void loop() {
//delay(100);
while(!startState){ //wait here to until s command is sent via Serial
command = Serial.readStringUntil('\n'); //this reads in full serial buffer to command up to a return line character
if(command[0]=='s'){
startState = 1;
}
};
for (int i = 0; i <= Points; i++){
A0_Val = analogRead(A0);
A1_Val = analogRead(A1);
A2_Val = analogRead(A2);
A3_Val = analogRead(A3);
A4_Val = analogRead(A4);
A5_Val = analogRead(A5);
float VA0 = A0_Val * (3.3 / 1023.0);
float VA1 = A1_Val * (3.3 / 1023.0);
float VA2 = A2_Val * (3.3 / 1023.0);
float VA3 = A3_Val * (3.3 / 1023.0);
float VA4 = A4_Val * (3.3 / 1023.0);
float VA5 = A5_Val * (3.3 / 1023.0);
Serial.print(A0_Val);
Serial.print(",");
Serial.print(A1_Val);
Serial.print(",");
Serial.print(A2_Val);
Serial.print(",");
Serial.print(A3_Val);
Serial.print(",");
Serial.print(A4_Val);
Serial.print(",");
Serial.println(A5_Val);
//Serial.println(",");
//Serial.println(i);
Serial.println(VA0);
Serial.println(VA1);
Serial.println(VA2);
Serial.println(VA3);
Serial.println(VA4);
Serial.println(VA5);
myFile.print(i);
myFile.print(" ; ");
myFile.print(A0_Val);
myFile.print(" ; ");
myFile.print(A1_Val);
myFile.print(" ; ");
myFile.print(A2_Val);
myFile.print(" ; ");
myFile.print(A3_Val);
myFile.print(" ; ");
myFile.print(A4_Val);
myFile.print(" ; ");
myFile.println(A5_Val);
if (i == Points){
startState = 0;
}
delay(100);
}
myFile.close();
}
Code with no issues:
#include <SPI.h>
#include <SD.h>
File myFile;
String command = "";
int Points = 10;
bool startState = 0;
char myFileName[] = "DATA00.CSV";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
SD.begin();
SPI.begin();
while (!Serial);
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("init failed!");
while (1);
}
Serial.println("init done");
//SD.remove(myFileName); //Clears the file everytime you hit reset or upload the sketch and run it for the first time. Does not clear everytime you type the "s" start command
for (uint8_t i = 0; i < 100; i++) {
myFileName[4] = i/10 + '0';
myFileName[5] = i%10 + '0';
if (! SD.exists(myFileName)) {
// only open a new file if it doesn't exist
myFile = SD.open(myFileName, FILE_WRITE);
Serial.println(myFile);
break; // leave the loop!
}
}
if (! myFile) {
Serial.println("couldnt create file");
}
//myFile = SD.open(myFileName, FILE_WRITE);
if (! myFile ) {
Serial.print("couldnt make the file ");
Serial.println(myFileName);
}
Serial.println("writing to file");
//Serial.println(startState);
Serial.println(myFileName);
}
void loop() {
// put your main code here, to run repeatedly:
while(!startState){ //wait here to until s command is sent via Serial
command = Serial.readStringUntil('\n'); //this reads in full serial buffer to command up to a return line character
if(command[0]=='s'){
startState = 1;
}
};
for (int i = 0; i<=Points; i++){
Serial.println(i);
myFile.println(i);
if ( i == Points){
startState = 0;
}
delay(100);
}
myFile.close();
}
I'm having a hard time finding any differences in the way each SD file is set up. For the first portion of code, the one that doesn't work, it makes it through the setup and can run the loop if I command it, but obviously the file isn't created, and no data is logged. For the code that does work, everything runs properly, the file is created, and the data is written.