So, I've been writing a function that shall receive a String working as a data package in the following format for example "2:n;12:n;11:n;", ";" splits individual command couples, where the first element (a led to be lit up) is divided from the second element (a time variable, 0 for 50ms;1 for 200;anything for no time) by a ':'.
This is what the function looks right now:
void processpkg(String pkg){
char input [PKG_SIZE + 1] ;
byte size = Serial.readBytes(input, PKG_SIZE);
//pkg.getBytes(input,PKG_SIZE);
input[size]=0;
Serial.println(input);
char* command = strtok(input,";");
while (command != 0){
char* separator = strchr(command, ':');
if (separator != 0){
*separator = 0;
int LED = atoi (command);
++separator;
int TME = atoi (separator);
digitalWrite(led[LED], HIGH);
switch (TME) {
case 0:
delay(tempo[0]);
break;
case 1:
delay(tempo[1]);
break;
default:
break;
}
}
command = strtok(0, ";");
}
}
As you can clearly see by the use of byte size = Serial.readBytes(input, PKG_SIZE); it was built to receive data from the Serial monitor, not from a string in a function model. I need to adapt it to work in that model, I tried using String.getBytes as it sounded like a logic alternative, but that didn't work. I'm not so sure if that idea failed because it's completely wrong or because I implemented it badly. How can I do this ?
PS.:If you want the whole test code:
#define PKG_SIZE 30
int tempo [] = {50,200,0,0};
int led [] = {2,12,11,4,13,10,7,8,9};
int col[] = { 6,5,3};
void setup() {
//
Serial.begin(9600);
// CONFIGURAÇÃO DOS PINOS "SAIDA" led[0]
for (int l = 0; l<=8;l++)
{
pinMode(led[l],OUTPUT);
}
for (int i = 0; i<=2;i++)
{
pinMode(col[i],OUTPUT);
}
tempo[2] = 0;
tempo[3] = 0;
Serial.println("Setup Done, code starting... ");
}
void loop() {
digitalWrite(col[0], HIGH);
digitalWrite(col[1], HIGH);
digitalWrite(col[2], HIGH);
processpkg("2:n;12:n;11:n;4:n;13:n;10:n;7:n;8:n;9:n");
}
void processpkg(String pkg){
char input [PKG_SIZE + 1] ;
byte size = Serial.readBytes(input, PKG_SIZE);
//pkg.getBytes(input,PKG_SIZE);
input[size]=0;
Serial.println(input);
char* command = strtok(input,";");
while (command != 0){
char* separator = strchr(command, ':');
if (separator != 0){
*separator = 0;
int LED = atoi (command);
++separator;
int TME = atoi (separator);
digitalWrite(led[LED], HIGH);
switch (TME) {
case 0:
delay(tempo[0]);
break;
case 1:
delay(tempo[1]);
break;
default:
break;
}
}
command = strtok(0, ";");
}
}