#define switch between serial and serial1

// using Streaming.h instead of print(ln) (Arduino library from Mikal Hart)
//#define USB_SERIAL // uncomment = Serial  |  comment = Serial1
  #include <spark_wiring_usbserial.h>
  USBSerial serial = Serial;
  #include <spark_wiring_usartserial.h>
  USARTSerial serial = Serial1;

void setup()
  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:


syntax, which is limiting if you are using someone else’s library and need to edit every call site.

@bko Thank you - I just did not take the time look.