Hi,
I searched a solution to read string into a dynamic array and I found "DynamicArrayHelper" for easier malloc-usage in the playground.
http://www.arduino.cc/playground/Code/DynamicArrayHelper
But I have to admit that I neither understand its usage nor understand the usage of the string-converter atoi().
Could anyone possibly help?
This is the code of what I wanted to do. Nothing works here...
#include <DynamicArrayHelper.h>
// #include <wiring.h>
struct sColor
{
unsigned int *& steps_value;
int cnt_steps;
};
typedef struct sColor tColor;
DynamicArrayHelper DAH;
tColor colors[3];
String input[3] = {"101 102 103 104 105 106 # #",
"201 202 203 204 # #",
"301 302 303 304 305 306 307 # #"};
int nextSpace(String str, int actPos)
{
int pos = actPos;
while (str.substring(pos,pos+1) != " ") {pos++;};
return pos;
}
void read_array()
{
for (byte col = 0; col < 3; col++)
{
int index = 0;
int startpos = 0;
int value;
int nxtSpace = nextSpace(input[col],startpos);
String item = input[col].substring(startpos, nxtSpace);
while (item != "#")
{
value = atoi(item);
DAH.SetElementInArray((void *&)colors[col].steps_value, &value, index, 1, sizeof(int));
index++;
startpos = nxtSpace + 1;
nxtSpace = nextSpace(input[col],startpos);
item = input[col].substring(startpos; findSpace(input[col],index));
}
colors[col].cnt_steps = index;
}
}
void setup()
{
Serial.begin(9600);
read_array();
Serial.println(colors[2].steps_value[2]); // 303
}
void loop()
{
}