Mid-Senior Engineers: Reinvent your career with Robotics, earning $100k-$200k+ in the next 90 days. Learn More

Merge Two Arduino Sketches Together

Liz Miller Learn Robotics

About the Author, Liz Miller, Founder/CEO @ Learn Robotics

Liz graduated with a degree in Robotics Engineering from Worcester Polytechnic Institute and researched drones at UPenn's GRASP Lab. Liz is a former Raytheon Engineer, where she managed major $MM automation projects worldwide. Today, she's the driving force behind Learn Robotics, offering the Robotics Career Blueprint for Engineering Professionals and beginner courses through the Online Robotics Class. Liz is a third-generation entrepreneur who is all about the application of innovation in robotics, automation, and AI. Follow Liz on LinkedIn and Facebook.

Disclosure: Some of the links below are affiliate links. This means that, at zero cost to you, Learn Robotics will earn an affiliate commission if you click through the link and finalize a purchase. Learn Robotics is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a way for websites to earn advertising revenues by advertising and linking to Amazon.com.

Join our Private Discord Server, MakeRobots! Sign Up

Do you have multiple Arduino sketches that you’d like to merge into one?

Then you’ll want to stay tuned for this tutorial!

I had a question come in this week about combining Arduino sketches. And for those of you who have never done this, you might be very pleased to know this is actually pretty easy.

This tutorial assumes you already know what Arduino is, how to create a sketch, and run code on your board. If you haven’t built anything in Arduino, then this tutorial won’t make sense. I recommend checking out our Four Steps to Writing any Arduino program tutorial, first, before diving into this one.

One thing to note, before we begin, this tutorial does not take into consideration multiple threads or simultaneous events. I will create a tutorial on how to do that in the near future. We will just look at migrating methods from one routine to another and verifying systemic integration.

Merge two Sketches into one

For this example, we will add music to our Animated Skull conditionally. It’s past Halloween, but that’s okay! Maybe you’re the kind of person who likes to keep decorations up until Thanksgiving!

We have one sketch that controls the music for the Skull and another sketch controls the sensors and servo to move the head.

Grab a copy of both of these sketches and follow along.

Here’s your download!

Musical Sketch (.ino)

Animated Skull Sketch (.ino)

First, open a third blank sketch and call it “Merged-Skull.ino”. We will compile contents from the previous two files into the merged file. I recommend doing it this way rather than taking content from one sketch and putting it into the other. This is because you could run into compiling problems or functionality problems when the code is transferred. If you have the original files to work from (and they both work independently), you can always go back to the beginning and start fresh. Mixed code gets ugly, so having a clean start is beneficial.

Arduino Sketches

To begin, we’ll start top to bottom and transfer global variables, setup() and other custom methods to Merged-Skull.ino.

 

Next, we’ll copy the contents of the loop() method from Musical-Sketch.ino and paste it into a new method called playSong().

Finally, we’ll call playSong() wherever we want the song to play. Note that this will be a sequential task, so the song will have to finish before we go back and check to see if someone has triggered the ultrasonic sensor. There are ways to get around Multi-tasking and threading using the Arduino. I will introduce how to do this in a future tutorial.

When all is done, be sure to compile your code, upload it to the Arduino, and test it out. You’ll want to make sure that your I/O pin wiring matches the pins defined in the code, or it will not work!

If you’re looking for more formalized Arduino Training, be sure to check out our online course, Arduino Prototyping for Beginners. We take students from no experience to knowing how to build, wire, and program custom prototypes. Sound like a good fit? Check it out here.

Experienced Engineer (Mechanical, Electrical, Computer, Software): If I offered to help you upgrade your engineering career to robotics, earning $100k-$200k+ in the next 90 days, would you take me up on that offer? Click here for details.
Liz Miller Learn Robotics

🚀 Pre-Launch: Become a "MakeR" with MakeRobots!

Hey Reader, 👋

Liz Miller, Founder/CEO, here with some Exciting News!

Learn Robotics just acquired MakeRobots™, an Online Robotics Community, and are prepping its Official Learn Robotics Debut in Late 2023.

MakeRobots™ is your one-stop-shop for learning, gaining coding, electronics, and robotics skills, connecting, and building robots for one low monthly membership!

Join MakeRobots™ at our Special Pre-launch Rate!
🤖 Access our Private Community & Robotics Courses
💬 Network, Collaborate, Connect with Other Makers
🔓 Only $5.99/month – locked in for life
⏱️ Pre-launch deal is Limited to the first 1,000 subscribers

This is a perfect opportunity for you to get into the fastest growing robotics community on the internet, at our ground-level, pre-launch membership rates.

👇 Click the button below to Claim your Pre-launch Membership and become a MakeR in the MakeRobots Community, today!

Learn Robotics Botly Favicon

MORE LEARN ROBOTICS ARTICLES