Hi.
I m using the code from Serial basics for parsing data.
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
char messageFromPC[numChars] = {0};
int a = 0;
int b = 0;
int c = 0;
int d = 0;
boolean newData = false;
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
newData = false;
}
}
/////////////////////////////////////////////////////////////////////
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0';
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
///////////////////////////////////////////////////////////////////
void parseData() {
char * strtokIndx;
strtokIndx = strtok(tempChars," ");
strcpy(messageFromPC, strtokIndx);
strtokIndx = strtok(NULL, " ");
a = atoi(strtokIndx);
strtokIndx = strtok(NULL, " ");
b = atoi(strtokIndx);
strtokIndx = strtok(NULL, " ");
c = atoi(strtokIndx);
strtokIndx = strtok(NULL, " ");
d = atoi(strtokIndx);
trellis.setPixelColor(a, rgbColor(b,c,d));
The thing is, Im no efficient with the transfer of data. Im sending an ¨a¨ at the beginning of the data
string because at parseData I cant change the first part of the string (messageFromPC) to an integer.
I try atoi in different places with no luck....
Anyone?