How to keep code Tidy - is a Library the answer ?

Guys,

I have an Arduino Mega R3 with Ethernet Shield - all working fine.

My ultimate goal when i finish this is to move my home heating system from a PIcaxe based system across to running on Arduino.

I have used the example UDP NTP Client and have this working with no issues.

However when i look at this there is a lot of code in there that i am really only going to call once per hour or so - and knowing that the code works was wondering how i can "get rid" of it so i do not have it sitting in the middle of my project (or at the end as a function etc)

I believe my two options (and i am a newbie to Arduino) would be to make use of the Tabs within the 1.04 IDE or look at turning the code into a library and then do a simple include - i have slightly modified the example code so it actually returns me time in Sydney Australia and intend to put in something to cater for Daylight Savings also - however once this is done then i do not plan on looking at the code again and would ideally like to have something in my project along the lines of

GetNTPTime(Hour,Minute); - knowing there would be a couple of variables returned etc

Any suggestions as to the best way to approach this ?

regards

Craig

You've identified the two sensible options. If you want to get the code out of your main sketch file then I suggest putting it in a separate .cpp file (i.e. a separate tab, as you described it) as the first step. I would only move it out into a library if you planned to reuse it for other sketches.

Note that if you pull functions/classes/data out of your sketch into a separate .cpp file and want to access any of them from outside the .cpp file then you should also create a .h file (conventionally with the same file name as the .cpp with just the extension changed) containing the corresponding external declarations, and #include that in the main sketch file.

For example:

Foo.cpp

unsigned long doSomething(unsigned long junk)
{
   return junk;
}

Foo.h:

extern unsigned long doSomething(unsigned long);

Sketch.ino

#include "Foo.h"
...

unsigned long result = doSomething(now);

Yes I would / have used tabs for this.
You can even reuse most of the code on other projects when you do this.
For example I have some code I use when writing menu structures, it always needs tweaking in diffrent projects so I simply have it as a tab and I can alter it to match what I am doing in the IDE and it is out of the way.

Great guys,

Thanks for taking the time to respond.

Being new to the whole C and Arduino thing - i will now have to get stuck into some serious study and how to use the external files. Thanks for the tips on how to integrate it.

Will report back as i progress

regards

Craig