How to Combine Two Arduino Sketches
In this Arduino tutorial, I will show you how to combine two Arduino sketches into one single program. This is a useful skill to know if you want to reuse code from existing sketches or if you want to combine two sketches that do different things.
To combine two Arduino sketches, you will need to:
- Open both sketches in the Arduino IDE.
- Copy the contents of the first sketch.
- Paste the contents of the first sketch into the second sketch.
- Make sure that there are no duplicate variable names or pin numbers.
- Compile and upload the sketch to your Arduino board.
- Here are some tips for combining Arduino sketches:
- Use descriptive variable names to make your code easier to read and understand.
- Use comments to explain what your code is doing.
- Use functions to organize your code and make it easier to reuse.
- Test your code thoroughly before uploading it to your Arduino board.
Guide for Combining Arduino Sketches
1. Copy all of the variables from before setup();
The information before setup();
includes libraries, variables, and other parameters. Select everything from one sketch above setup and copy it. Then paste it above setup();
in the second sketch. It is a good idea to put a comment above and below the pasted code such as // begin pasted code
and // end pasted code
, that way you know which code is from the previous sketch.
Look for duplicate pin assignments. If two variables such as two LEDs share pin 13, then you could change one to pin 10 or pin 8 or another open digital pin.
Look for duplicate variable names. If names are duplicated, determine if both variables are needed or if only one is. If there are two variables called led
then you probably need to rename one to led1
and the other to led2
but if there are two variables labeled currentMilliseconds
then you only need to keep one because you only need to have the current time recorded in one variable.
If you change a variable name before setup then it need to be changed everywhere in the code.
- Copy all of the variables
- Rename any duplicates
- Reassign pins that are the same to different pins
- Make notes about variable renaming
- Make notes about pin reassignment
- Put comments above and below the code indicating that it was added
2. Copy information from setup();
Now we will copy the information from the setup();
function. This will include specific setting, pinMode();
, beginSerial();
and other single run lines of code. Do not copy the beginSerial();
as a sketch only needs one. Add comments to your code to show what you added. If you updated variable names before setup();
make sure to change them here.
- Copy the new information
- Update any variable names from previous step
- Update any reassigned pins
- Add comments to indicate the added code.
3. Copy information from loop();
Now copy the code from inside the loop();
from the other sketch. Make sure to not include the ending }
bracket. After pasting the code into the combined sketch, put code comments above and below the pasted code. Change variable names as needed to match the names above.
- Copy the new information
- Update any variable names from previous step
- Update any reassigned pins
4. Copy information from below loop();
There are likely additional functions below loop();
. Copy everything and then paste it at the very bottom of the combined sketch. Put a comment indicating where the pasted code starts. Make sure to update any variable names.
5. Test, Test, Test
Then we must test the code to fix unexpected errors and problems. Click the checkmark at the top left of the Arduino IDE to see if the code compiles. If the text at the bottom is white and it says compile complete, then the code does not have syntax errors. It may still need tweaking to work with the electronics hardware though.
If the text at the bottom is red, then read the error messages and attempt to correct them. Often it will be a typo in a variable name or a missing }
or an extra }
. One trick is to copy the error message and paste it into a search engine. There is often an explanation of the error and instructions on how to fix it.
Do your best but you may not be able to remove all the errors. Turn in you best attempt.