Hey I am using the SD Card Library from here:
http://code.google.com/p/sdfatlib/I am having a problem when reading the SD Card more than once. What I want to happen is when I click a "Sync Button", it should sync with the SD Card, and store values into an array. However, what happens is I power on my Arduino, Put SD Card in, Hit Sync Button, it shows me the proper values that is on the SD Card, and then it continues with the program. However, when I remove the SD Card, change one of the values on my computer, stick it back into the SD slot on the Arduino, click the sync button again, it gives me these random values. I have to reset the Arduino for it to read the changed values.
I used the error handler that was given and whenever I run it the second time it prompts "error: openRoot failed".
This is the Code that I used:#include <SdFat.h>
#include <SdFatUtil.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd (2, 3, 4, 5, 6, 7);
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
#define error(s) error_P(PSTR(s))
void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}
int current_setpoint;
int cool_setpoint_values[7];
int heat_setpoint_values[7];
void setup()
{
Serial.begin(9600);
lcd.begin(20, 4);
}
void SDRead()
{
if (!card.init(SPI_HALF_SPEED)) error("card.init failed");
if (!volume.init(&card)) error("volume.init failed");
if (!root.openRoot(&volume)) error("openRoot failed");
if (file.open(&root, "VALUE00.TXT", O_READ)) {
int val[14];
int index = 0;
int16_t n;
uint8_t buf[14];
while ((n = file.read(buf, sizeof(buf))) > 0) {
for (int i = 0; i < n; i++){
if ((int)buf[i] - 48 != -35 && (int)buf[i] - 48 != -38) {
val[index] = (int)buf[i] - 48;
index++;
}
}
}
for(int i = 0, index = 8; i <= 7; i += 2, index += 2) {
cool_setpoint_values[i] = val[i] * 10 + val[i+1];
heat_setpoint_values[index] = val[index] * 10 + val[index+1];
lcd.clear();
lcd.print(heat_setpoint_values[index]);
delay(2000);
lcd.clear();
}
}
else
error("file.open failed");
}
void sync_sd_card_data()
{
const int sync_button = 14;
pinMode(sync_button, INPUT);
int sync_state = digitalRead(sync_button);
if(sync_state == 1) {
lcd.clear();
lcd.print("Syncing...");
delay(1000);
SDRead();
lcd.clear();
lcd.print("You may now remove the SD Card.");
delay(1000);
lcd.clear();
}
}
void loop()
{
sync_sd_card_data();
}