How to program for two Arduinos with different code but share one same .h? - Arduino Stack Exchange


i programming 2 arduinos. 1 used tx , other rx. of course programmed different codes, share same .h files programmed(let's 'share.h'), not libraries , shouldn't put in arduino's 'library' folder.

so put 'share.h' , main.ino in 1 folder, , ino like:

#include "share.h" #define tx #ifdef tx void setup(){...something tx...} void loop(){...} #else void setup(){...something rx...} void loop(){...} #endif 

the shortage every time want update code 2 unos, have to: first change board port 40(the tx uno) in ide, add #define line, , upload. have change port 42(the rx one), comment #define line , upload. inconvenient!

so have ideas? appreciation!

ok, first of have answer question:

how can tell compiler program should compile?

the first (and easiest) solution have preprocessor job. it's matter of writing 2 /...

personally, instead of wrapping in ifdef, have done like

#include "share.h" #define tx #ifdef tx #define setup_tx() setup() #define loop_tx() loop() #else #define setup_rx() setup() #define loop_rx() loop() #endif  void setup_tx(){...} void loop_tx(){...}  void setup_rx(){...} void loop_rx(){...} 

this way put 2 different setup/loop pairs in 2 different files (e.g. tx.ino , rx.ino). ifdef block can moved in h file.

note: i'm not sure wheter function redefinition works well, because never tried it. if doesn't work can try doing opposite (i.e. #define setup() setup_tx() , on) or, in end, make single-line functions (i.e. void setup(){setup_tx();} , on).

another solution can applied if processors different. instance if have arduino uno transmitter , arduino mega 2560 receiver, write:

#ifdef __avr_atmega2560__ - receiver functions #else - transmitter functions #endif 

but require switch processor along port.

the third solution place shared h file in include directory. can using common include dir:

the include path includes sketch's directory, target directory (/hardware/core//) , avr include directory (/hardware/tools/avr/avr/include/), library directories (in /hardware/libraries/) contain header file included main sketch file.

(see here)

you can pass -idir option avr-gcc include dir in include dirs list, requires change boards.txt file.

the last solution can think @ compile both programs, load same files on both arduinos , choose @ runtime correct program use assinging pin "choice of program" function.

for instance assing pin 8 function. on tx board connect pin 8 vcc, while on rx board connect gnd. program is:

#include "share.h"  // loop function callback (to speed things up) typedef void (*loopfunction)(); loopfunction currentloop;  const int rxtx_pin = 8;  void setup() {     pinmode(rxtx_pin,input);     if (digitalread(rxtx_pin))     {         setup_tx();         currentloop = loop_tx;     }     else     {         setup_rx();         currentloop = loop_rx;     } }  void loop() {     currentloop(); }  void setup_tx(){...} void loop_tx(){...}  void setup_rx(){...} void loop_rx(){...} 

imho best solution first one, i.e. define one. but... should choose best particular problem.

bye


Comments