How do i extend this?
Begin with getting rid of the String class and all instances of it.
void loop()
{
serialReader();
}
Unless this is the start of a larger project, or trimmed down code to illustrate a problem, calling just one function from loop() is unnecessary overhead.
inByte = Serial.read();
You don't know that there is anything to read.
if (inByte > 0 && inByte != terminatingChar) { //If we see data (inByte > 0) and that data isn't a carriage return
I'd recommend NOT using compound statements in this if statement. Nested ifs, in this case, are far easier to understand.
delay(10); //Allow serial data time to collect (I think. All I know is it doesn't work without this.)
You really need to understand why, and get rid of this. Waiting around for each character is a waste of time.
Looping UNTIL a specific character arrives is a whole different story.
This code will read all the serial data and store it in an array, without a single String or delay:
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
Serial.begin(57600);
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
You can change the SOP and EOP values (or remove the SOP and started stuff),
You can move the while code to a separate function, in a while(!ended) loop, and return when ended is true.