Making (More) Complicated Arduino Projects: Nx(software+hardware)
by CapeGeek in Teachers > University+
1293 Views, 11 Favorites, 0 Comments
Making (More) Complicated Arduino Projects: Nx(software+hardware)

Thinking Jointly About Software and Hardware
Beginning students and Makers initially gain Arduino skills by building a set of 5-8 of the classic sketches (Blink, Servo, PIR, etc.) as independent modules. As they advance, it is common to envision more complicated projects. These "bigger and better concepts" usually need to mix or combine one or more of previously independent modules. It is seldom pointed out, but one of the really radical things about Arduino (and Pi) is that it is not just about learning about programming or hardware separately. By their very nature they stretch you to think "jointly" about how they go together to make more complex devices real.
It is natural to think it should be easy to combine modules by just by copying and pasting one sketch in front of the other and simply adding additional hardware components on the breadboard. Sort of like how easy it is to write a compound English sentence: "First, do A, then B, then C," or adding integers A+B will produce a predictable sum C. But multiplying hybrid system complexity "Nx(software+hardware)" is not that simple. Basic combinations usually result in many unexpected errors, leading to hours of painful trial and error debugging. This is non-productive if you are in a Project Based Learning activity, where, ideally, you should be able to make independent progress.
This problem is usually starts by having multiple Setup() and Loop() functions, when there should only be one, more than one thing plugged into overlapping pin numbers, and conflicting variable names that must be renamed from the original sketches. Resolving these problems is really not rocket science, but it is typically solved only after lots of frustrating trial and error and talking to the instructor. Frequently the student just gives up and moving on to another project! The goal of this Instructable is to describe several ways to successfully scale up complexity of Arduino constructions. Three methods will be described.
The first is just a simple brute force process of fixing overlapping definitions and then sorting like-with-like from the original sketches into a new combined sketch. Since most early projects will not be terribly complicated this method will be be sufficient for most beginner projects.
The second method is to rewrite parts of the original sketches as functions. This is called abstraction, as it can produce a new combined sketch that is simpler to understand. Later, you can investigate more general abstraction, using object oriented programming (OOP) methods using classes and objects. You may have already learned about these if you learned C++ or related programming language. But the the basic method shown here first is simple, organized, and easy to understand, so it should help beginners avoid having to ask for help.
The third will introduce the use of Arduino shields. These are modular circuit boards that plug on top of your Arduino PCB to extend its functionality. These can be thought of as a form of hardware functional abstraction to simplify complex constructions.
Supplies
- Arduino board, Arduino IDE
- Optionally TinkerCAD to speed prototype debugging
- Multiple independent Arduino Sketches providing independent functionality
- Associated circuit components
- A student generated project design that requires the multiple functionality
Example of Sketch Combining

To illustrate the problem we will start with a simple hypothetical project that to involves two LED Blink sketches, as shown in the figure. Initially, there are two classic Blink sketches that have two different colored LEDs (red and green) to blink, and one is to stay on for 1 second and the other for 2 seconds. The goal is to produce one combined sketch.While this example is very simple, the process can be used with any combination of sketch functionalities and is easily generalized to more than two.
A Simple Example

The figure shows the sketches of the two independent Blink circuits. The problem is the sketches have same name, pin names, and pin numbers, use different colored LEDs, and both have different duration times. While probably not a practical example, its simplicity helps to illustrate the process.
Process to Combine Sketches

This diagram shows the outline of how to reorganize the two Sketch components into a new Sketch with the combined functionality.
- check both sketches and look for duplicate names (other than Setup and Loop). In this case LED. Find and replace those first before you continue. We will use RED_LED and GREEN_LED.
- Open a fresh sketch with a new name for the target. Don't try to edit one of the existing sketches. Then copy variable and pin assignments, giving unique identifying names and numbers (e.g. RED_LED and GREEN_LED, and 13 and 8).
- Move copies of the Setup instructions (INPUT/OUTPUT assignments, etc.) and any one time instructions.
- Copy the instructions from each Loop into the new sketch, one before.
This will get you a sketch that should compile without error that has the combined functionality. As shown in the original goal block diagram, there may be other code before, between, and after these blocks of code. But adding that should be straightforward now that you have worked out the overlaps, etc.
Combined Sketches

This shows the result when the process is applied to our simple example. Now save and check the installation with the Arduino IDE. The result should be a new sketch with the combined functionality. You should now be able to apply the same process to more complicated examples in your own projects.
Optional: Using Functions to Increase Modularity


Since the goal is to combine multiple Arduino sketches into a single sketch, you might consider an additional step that can simplify the combined result. This can be done by rewriting the core of the original Sketches as FUNCTIONS that can be "called" in the new Sketch. The advantage is the final sketch is smaller and simpler, making it more modular and easier to understand. This form of simplification is called abstraction. The diagram shows the final result. It is shorter because the key Loop instructions in the combined sketch are replaced by short one word "function calls" to a new function called blinkLED, but in this case with two different parameters to replicate the two different blink behaviors. This is called twice, but with different LED pins and different duration .
There are lots of good explanations to give you more details about Functions in Arduino:
https://docs.arduino.cc/learn/programming/functions
https://startingelectronics.org/software/arduino/learn-to-program-course/15-functions/
https://www.circuitbasics.com/using-functions-in-arduino-programming/
If you learned programming before Arduino, Python, etc., you probably leaned about using subroutines. You can think of subroutines serving a similar purpose as functions.
Using Arduino Shields to Simplify Adding Hardware Complexity

Orderly consolidating sketches or using functional abstraction are methods to deal with software complexity. But usually when you move from simple, independent Arduino modules to Arduino systems that combine multiple functionalities, the hardware also grows in complexity. In many cases this can be dealt with just by adding more components, as long as you resolved hardware conflicts, like using the same pin more than once. But some cases require multiplicative complexity, such as adding 5 copies of a relay driver, 3 steppers in an arm, or 2 motors in a car. Other examples might be when adding one new component requires much more complex circuitry, such as a radar sensor. One possible solution to these problems has evolved in the Arduino world called Shields.
Arduino shields are modular circuit boards that plug on top your Arduino PCB to extend its functionality. Each shield has a specific function. One of the features of Arduino is the use of breadboards to enable you to easily construct circuits. This is fine for prototyping, but they can become clumsy as the complexity increases or you want a more finished and durable project product. Shields provide a solution to this problem.
Here is a short, basic list of useful shields: Motor Shield, Ethernet Shield, GPS Shield, JOY Stick Shield, Relay Shield, Motor Shield, Relay Shield. The list is endless and constantly growing. The best reference is to do a web search mentioning "Arduino Shield XXXXX". Where XXXX is the functionality you are looking for.
Power Supply Hints
When you start to scale up the complexity of a system it is very common to have problems caused by power supply issues. The power supply in an Arduino is limited to 40ma. As long as you are only using it to power one small device at a time, like one LED, one servo, etc. You won't have a problem. But if you try to power several LEDs in parallel or serial or motors that take more power you might start seeing unexpected problems or even destroy the Arduino. Devices that have coils, like steppers, servos or small motors can also cause transient noise that can cause problems when they are turned on and off. So be aware of this issue when scaling up. This is very common when you are connecting high load devices like stepper motors, DC motors, or servo motors. When activated they can suddenly demand a lot of current, which can cause the voltage for the Arduino to drop. This can cause the Arduino to reset or the device to chatter rather than running smoothly. Note that these steps may not be necessary unless your design starts to have these unexpected behaviors. Just keep this issue in mind.
One fix is to add large capacitors that act like short-term backup power supplies at moments when more power is needed and small capacitors to filter out noise spikes from power surges and long input lines on sensors. Typically a 10 to 100 μF capacitor is connected across the 5 volt power lines. These are usually polarized capacitors (the silver strip on the capacitor signifies the ground leg) and must be placed so the ground side goes to ground. A 100nF capacitor is added to filter high frequency transients across the supply to any device (e.g. stepper).
One other fix to keep in mind is to add external power instead of using the 5 volts from the Arduino board when power demands increase with multiple motors, etc. If you start having these problems there are many references you can check on line.
Summary
This Instructable has described three methods to combine one or more sketches to build more complex Arduino constructions. These techniques should be particularly helpful for students trying to complete Problem Based Learning (PBL) constructions as independently as possible. As mentioned initially, these are not rocket science, but they can take time and patience and practice.
There are also more complex ways to deal with complexity using Arduino Libraries, or object oriented programming (OOP). Object-orientation is a technique for writing programs in a way that makes it easier to manage as they grow in size and complexity. But the basic process shown here should work reliably for you as you move into more complex designs. Once you understand these methods it should save you a lot of trial and error.
Below are some additional examples:
The first is a different explanation of the same basic example using two copies of the same sketch:
Combining Two Arduino Sketches
This one combines code for 3 sensors; temp, gas, and soil moisture:
Combining code for multiple sensors in a single program
The second combines two different sketches. One controls music and the second uses sensors and a servo to move an animated head.
Merge Two Arduino Sketches Together
If you want to check out object oriented approaches here are some good introductions, but just stick to the simpler methods for now if they sound overwhelming.
https://www.circuitbasics.com/programming-with-classes-and-objects-on-the-arduino/
https://learn.adafruit.com/multi-tasking-the-arduino-part-1/a-classy-solution
https://arduino.stackexchange.com/questions/77745/arduino-object-oriented-programming
You can find the latest information about shields by going to any of the supplier sites or Amazon.
I hope this Instructable enables you to build more complex project constructions with less frustration.