+1 vote
6.3k 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 (960 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 8.7k views
asked Mar 15, 2017 in Arduino by Manoj
0 votes
2 answers 8.8k views
asked Aug 1, 2017 in Arduino by nwbi
0 votes
1 answer 8 views
asked Sep 19 in Arduino by Samurai
0 votes
1 answer 16 views
0 votes
1 answer 1.7k views
0 votes
1 answer 16 views
...