Change Serial# on the fly

Sorry if the subject description is a bit misleading. But what I want to do is to "direct the output" to a different Serial# based on a variable but in a way so I don't have to code everything twice. Let me give an example:

if (x)
  SerialUSB.println("This is the response bla bla bla bla ...");
else
  Serial1.println("This is the response bla bla bla bla ...");

In my project I can have MANY response strings, current max is 27, of response strings per condition. So having huge duplicated sections is difficult to maintain and also take a lot of editor space not to mention room in the AZ.

Thus I would like something like

if (x)
  SerialX = SerialUSB;
else
  SerialX = Serial1;

SerialX.println(responseStr);

Can this be done?

TIA
MikD

Yeah. Make serialX a “stream” variable...

Thanks for the hint. which I used to find this: c++ - Writing First Library - Serial Stream Object - Arduino Stack Exchange

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.