Hello all,
I am basically fighting with Arduinos here.
I am trying to work with serial data.
What I am trying to do is simply collect 24 bytes into to a String called cmd.
Then I try to process the cmd to get all the data I need. My code worked great until the Arduino started to do all kind of weird things.
Before I show you the code, I want to say that at the first version I worked only with "Strings". After some internet research I found that the String library is not so perfect and with a lack of memory the device starts to get crazy. So I switched almost everything into to char*.
Now this is the code:
char* token[MAX_TOKEN];
char* fun[MAX_FUN];
char* device[MAX_FUN];
char* option[MAX_FUN];
char* timer[MAX_TIMER];
void serialEvent() {
while (Serial.available())
{
char inChar = (char)Serial.read();
cmd.concat(inChar);
if(inChar == '&') // THE SIGN '&' ZERORIZE THE CMD
{
cmd = "";
}
if(cmd.length() > CMD_BYTES) // the cmd has to be CMD_BYTES long!
{
cmd = cmd.substring(0,CMD_BYTES);
}
if (cmd.length() == CMD_BYTES) // only if the cmd is CMD_BYTES long cmd is being proccess!
{
strcpy(*token, cmd.substring(1,8).c_str());
// strcpy(*fun, cmd.substring(8,10).c_str());
/*
strcpy(*token, cmd.substring(1,8).c_str());
strcpy(*fun, cmd.substring(8,10).c_str());
strcpy(*device, cmd.substring(10,12).c_str());
strcpy(*option, cmd.substring(12,14).c_str());
strcpy(*timer, cmd.substring(14,24).c_str());
*/
sw = true;
Serial.println("Command is out of the oven!");
cmd = "";
}
}
}
Now this code works only when one strcpy line is not commented, and ever so it shows me things like : "êÞ
Ï']ø`5¿ï¶,Ö#~\Xÿäóÿ½?þ
bð,ÿu¿²ï¢½íÉm»
oeå"
Now I anderstand that I might be out of SRAM lack.
So what is the best way to work with Arduino and strings?\
**I am working with duemi' and pro mini 3.3 8Mhz
Thank you!