Hello,
I'm working with an arduino mega 2560. I have a string and 4 ints that I'm saving to my SD card. The string is an rfid card scan and the 4 ints are bools that control 4 relays. I am saving the data to a file on the card as a comma delimited string that looks like this: "1234567,0,0,1,1". When I power down the device and take out the card, the values are all stored correctly on the file. They are saving to the sd card in case of a power failure. I didn't want to use EEPROM because I need the data to be as up-to-date as possible, and every time an led comes on or a card is scanned, the file gets removed then created and re-populated with the new string.
My problem is, when I try to read the contents of the file, right now I'm just doing Serial.println(f.read()); all I get is a bunch of lines with "-1" on them.
Here is a scaled down version of the code. this should only be code for the file handling. the rest of my code just calls the sdWrite() function:
#include <Arduino.h>
#include <PLDuino.h>
#include <SimpleTimer.h>
#include <SPI.h>
#include <SD.h>
#define FILENAME "data.tmp"
bool sdInit()
{
if (!SD.begin(PLDuino::SD_CS))
{
Serial.println("Failed to initialize SD card. Please check it.");
return false;
}
return true;
}
bool sdWrite(bool fileCreate)// Trying to create a file
{
Serial.println("Creating a file...");
if(SD.exists(FILENAME))
{
Serial.println("remove");
SD.remove(FILENAME);
}
// Opening a file for writing...
File f = SD.open(FILENAME, FILE_WRITE);
if (!f)
{
// Stop on error
Serial.println("Failed to create " FILENAME " file. Stop.");
f.close(); // close when done
return false;
}
else
{
Serial.println("create");
if(!fileCreate)
{
Serial.println("write file");
f.print(RFID);
f.print(",");
f.print(led1);
f.print(",");
f.print(led2);
f.print(",");
f.print(led3);
f.print(",");
f.print(relay);
f.println();
}
}
f.close(); // close when done
return true;
}
String sdRead()// Trying to read a file
{
Serial.println("Re-opening the file for reading...");
// Trying to open our file.
File f = SD.open(FILENAME, FILE_READ);
if (!f)
{
// Stop on error
Serial.println("Failed to open newly created file " FILENAME ". Stop.");
f.close(); // close when done
return "false";
}
Serial.println("File contents: ");
// Read out file contents and print them to Serial.
String fileString = "";
int count = 0;
while(!f.available())
{
//fileString += f.read();
Serial.println(f.read());
if(count > 100)
{
return "";
}
count++;
}
Serial.println("end of open");
return fileString;
f.close(); // close when done
}
void setup()
{
if(initOK)
{
bool initOK = sdInit();
Serial.println("initOK");
bool writeOK = sdWrite(true);
if(writeOK)
{
Serial.println("writeOK");
String readStr = sdRead();
if(readStr != "")
{
readStr.trim();
String readParse[5] = {"","","","",""};
int counter = 0;
String temp = "";
for(int x = 0; x < readStr.length(); x++)
{
char readChar = readStr.charAt(x);
if(readChar == ',')
{
readParse[counter] = temp;
temp = "";
counter++;
}
else
{
temp += readChar;
}
}
RFID = readParse[0];
setVars(readParse[1].toInt(), readParse[2].toInt(), readParse[3].toInt(), readParse[4].toInt(), 0, 0);
}
}
}
}