I'm working on a new project (diagnostic tool) that can be operated with a desktop (verbose mode) or with a portable power supply (output only mode).
Thing is, I won't be needing the verbose Serial.print lines if I will be using it without a desktop, so I wrote a function to test whether or not the program should print something. I am trying to save SRAM and shave off execution time due to unneeded Serial.print calls.
void setup()
{
// some code here
}
void loop()
{
// some code here
SerialPrintWrap("I can't get it to work.");
}
void SerialPrintWrap(char conditionalPrint[])
{
if(digitalRead(verbosePin) == HIGH)
{
Serial.println(F(conditionalPrint));
}
}
The F() macro keeps string literals from being copied from flash memory to SRAM.
It is NOT to be used around arguments passed to the function. It CAN be used in the call to the function, but then the function needs to be defined differently.
The Print class is what Serial derives from. Look at how it deals with strings in SRAM and in flash. Two different functions, and you (or the compiler) must call the correct one.