I have a data logger, not finished, but when I try to have the SD.exists() function find a text file (from notepad) named 'setup', it wont find it, Is the SD not formatted correctly, or do I need to initialize the card first or what?
Contents of the SD card:
- setup.txt
I only included the function that the problem is in, this function is called in the setup function if that's useful.
The code:
boolean CardWorking(boolean serialConn) //make sure the card is working properly, and is inserted
{
//give the user time to check the SD card.
//delay(2000);
if (SD.exists("setup.txt")) //the setup file is present
{
if (serialConn) //card works and there is serial
{
//serial, file, good thing
//create a temporary serial connection.
Serial.begin(9600);
//notify the user.
Serial.println("The SD card is working properly.");
Serial.println("The setup file is present.");
Serial.end();
}
else if (!serialConn) //card works and there is no serial, so use the leds in the circuit
{
//no serial, file, good thing
//blink the leds to show that it worked.
for (int i; i <= 3; i++)
{
digitalWrite(dataSaveLed, HIGH);
digitalWrite(sensorLed1, HIGH);
digitalWrite(sensorLed2, HIGH);
digitalWrite(sensorLed3, HIGH);
delay(300);
digitalWrite(dataSaveLed, LOW);
digitalWrite(sensorLed1, LOW);
digitalWrite(sensorLed2, LOW);
digitalWrite(sensorLed3, LOW);
delay(300);
}
}
//it works, so return true.
return true;
}
else if (!SD.exists("setup.txt")) //the setup file is missing or the card is not inserted
{
if (SD.begin(4)) //the card works, but the file is missing
{
if (serialConn) //file missing but there is serial
{
//serial, no file, error
//create a temporary serial connection.
Serial.begin(9600);
//notify the user.
Serial.println("The SD card is working properly.");
Serial.println("The setup file is not present. Please check the card and reset to try again.");
Serial.end();
}
else if (!serialConn) //no file and no serial
{
//no serial, no file, error
}
//it works, but there is no file, so return false.
return false;
}
else if (!SD.begin(4)) //the card is not working, probably not inserted
{
if (serialConn) //card does not work, but there is serial
{
//serial, card broken, error
}
else if (!serialConn) //card does not work and there is no serial
{
//no serial, card broken, error
}
//because it does not work, return false.
return false;
}
else //something else is wrong... backup plan for any errors that fall through the statements, just use a general error message
{
//general error statement, maybe a blinking led or two
if (serialConn) //there is serial, so tell the user over serial
{
}
else //there is no serial, so use the leds.
{
}
//because it didn't work, return false.
return false;
}
}
}