I want to read a string and more floats from SD-card (SLsetup.csv) and store that values to the internal EEPROM.
First I wrote a program to read the figures from SD-card; it functions fine:
//===========================================================================
//Import parameters from SD-card (SLsetup.csv), convert them to float-variables (and put them in EEPROM on board of Arduino 2560 Mega)
void Import_parameters(){
int intN=1;
byte ASCIIvalue;
String strBuffer="";
float sngTempvalue;
//Open the file for reading:
myFile = SD.open("SLsetup.csv");
//Read projectname as string, N has to be NumberOfCharacters +2 (so including CR and LF)
for (intN=1; intN<=13; intN++)
{char in_char = myFile.read();
int intASCIIvalue = int(in_char);
strBuffer += in_char;}
Serial.print("Arduino Received: "); Serial.print(strBuffer); Serial.print("\n");
strBuffer = ""; // Clear received buffer
//Read floats
while (myFile.available()) {
char in_char = myFile.read();
strBuffer += in_char;
if (in_char=='\n') {
Serial.print("Arduino Received: "); Serial.print(strBuffer); Serial.print("\t"); Serial.print("Floatwaarde = "); Serial.print(StrToFloat(strBuffer),8); Serial.print("\n");
strBuffer = ""; // Clear received buffer
} // End if
} //End while
// close the file:
myFile.close();
} //End of function
//============================================================================
float StrToFloat(String str){
char carray[str.length() + 1]; //determine size of the array
str.toCharArray(carray, sizeof(carray)); //put str into an array
return atof(carray);
}[/color]
Lateron I made some changes, but the result is that the program comes in a loop (only the string and the first float will be read)!
//===========================================================================
//Page 26, Import parameters from SD-card (SLsetup.csv), convert them to float-variables (and put them in EEPROM on board of Arduino 2560 Mega), written on 20130531
void Import_parameters(){
int intN=1; byte ASCIIvalue; String strBuffer=""; float sngTempvalue;
char output[] = " ", charArray[]="", EOL='\n', in_char;
File myFile;
int intRow=1, intStarttime, intEndtime;
intStarttime=millis();
//Open the file for reading:
myFile = SD.open("SLsetup.csv");
//Read projectname as string, N has to be NumberOfCharacters +2 (so including CR and LF)
for (intN=1; intN<=21; intN++){
char in_char = myFile.read();
strBuffer += in_char; } //End for loop
//Store projectname to EEPROM
strBuffer.toCharArray(charArray, 21); //conversion of string (strBuffer) into characterarray. Aantal moet 2 hoger zijn dan aantal letters
Serial.print(charArray); Serial.print("\n");
EEPROM.writeBlock(0, charArray, 21);
strBuffer = ""; // Clear received buffer
//Read floats
while (myFile.available()) {
char in_char = myFile.read();
Serial.print(in_char); Serial.print("\n");
strBuffer += in_char;
if (in_char=='\n') {
//Store floats to EEPROM
EEPROM.writeFloat(intRow*4+16, StrToFloat(strBuffer));
Serial.print(StrToFloat(strBuffer)); Serial.print("\n");
intRow++;
strBuffer = ""; // Clear received buffer
} // End if
} //End while
// close the file:
myFile.close();
intEndtime = millis();
Serial.print (intEndtime - intStarttime); Serial.println();
Serial.print (EEPROM.readBlock(0, output, 19)); Serial.println();
Serial.print (EEPROM.readFloat(20)); Serial.println();
Serial.print (EEPROM.readFloat(24)); Serial.println();
Serial.print (EEPROM.readFloat(28)); Serial.println();
Serial.print (EEPROM.readFloat(32)); Serial.println();
intEndtime=millis();
Serial.print(intEndtime - intStarttime); Serial.print('\n');
} //End of function
//============================================================================
float StrToFloat(String str){
char carray[str.length() + 1]; //determine size of the array
str.toCharArray(carray, sizeof(carray)); //put str into an array
return atof(carray);
}