You've been told not to use String
before:
Jobi-Wan:
You can save some by not using Strings.
There are many reasons we are telling you this. You have experienced this common timeline of String
usage. If you really want to know why, read all this (especially The Evils of Arduino Strings). You don't have to understand this advanced information to stop using String
.
Here are some examples to get you started. Instead of this:
for(byte i = 0; i < TimeTypeCount; i++) {
//OPENING THE FILE
File CV = SD.open("/SF" + String(i) + ".DAT");
//READING CONTENT
while (CV.available()) {
if(char(CV.read()) == '\n') {
WritingCursor[i]++;
}
}
}
... do this:
char filename[] = "/SF-.DAT";
for(byte i = 0; i < TimeTypeCount; i++) {
//OPENING THE FILE
filename[3] = i + '0'; // replace the '-' with a digit
File CV = SD.open( filename );
//READING CONTENT
while (CV.available()) {
if(char(CV.read()) == '\n') {
WritingCursor[i]++;
}
}
}
Instead of these two functions:
void GetValues () {
if(Serial.available()) {
InputValue = "";
while(Serial.available()) { //mySerial.available()
InputValue = (InputValue + char(Serial.read())); //mySerial.read()
}
stringconv = new char[InputValue.length()];
InputValue.toCharArray(stringconv,InputValue.length());
counter2 = 0;
Value[0] = Analizing(0,InputValue.length(),',').toFloat();
Value[1] = Analizing(counter2,InputValue.length(),',').toFloat();
Value[2] = Analizing(counter2,InputValue.length(),',').toFloat();
Value[3] = Analizing(counter2,InputValue.length(),'\n').toFloat();
}
}
String Analizing (short Initializer, short Lenght, char Character) {
String Value = "";
for(short x = Initializer; x < Lenght; x++) {
if(stringconv[x] == Character) {
counter2=x+1;
break;
}
Value = (Value+stringconv[x]);
}
return Value;
}
... just do this:
bool GetValues () {
bool lineReceived = false;
while(Serial.available()) { //mySerial.available()
char c = Serial.read();
if (c == '\n') { // newline means the line is all here!
lineReceived = true;
InputValue[ InputCount ] = '\0'; // NUL-terminate the C string
// Parse the line
char *token = strtok( InputValue, ',' );
Value[0] = atof( token );
token = strtok( NULL, ',' ); // get the next token
Value[1] = atof( token );
token = strtok( NULL, ',' );
Value[2] = atof( token );
token = strtok( NULL, ',' );
Value[3] = atof( token );
InputCount = 0; // reset the count
} else {
// Not a newline, save another character (if there's room)
if (InputCount < sizeof(InputValue)) {
InputValue[ InputCount++ ] = c;
}
}
}
return lineReceived;
}
GetBytes
is based on the "Serial Input Basics" (see Useful Links). It returns true
when the complete line has been received.
Instead of waiting 5 seconds and then parsing the line, keep calling GetBytes
until it returns true
. Then you know a new command was received. loop
should use it like this:
if (GetValues()) {
if(TCursor0+1 == 12) {
TCursor0 = 0;
if(TCursor1+1 == 5) {
...
}
}
}
} // loop
Then you don't need current/previous millis.
You don't even need "Analizing". The strtok and atof functions do that for you, using a char array (aka C string). There is a C string version of every String
function you are using. If you're not sure how to convert a String
to a C string, just ask. There are usually several ways to do the same thing. Tutorials here, here and here.
When all the String
variables are gone, you program will be much smaller, faster and more reliable.
Cheers,
/dev