+1 vote
8.2k views
in Arduino by
Is it possible to detect Serial1 is present on an arduino board, and if Serial1 is not available use Serial itself? Idea is to make a single program which can run on both UNO and MEGA so that I have to look after the pins only during production, and the software will cares the rest. Please let me know your thoughts on this.

2 Answers

+1 vote
by

This is not a simple way. Because the compiler will check your program when you program is building. It would throw a error when your select uno board in the IDLE but use Serial1 in your program. The compiler don't know what Serial means when you select uno board in the IDLE. Because Serial1 not in compiler preset file.

Using software Serial is a Not bad way to deal with your question. See this https://www.arduino.cc/en/Tutorial/SoftwareSerial

Hope you will success deal with question

0 votes
by (1.3k points)
You can use HAVE_HWSERIAL1, HAVE_HWSERIAL2 etc etc. See below I used for my xbee. If mega, the xbee is supposed to run on Serial1 and while on a UNO its supposed to be with the Serial

  #ifdef HAVE_HWSERIAL1
    Serial1.begin(BAUDRATE);
    myArduino.xbee.setSerial(Serial1);
  #else
    myArduino.xbee.setSerial(Serial);
  #endif

Related questions

+2 votes
1 answer 9.8k views
asked Mar 15, 2017 in Arduino by Manoj
0 votes
2 answers 10.1k views
asked Aug 1, 2017 in Arduino by nwbi
0 votes
1 answer 1.0k views
asked Sep 19, 2021 in Arduino by Samurai
0 votes
1 answer 1.3k views
0 votes
1 answer 3.5k views
0 votes
1 answer 863 views
asked Sep 12, 2021 in Arduino by mithun (1.3k points)
...