// using Streaming.h instead of print(ln) (Arduino library from Mikal Hart)
//#define USB_SERIAL // uncomment = Serial | comment = Serial1
#ifdef USB_SERIAL
#include <spark_wiring_usbserial.h>
USBSerial serial = Serial;
#else
#include <spark_wiring_usartserial.h>
USARTSerial serial = Serial1;
#endif
void setup()
{
serial.begin(9600);
delay(10000);
serial << "key 'r' to start" << endl << "write and read back SD sector " << numberSector << endl << endl;
}
void loop()
{
if (serial.available() > 0)
{
int inByte = serial.read();
if (inByte == 'r')
{
......
hmmm with cpp and a common base class a runtime solution
Is there a common base class? Is so something like:
bool UseUSB = true;
SeriallBaseClass & serial = UseUSB ? Serial : Serial1;
will work.
The base class you are looking for is Stream.
You can also do things like:
Stream * mySerial;
if (something) {
mySerial = &Serial;
} else {
mySerial = &Serial1;
}
but then you have use:
mySerial->write(aByte);
syntax, which is limiting if you are using someone else’s library and need to edit every call site.