Hi ;
This maybe a little long winded . My intent is to assign variables inside a program by reading values from a text file on an SD card. This is to avert recompiling every time I need to change these values. Below are the fields listed in the text file .
test_time_hrs 3
delay 1000
wind_min_volt .41
What I want to do is read each line into a string variable and then parse it to recover the field name and value ie field name on the first line would be test_time_hrs and field value is the 3. Then using conditional statements, match the field name and assign the field value. I am able to read in the string using l_line = test_data.readStringUntil('\n');
however I'm having trouble determining the length using m=sizeof(l_line);
so that I can parse it using while() or for(). It seems to give the literal length of l_line which is 6. Enclosed is my code. I spent a lot of time on this so any help would be appreciated .
Thanks
Justin
#include <SD.h>
#include <SPI.h>
File test_data;
String variable;
String l_line="";
int test_duration_hrs;
int time_delay;
double voltageMin;
int num = 0;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// ***********************SD card initialization
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(4)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
if (SD.exists ("test.txt"))
{
test_data = SD.open("test.txt", FILE_READ);
while (test_data.available()) {
l_line = test_data.readStringUntil('\n');
// l_line=test_data.read();
//l_line+="\n";
Serial.println( l_line);
if (l_line != "");
{
Serial.println(l_line);
int m, n =0 ;
m=sizeof(l_line);
Serial.println("sizeof m " +String(m));
String r;
variable = "";
while (n<m) //l_line[n]!="\n")
{
//variable+=l_line[n];
//Serial.print(String(n));
if (l_line[n] != " ")
{
variable += l_line[n];
n++;
}
else
{
r = variable;
Serial.println(r);
while (l_line[n] = " ")
{
n++;
}
}
}
}
Serial.print(variable);
}
}
}
void loop() {
}
SD_text_read.ino (1.63 KB)