I would like to be able to read configuration parameters for a datalogger application from a text file stored on an SD card -- something like this:
3,3.3,9600,OUTFILE.CSV
I know how to read this string from a file and even how to break it into separate strings using the commas as delimiters. In other languages it would be easy to process this string as int, float, int, and string values. But the Arduino language seems to have no capability to convert the strings containing numbers into their numerical values. Is there a way to do this? Perhaps with atoi() and atof(), but I can't get that to work. Can you help?
File myFile;
char fileName[]="CONFIG.TXT";
String configString="3,9600,3.3,logger_file";
int fileByte,commaPosition;
//char * x[20];
int a1,a2;
float a3;
//char x4[20];
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
Serial.println(configString);
Serial.print("length: ");
Serial.println(configString.length());
do {
commaPosition=configString.indexOf(',');
if (commaPosition!= -1) {
Serial.println(configString.substring(0,commaPosition));
configString=configString.substring(commaPosition+1,configString.length());
}
else {
if (configString.length()>0) {
Serial.println(configString);
}
}
} while (commaPosition>=0);
}
void loop()
{
}
Here is the output:
Initializing SD card...initialization done.
3,9600,3.3,logger_file
length: 22
3
9600
3.3
logger file
What Now? How can I interpret 3 and 9600 as integers, 3.3 as a floating point number, and logger file as a string? Nothing that I've tried has worked at all.
Yes, I have tried both String and string, and atoi() and atof(). I have also tried using Serial.parseInt() to get the integer value before the first comma as I read the file -- I will worry about the floats later. I'm sure some combination of these things will work, but I certainly can't figure it out.
Arrch:
Ditch the use of Strings unless you want support from only 1 person on these forums (zoomkat).
Post the code you tried that involve strings, and use CODE tags.
You got code? Didn't think so! Good to see you direct the support of the other forum members.
Below is some old servo code that converts a String (readString)into an integer.
// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.attach(9);
}
void loop() {
while (Serial.available()) {
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(3);
}
}
if (readString.length() >0) {
Serial.println(readString);
int n = readString.toInt();
Serial.println(n);
myservo.writeMicroseconds(n);
//myservo.write(n);
readString="";
}
}