Share files across sketches

Before someone replies “use a library”, read why that likely won’t work.

Maybe there is a way to do this, but I am not aware of it.

I have some functions I share between projects. The projects are for different boards - a MEGA and a Pro Mini. An example file is one for GPS. On the MEGA, I connect that to Serial 1 and so in the main file I have this defined:

#define GPSSer Serial1

on the Pro Mini, I use nss instead and have this declaration:

#include <NewSoftSerial.h>
NewSoftSerial GPSSer (2,3);

So then in GPS.pde (a secondary file with a tab in the IDE), I can just use GPSSer.available() and GPSSer.read().

The two different meanings for GPSSer are source compatible, but not object compatible so I can’t make a library that I call with the serial port object.

There are other ways to get around it like coding the input in the main and only having the character passed to the GPS function, but this is just one example. I have a few files with similar issues.

I also like that it is all there in the IDE and I don’t have to go wok on the library for one thing and back in the program for another.

What would be ideal is a way to have some way of indicating to the IDE that there are files outside the project folder tat are part of the interactive project.

I'm going to ignore your comment at the top, and suggest that you use a library.

You could have something like this:

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// The Mega way
#else
// The other way
#endif

This way, the same source code will compile on all types of Arduinos.

I think I am a little (or maybe a lot) confused about what libraries are in the Arduino environment. Do they get compiled when they are used? I am used to libraries that are object code and are linked to. If they are built each time, then I could do what you are saying and make a library, though that still doesn't address my desire to be able to intermingle files in the IDE (on separate tabs).

In the Arduino world, a "library" is nothing more than a header file and corresponding source code file. The sketch references a header file. The compiler finds the corresponding source code file AND compiles it. Every time.

The header and source files need to live in specific places for this magic to work, which is the source of so many library problems.

though that still doesn't address my desire to be able to intermingle files in the IDE (on separate tabs).

The idea behind making something a library is that it is tested code, so it can be moved into a different directory (the library directory) and not have it visible with the sketch (and accidentally modified).

The library can be developed as part of the sketch. When this is done, you get multiple tabs. Once it is KNOWN to work, the extra tabs are not needed anymore.

I am using multiple tabs now. I have about 4 or 5 in each project and some are shared, which I do by copying them around. I have 3 different units all communicating with xBees and one of the things that the projects share are my definitions and processing of the commands/responses they send each other. That sort of development generally starts in the project of the initiating unit and then sort of follows the packet and execution flow. So the source file is still very much a work in progress but needs to be shared between projects.

Another sollution might be to allways use the NewSoftSerial() instead of the hardware Serial.

OK you make not use of the capabilities of a specific platform but it keeps things simpler to understand.

Another sollution might be to allways use the NewSoftSerial() instead of the hardware Serial.

Unfortunately, NewSoftSerial does not support the MEGA.

Unfortunately, NewSoftSerial does not support the MEGA.

I didn't know that, is that really a fact?

Lefty

I agree with retrolefty at least the "I didn't know that" part.

I'll ask the author of NSS.

Someone claims to have made a mod where it will work given some rather bizarre caveats: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1287382108 So I might be able to find some happy medium of pins it will work on that are on the Pro Mini also, but I had followed along with examples and wired the GPSes on the Pro Minis to pin 2.

Also, I think I get better performance with the hardware serial and the MEGA has a lot of other jobs to do besides reading the GPS.

And finally (probably not really), that is just one example of why I wish I could share IDE files. This particular forum is about how users like me wish it was. I don't expect total agreement and I know there are ways to work within what is there. I would just like it better if I could share files that show up in the IDE.

There was a modification posted recently that enabled NewSoftSerial to work on the Mega (on certain pins). It previously did not. The rationale was that the Mega has 4 hardware serial ports, so software serial ports were not seen as needed. How often does one need to hook more than 4 serial devices to a Mega?