How to Combine two (or more) Arduino sketches [a MUST watch for new programmers]

Are you working on an Arduino project, where you have to take code from two or more places and combine them into the same program?

But it’s turning into a Jerry Springer show?!  
One sketch is throwing chairs, the other is swearing at the top of their lungs – it’s a nightmare.

I mean, you have one sketch, and it does one thing.

And you have another sketch and it does another thing.

Why can’t these sketches hug, make friends, and work together!?

You know…I think what this calls for is a little Arduino therapy session.

Now, I’m not a licensed therapist or anything, but I have watched “What about Bob” so I have a couple ideas.

In fact, it’s a seven step program to get your sketches working together like a real team.

Let’s call in a prescription for your Arduino project.

Side effects may include upset stomach, dry mouth, and explosive bowls.

You ready?

To get up to speed on some common control structures, then this is the video you need to watch next. 

Here’s the complete 7-step program in 8 easy-to-follow sections.  We’ll go through each one in the video above.

  1. Ensure each sketch and circuit works independently, on the same dev board
  2. Ensure each sketch works independently when the circuits are combined 
  3. Create a new sketch
  4. Add a To Do list at the top of the new sketch
  5. Paste in the entire code from the first sketch
  6. Verify the sketch compiles
  7. Line by line, move the other sketch over, verifying line by line
  8. Danger Will Robinson!

1. Ensure each sketch and circuit works independently, on the same dev board

What you need to do first is make sure that each Arduino program you have works on its own.  What do I mean exactly by works?

Well, what I mean is this…

First, the sketch compiles in the Arduino IDE.  So, when you have one of your sketches open, and you click the check mark in the top left, you don’t get any error messages. 

Second, when you have the circuit made, you are able to upload your sketch to the Arduino board using the upload button AND the circuit behaves as you expect.

So let’s say you’re combining two sketches.  One that controls servo motors and another that uses a voice recognition module.

You need to make sure your servo code compiles and then you need to build the servo circuit, by itself – and make sure it is working with that code.

And you need to do the same thing for your voice recognition module.

This means you might need to lay out the circuits on two separate breadboards.  And that’s ok.

What is also important – is that you are using the same Arduino board for each circuit.  You can’t check one sketch on an Arduino UNO, and the other sketch on an Arduino NANO.  Both sketches need to be working properly on the same Arduino board.

The reason this is so important is because sometimes Arduino libraries are board dependent, that is, they may be compatible with one kind of Arduino board, but not another.  

For example, the voice recognition module I am using can communicate it 2 separate ways, either I2C or UART.  However, the UART method is not easily compatible with an Arduino Nano – even though it works great on an Arduino UNO.  So I need to make sure I am using the I2C method of communicating with the board.

I know this may sound obvious, but you can’t take 2 sketches that don’t work, and then combine them and expect them to work.  It will just be more Jerry Springer. 

Think about it this way.  Say you have two cups of milk – but one of the cups has red food coloring in it.

If you combine these into a single container – now they’ll both be red.

2. Ensure each sketch works independently when the circuits are combined 

Now what I want you to do is combine the two circuits.

In the example before, I had a servo circuit, and a voice recognition module circuit.  Now I need to take one circuit and combine it with the other using the same Arduino board.

This will do a couple things.  First, you may notice that the circuits share similar pins.  If they do, you have to figure out if this will be ok, or if you’ll have to use different pins.

For example, in this servo sketch, the circuit uses a 16 channel PWM driver that uses an I2C bus – these are pins 4 and 5 on the Arduino Nano board I am trying to use for this project.  But i also need to use pins 4 and 5 for the Voice recognition module.  Luckily, since both of these peripherals use I2C, they are able to share the same pins.

Once you combine your circuits, now you need to take each sketch independently, and make sure it still uploads ok and works fine.

Just to be clear – I have merged the circuit for my servos and my voice recognition module.  But now I am going to upload JUST the servo code again, and see if my circuit works like it should.

Then, I’ll upload JUST the voice recognition code, and make sure it works.

If your programs worked before, but now that they share the same circuit they are not working, this tells you something may be up with the circuit.  

Before proceeding, you need to get clarity on the circuit. 

If that means changing pin numbers on the circuit and in the code,  then you need to do this BEFORE you start trying to combine the programs.

I mean, imagine you had two cups of milk.  But one was poisoned with cyanide. If you combined the two cups, and drank it, you’d still be poisoned. 

If you’ve made it this far – you might start feeling cocky.  

In fact, you might be like, I’m just gonna paste all this code together and go for it! <say over optimistically)

Just as sure as the sun rises in the east, so shall your short cut lead to doom.

3. Create a new sketch

What I want you to do now is create a sketch, and then delete everything in it.  

This is going to be the place where all your code comes to live happily ever after.  Make sure you save it, and give it a good name so you can remember where it is.

4. Add a To Do list at the top of the new sketch

At the top of this sketch, I want you to write out a To Do list in the comments.

This list will express everything you want your program to do in a logical order.

If you have a simple program, this can be a really short list, if you have a long program, this will be a very long  and nested list.

Let’s take a look at the To Do list for my servo and voice recognition module.

Just as little background, all this code I have here is from one of our members.  She’s built an Iron Man mask using the servo code, but now she wants it to be voice activated.

Right now, each servo is operated by a separate button, but she wants each servo to be operated by a voice command.

To do:

  • uncheckedWhen specific voice commands are made, move a servo accordingly
    • uncheckedCode to recognize voice commands
    • uncheckedCode to move servo
    • uncheckedCode to move servos when specific commands are made
  • uncheckedTurn on a green LED when a servo is moving
  • uncheckedTurn off the green LED when the servo is done moving
  • uncheckedBlink a red LED when the servo is done moving
  • uncheckedWhen a single voice command is made, move all the servos one after another.

This simple TO DO list is what I will use to start combining my code.

5. Paste in the entire code from the first sketch

Now what I want you to do is paste one of the sketches – in its entirety –  into this new sketch, below your to do list.

Which one?  Well, pick the one that you think is doing the most work, or has the most code in it.

For example, I am going to pick the voice recognition code, because it has a layout in the loop that I think will work with my servos.

6. Verify the sketch compiles

Once you paste your code in this new sketch,  I want you to verify it and upload it, and make sure it’s working.

You might be like, Mike, I already did that – why would I do it again.

Well here is the deal.  

You just copied code from one sketch into another.

Anytime you copy and paste code, there is a chance you’ll have either forgotten something, like those devishish curly braces, or somehow added code that shouldn’t be there.

Don’t be laissez faire with your copy and paste.  

Think of a copy and paste like you’re a tight rope walker going over Niagara Falls – one wrong step, and you’ll fall to your death.

Wait – that’s way too intense.  Think of copy and paste like making sure you have all the things packed up for a camping trip to Niagara Falls.  If you forget something, it’s kind of a pain.

7. Trim First, Adjust, then merge line by line

Now you are finally at the point where we are combining the programs together.

It’s taken a long time to get here, but the battle has just begun.

Now before, I said something like – make sure both your sketches are doing what you want and working independently.

But you know, I kind of lied – call me a Cretan.

In reality, you’ll probably have two sketches that do different things, and you want to combine them to do a new thing.

For example, in the servo code, there are a bunch of buttons. 

I don’t need buttons anymore.  

And in the voice recognition code, it only listens for 3 different voice commands, but I need it to respond to at least five different commands.

So what you’ll need to do is first trim the current sketch of what you think is not necessary.

And do this one line at a time.  Every time you remove a line, verify the code, and make sure everything is still working.

You might be thinking I am being overly cautious – and I am.  That’s because, if you are new to Arduino programming, then it is really hard to know whether something is vestigial, or super duper important.

Once you trim, then you need to adjust the sketch to start knocking off some of your ToDo list.

For example, my voice recognition sketch only responds to 3 voice commands, but I want it to respond to 5 commands.

So before I start pulling any other code in, I am going to adjust it, and try to get it to respond to 5 voice commands.

Once I have my current sketch trimmed and adjusted, now I will start copying in my other code.

And guess what. I am going to do it, line by single line! Verifying each time I paste a new line!

Again – lots of caution!  You might be thinking, Mike, you’re just going overboard – you are dumb.

You have a point.  I’m not smart. 

But therapy is hard, you have to put in the work.  Follow the process, even if you think it’s dumb.

If you are a more seasoned developer, take more liberties, but if you looking at this code, and you don’t know it works, but you really want it work.  Then go slow.  This is going to help you find your errors faster.  And when you find errors faster, you can solve them faster.

When you start pasting in code.  I want you to start at the top of the other sketch and work your way down.

An Arduino program has 2 main functions, setup and loop.

In a single Arduino program, there can only be 1 setup function, and one loop function.

As you start copying and pasting code, pay attention to where the code is at and paste it in the corresponding spot.

If the code is above setup in one sketch, make sure it is above setup in the other sketch.

If it’s in the loop() function in one sketch, make sure you paste it in the loop of the other sketch.

Also, only copy over code you think you need.  If in doubt, copy it over, and remove it later.  But if you’re pretty sure there’s some code you absolutely don’t need, then don’t clutter up your new program. 

Now don’t expect this to be easy going – instead, expect this to be really difficult!

If you’re a new programmer, I guarantee this will be tough, you’ll get errors, but that’s ok. 

It’s part of the process.  The big advantage of doing this line by line is that you’ll see where the errors are super fast.  When you run into an error, don’t keep pasting over code.  Stop, try to figure out what the error is, then proceed.

8. Danger Will Robinson!

Now, anytime I meet someone who is planning on joining the military, I always give them a little talk.

I spent 20 years in the military, and there are certain things I know that can trip up a young person if they’re not on the look out for them.  

The same thing goes when you’re combining code.

First – timing is the devil.  

Timing is the biggest killer of hopes and dreams.

If you see the delay function peppered through either of your sketches, be prepared for a battle.

I’ll link to a video series about arduino and timing in the description – Hopefully, that will be able to help you with your code when timing becomes an issue.

Second, you need to understand your code flow.

If you want one thing to happen, and then another to happen when something else happens – it can get kind of confusing pretty fast.

Spend some time learning about control functions like if/else, switch, for, while, etc.

AppLab Bricks open in background with actual brick

Arduino AppLab Bricks → Marketing Garbage or New Powerful Interface?

Arduino Ventuno single board computer - top side

New Ventuno Q Dual Brain Single Board Computer

AppLab Pip Install

How to Add Python Packages in Arduino AppLab (No pip install needed)

Arduino Power Section Schematic

Kit-on-a-Shield Schematic Review

Just how random is the ESP32 random number generator?

Just how random is the ESP32 random number generator?

Leave a Comment