I'm working on an interpreter, so I started with the serial event example from the IDE:
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
but I'm just not a fan of the String or the std::string classes, so I revised it a bit:
There are threads that incorporate a start and stop character, but as this is just for serial monitor input, I didn't see any of that as necessary for this application.
It is; you can print a c-string in hex including the terminating character to see
But I think that '\0' is cleaner when using c-strings.
Same thing as with conversion of numeric characters to number. Subtracting '0' is cleaner (my opinion) than subtracting 48 or 0x30 although the effect is the same; but all three will give the same result.
I had considered that.
Here's the question that came with it.
How heavy does a block of code need to be to benefit being put into a function?
Though not as critical here, my next project will have to consider that question more.
I am. It's a great article that works for a lot of boards.
This is important for universal applicability. I'm just coding this for the 328 and 2650, so it's ok to use it, but I do recognize the importance of knowing how to do it without the event.