Different Serial ports problem

Hey guys, I'm writing a library for myself and I run on a problem. I wanna use my library in Uno, Mega and Micro boards and write data to Serial. For Mega my program runs nicely but not on Uno and Micro. I know that the problem is caused because they use different serial port numbers so my question is; Is there any way to detect what board I'm using and determine the serial port according to that knowledge?

Thank you and sorry for the grammar errors!

Have a look in your arduino folder for Arduino.h

./hardware/arduino/avr/cores/arduino/Arduino.h

Near the top of the file ~line 71, you can see a bunch of defines for the different MCUs, which you can use for conditional compiling within your own sketch.

#ifdef __AVR_ATmega328P__
  //compile for uno
  Serial.println("this is an Uno");
#endif

#ifdef __AVR_ATmega2560__
 //compile for Mega
 Serial.println("this is a mega");
#endif

For Mega my program runs nicely but not on Uno and Micro.

The Micro and the Leonardo use Serial to talk to the PC, and Serial1 to talk to the RX and TX pins. The Mega and the Uno use Serial to talk to the PC and the RX and TX pins.

If your code works on the Mega but not the Uno, there is something wrong with your code.

Of course, we can’t see what that is, because you didn’t post it.

kimppa: Hey guys, I'm writing a library for myself and I run on a problem. I wanna use my library in Uno, Mega and Micro boards and write data to Serial. For Mega my program runs nicely but not on Uno and Micro. I know that the problem is caused because they use different serial port numbers so my question is; Is there any way to detect what board I'm using and determine the serial port according to that knowledge?

Thank you and sorry for the grammar errors!

Another idea might be to use an argument variable to set the board type and use that for different Serial schemes. If you have to instantiate a class, you could make it part of the parameters.

If you're [u]only[/u] sending to the PC, I don't think you need to compile any differently for the various boards.

Your grammar is better than many folks who've been born in English-speaking countries.

Thank you for the answers! I go check that 'Arduino.h' file and check those #ifdef's .The boards are sending data to Bluetooth chip which is connected to my Android phone so I can't use regular 'Serial.print' on micro.