Hey all,
I've got a real brain scratcher going on here. I've take the time to write my own functions that parse a string of comma separated number into an array of individual float values.
The program works well the first time through and parses everything correctly. However on the second loop it seems to get stuck. I've used Serial.print's to narrow it down to the point where it calls
int numberofvalues = numberofvaluesinstring(stringOne);
The function is called but if I put a Serial.print at the beginning of the numberofvaluesinstring function, it "locks up" and doesn't start that function even though it did it the first loop.
I've added a Serial.print command to the beginning and end of each function so I can debug where the problem is. The code is as follows:
void setup() {
// put your setup code here, to run once:
// 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
}
}
void loop() {
String stringOne = "1,2,3,12.5,123,543,1234,1,1,1"; //String to Parse
Serial.println("Start of loop");
// Code from here down to next mark must be copied over along with parser function
int numberofvalues = numberofvaluesinstring(stringOne);
float values[numberofvalues]; //Define array size to hold parsed numbers
parser(stringOne, numberofvalues, values); //Call parser function
//Optional display parsed values in order
for(int q=0; q<numberofvalues;q++) //Display all values parsed
{
Serial.println(values[q]);
}
Serial.println("End of Loop");
Serial.println("");
}
int numberofvaluesinstring(String stringOne) //Take String of values and return the number of comma separated values
{ Serial.println("Numberofvaluesinstring Start");
int stringlength = stringOne.length(); //How many characters are in the string?
int dividercount = 0; //Counter to count the number of separators
for (int n = 1; n<=stringlength; n++) //Increment through the string and count the separators
{
if (stringOne.charAt(n)==',')
{
dividercount = dividercount + 1;
}
}
int numberofvalues = dividercount + 1; //Number of values are always one more than separators
Serial.println("Numberofvaluesinstring End");
return numberofvalues;
}
void parser(String stringOne, int numberofvalues, float values[]) //Break the string down into invidual float values
{
Serial.println("parser Start");
int separatorindex[numberofvalues]; //Store positions of separators in this array
String substrings[numberofvalues]; //Break main string into substrings in this array
separatorindex[0] = stringOne.indexOf(','); //Store position of first separator
for(int i=1; i<(numberofvalues-1); i++)
{
separatorindex[i] = stringOne.indexOf(',',separatorindex[i-1]+1); //Store position of following separators
}
substrings[0] = stringOne.substring(0,separatorindex[0]); //Store first value before first separator to a substring array
values[0] = substrings[0].toFloat(); //Convert first substring to a float
for(int m = 1; m<=numberofvalues; m++)
{
substrings[m] = stringOne.substring(separatorindex[m-1]+1, separatorindex[m]); //Store subsequent values between separators
values[m] = substrings[m].toFloat(); //Convert it value to a float and store in values array to be refereced back in program.
}
Serial.println("Parser End");
and the output is as follows:
Start of loop
Numberofvaluesinstring Start
Numberofvaluesinstring End
parser Start
Parser End
1.00
2.00
3.00
12.50
123.00
543.00
1234.00
1.00
1.00
1.00
End of Loop
Start of loop
as you can see, it refuses to go and start the numberofvaluesinstring function. There is literally nothing between the "Start of loop" and "Numberofvaluesinstring Start" prints that would cause it to stop.
Any thoughts?
Thanks
Jim
P.S. Thinking it might be a memory error, I tried running it on a Mega and I get the same issue. According to the compiler, I'm not anywhere close to using all the SRAM....I think it's something like 320 bytes.