Hey first time posting on these forums, so please forgive any faux pas.
I'm a college student preparing for the SAE SuperMileage competition, and we want to use arduinos as our ECU's. It has two LCD displays to show the speedometer, odometer, fuel level, etc. It also has an SD card to record all that data so we can review and analyze test runs. The only problem is, I can only write to the SD card if the first thing I write is a short enough string. Here's a code snippet from a function I wrote to initialize and add headings to the txt file.
boolean initializeSD() {
lcd.clear();
lcd.print("Initializing SD card");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(chipSelect, OUTPUT);
lcd.setCursor(0,2);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
lcd.println(" Card failed ");
// don't do anything more:
return false;
}
lcd.print(" Card initialized ");
lcd.setCursor(0,3);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("Time(ms) Speed(mph)");
dataFile.close();
lcd.print(" File Ready ");
return true;
}
else
{
lcd.print(" Cannot Open File ");
return false;
}
I would expect the code in the if statement to execute if the file was able to be opened, but whether or not the code executes seems to change based on the first line WITHIN THE IF STATEMENT!?!
If I make that dataFile.println try to write a string under 16 characters, it works fine, but if it is over 16 characters, it goes to the else statement instead, and displays my error message! I checked the sd card, and it created the file, but there is nothing written in it.
How can code within an if statement change whether or not the if statement is executed?
Thanks in advance guys