Output to Nano Every Tx/Rx pins

I have used the Nano and now trying to use the same code on a Nano Every. I've read about the differences a bit. I read one difference is the Tx/Rx lines are Serial1 on the Every and Serial on the Nano.

I have a little cheap project using PIR sensor and buzzers to keep pack rats out of areas of my yard, shed, under the hood of cars parked outside, etc. I wanted to be able to walk up and plug into the tx/rx pins with a laptop and see status, how many times triggered, etc. I had to use just tx/rx so when plugging in it wouldn't trigger a reset and I loose everything. I'm using a USB to serial converter for this. This works fine on the Nano

(I'm still learning and have not yet spent the time to get wifi/IOT boards working which would eliminate this need, I know. Also, trying to keep these things cheap, run them on the car battery so like the low cost of simple Nano Every).

I can get the Nano Every Tx/Rx lines to output by a Serial1.print cmd instead of a Serial print.
However, I also need the serial print commands for debug, working on the IDE.
So, now I have to duplicate every print command to Serial and Serial1 command in the sketch if I want it to be visible on IDE and on the serial output (I'm using Putty for serial Tx/Rx output).

Googling I see discussions of using stream classes to make this easier somehow but it's a bit above my skills now. I was wondering if there is an easy way to make print statements go to both places which I've missed. If not, the best link/reference on how to do so I can read/learn.

I have used sprintf to create a buffer and print that in some cases. Using this everywhere would make it a bit easier as I'd create the print output buffer once and then just print it twice to serial and serial1. But I read somewhere sprintf wasn't recommended for some reason and I had a learning experience with it overflowing buffer and clobbering something that took me forever to figure out - so a bit hesitant to keep using it. If no worries/concerns with sprintf - perhaps this is easiest/quickest for me at this point. Thoughts on this appreciated.

Any advice, pointing to direction for learning would be also appreciated.

Use snprintf() to avoid buffer overrun errors.

Thanks, appreciate the info and will use that instead of sprintf.

Serial is (still) used for SerialMonitor.
Serial1 is for the separate UART (hardware UART).

You don't have to duplicate. You can use the USB port for the uploads and the TX/RX pins using the adapter. Just do all prints to using Serial1.

For the IDE you have two options

  1. Change the port between upload and debug session; a little inconvenient.
  2. Open a second sketch (e.g. using file → new) and configure it to use the second (Serial1) port. That way you can upload your sketch using the one instance of the IDE and view the results using the second one.

The alternative is to use a 3rd party terminal program that uses the second (Serial1) port to view the results and use the IDE for the upload. This would be my preferred solution.

Thanks. I was able to use Putty to watch the serial1 while also editing in the IDE. That said, it's not as easy to read and most of the output I use to check that I didn't break something, my sketch is doing what it's supposed to - so I decided to leave serial output for debug/checking in the IDE and for the output I want for the low tech status by hooking up a laptop to both serial and serial1. For anything more than a single printf line, I used snprintf to a buffer and just output that buffer twice.
I appreciate the suggestions and ideas - helped me figure out what I wanted to do.

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