I am trying to find out why my code to write to an SD card is not working. I am not sure if I am not writing to the card right or reading from the card as it seems like the program does successfully write to start with.
The code to write is shown below. One question I have concerns the line myFile.print('\r');
I have it there because when I do a read I stop when I encounter a \r. But should I use "\r" or '\r' or doesn't it make a diference?
Also, kilowattHourt is a float. Should I be using myFile.println(kilowattHourt, 2); instead?
myFile = SD.open("killog.txt", FILE_WRITE); //open file for writing
delay(1000);
myFile.seek(0); //set to always write to first byte of file
myFile.println(kilowattHourt); //kilowattHourT format xx.yy
myFile.print('\r');
myFile.close(); } //save kilowattHour to file
Code to read first float from file:
//myFile = SD.open("killog.txt"); //killog.txt holds kilowatt hour running total
j = 0;
do {
// test for kwh full
if (j == sizeof(kwh)) {
Serial.println(F("line too long"));
break; }
kwh[j] = myFile.read(); }
while (kwh[j++] != '\r');
kilowattHourt = atof(&kwh[0]); //convert read string to float
//Serial.print(F("KWH from SD "));
//Serial.println(kilowattHourt);
myFile.close();}
Thanks guys for your help.
JC