Getting rid of Strings

The timing output from the race timing PC running RC-Timing (or other RC timing program) becomes an input to our system on the Arduino serial port (USB port or pins 0 & 1). Up until now I’d been relying on code from the Serial Event tutorial in the official Arduino documentation.

It was when I was trying to add a second serial port to the project that I seemed to run up against some issues with the way data was being stored, for some reason I could inject a small amount of data into the serial buffer myself (and the code would handle the data correctly) but when I connected the Arduino to RC-Timing I was getting or creating garbage. Anyway, I asked a question on the Arduino forum and received an interesting reply from a user called Robin2 (it’s reply #8 in the thread).

Anyway, I figured these people probably aren’t wrong with their views on String objects; I haven’t delved deep into the issue but I was willing to try the alternative (character arrays).

So let’s look at the code:

void serialEvent(){
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = ‘[‘;
char endMarker = ‘]’;
char rc;

// if (Serial.available() > 0) {
while (Serial.available() > 0 && stringComplete == false) {
rc = Serial.read();

if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
rideChars[(ndx + 1)] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars – 1;
}
}
else {
receivedChars[ndx] = ‘\0’; // terminate the string
rideChars[(ndx + 1)] = rc;
rideChars[(ndx + 2)] = ‘\0’;
recvInProgress = false;
ndx = 0;
stringComplete = true;
}
}

else if (rc == startMarker) {
recvInProgress = true;
rideChars[ndx] = rc;
}
}
}

You’ll notice there’s a bunch of local variables declared at the start of the function. These serve various functions – recvInProgress lets the program know whether there is still data to be read in from the serial buffer. ndx is an index variable (used to write to a specific character in the character array). Then we have the two markers, starMarker and endMarker – these indicate to the program when we should start reading into the receivedChars array (which is a global variable declared at the start of the main program), and when we should stop reading into receivedChars. This then resets all the variables other than receivedChars to empty and terminates the array with a ‘\0’ character.

There are three circumstances that the serialEvent() function deals with – is the received character (the variable called rc which is of char type) our start marker, something inbetween the start and end marker, or is it the end marker?

You’ll notice in the code that I also store the received character from the serial buffer into a seperate character array (again, a global variable) called rideChars. This stores the message received with the ‘[‘ and ‘]’ characters added onto the beginning and end of the array. This is for the feature described in the post ‘External display board output using SoftwareSerial’.

Leave a comment