How to combine two Arduino sketches
I have two Arduino sketches for two sensors. How to combine two different Arduino sketches to one complete sketch?
Answer
Suppose that you have two Arduino code as below
Arduino sketch 1
/*******************************
* SKETCH 1 DEFINE, VARIABLES...
********************************/
void setup() {
/*****************************
* SKETCH 1 SETUP CODE
****************************/
}
void loop() {
/*****************************
* SKETCH 1 LOOP CODE
****************************/
}
Arduino sketch 2
/*******************************
* SKETCH 2 DEFINE, VARIABLES...
********************************/
void setup() {
/*****************************
* SKETCH 2 SETUP CODE
****************************/
}
void loop() {
/*****************************
* SKETCH 2 LOOP CODE
****************************/
}
How to combine two above sketches
- In both sketches, use millis() instead of delay()
- Make sure that two sketches do not conflict to each other in using Arduino pin. If conflicted, change the pin in a sketch
- Make sure that variable name or user-defined functions in both sketches are not the same. If both sketches have the same variable name or user-defined function, rename the variable name or user-defined function to make it different
- Merge two sketches. code will merge as below
/*******************************
* SKETCH 1 DEFINE, VARIABLES...
********************************/
/*******************************
* SKETCH 2 DEFINE, VARIABLES...
********************************/
void setup() {
/*****************************
* SKETCH 1 SETUP CODE
****************************/
/*****************************
* SKETCH 2 SETUP CODE
****************************/
}
void loop() {
/*****************************
* SKETCH 1 LOOP CODE
****************************/
/*****************************
* SKETCH 2 LOOP CODE
****************************/
}
※ NOTE THAT:
The combined code might not works. You may need to make more modification based on the combined code.
Buy Arduino
1 × Arduino UNO Buy on Amazon | |
1 × USB 2.0 cable type A/B Buy on Amazon | |
1 × Jumper Wires Buy on Amazon |
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.