Combining Arduino Sketches

This is a very common problem: adding two or more working sketches into one sketch.

Putting a bunch of code together and then getting a Zillion error messages is No Fun!

FIRST! Start by understanding both sketches and the resources and libraries they use before attempting to stitch them together. If there is any part of either sketch you don't understand, that will be the part that causes you trouble.

Go to Arduino Software and then Example Sketches on this WIKI (Click HOME on right).

See the Sketch Template here Copy and Paste that into a blank IDE window.

  • Copy the sections from ONE of your existing sketches into the sections of that template. Verify that to make sure it is all OK. Save with a name.
  • Get another blank window and sketch template. Copy your second working sketch into that and make sure it verifies OK. Save with name.

Now, get ANOTHER blank window and sketch template. Save it with a name for your actual project. Start copying the sections, one by one, and run Verify after each thing you add: Here are the sections:

/*-----( Import needed libraries )-----*/

Copy the #define statements for libraries one at a time and Verify after each one. If you have obvious Library Conflicts you'll know that right here.

/*-----( Declare Constants and Pin Numbers )-----*/

#define your pin numbers here. See if you have any conflicts. Verify.

/*-----( Declare objects )-----*/

Example: your "EthernetServer server(80);" Verify. See if any conflicts here.

/*-----( Declare Variables )-----*/

Copy in both sets of variables, check for conflicts, Verify.

Now, start copying sections of code.

void setup() / SETUP: RUNS ONCE

Start adding statements in SETUP from both sketches, Verify often.


Now, this is the hard part, because the code and logic has to work in the right sequence and with workable timing.

Really think through the code in each sketch. Figure out how it can work together. You MAY have to change the way some things are done. NonTrivial!

/*-----( Declare User-written Functions )-----*/

User Written Functions can go After "LOOP". I suggest you copy the functions you may have, like "void readTag() " into this area to keep things organized. These are functions that were originally OUTSIDE of both SETUP and LOOP.

Personally, I like to have LOOP just contain three functions as shown in this example:

OK, this is difficult, but I hope this will help you stay organized and find problems one at a time.

HERE is another helpful article on this subject.

Anyone with suggestions or pointers to other related guidelines, please mail

Divide and Conquer!