Difference between revisions of "CombiningArduinoSketches"

From ArduinoInfo
Jump to: navigation, search
m
m
Line 3: Line 3:
 
This is a very common problem: adding two or more working sketches into one sketch.<br /> <br />   
 
This is a very common problem: adding two or more working sketches into one sketch.<br /> <br />   
  
Putting a bunch of code together and then getting a Zillion error messages is No Fun!<br /> <br /> '''''FIRST! Start by understanding both sketches and the resources and libraries they use before attempting to stitch them together. If there is any part of either sketch you don't understand, that will be the part that causes you trouble.'''''<br /> <br /> <span style="line-height: 1.5">
+
Putting a bunch of code together and then getting a Zillion error messages is No Fun!
 +
 
 +
'''''FIRST! Start by understanding both sketches and the resources and libraries they use before attempting to stitch them together. If there is any part of either sketch you don't understand, that will be the part that causes you trouble.'''''
  
 
See [http://arduinoinfo.mywikis.net/wiki/YourDuinoStarter_SketchTemplate the Sketch Template here] Copy and Paste that into a blank IDE window.<br /> <br />  
 
See [http://arduinoinfo.mywikis.net/wiki/YourDuinoStarter_SketchTemplate the Sketch Template here] Copy and Paste that into a blank IDE window.<br /> <br />  
Line 17: Line 19:
 
Copy the #define statements for libraries one at a time and Verify after each one. If you have obvious Library Conflicts you'll know that right here.
 
Copy the #define statements for libraries one at a time and Verify after each one. If you have obvious Library Conflicts you'll know that right here.
  
'''/*-----( Declare Constants and Pin Numbers )-----*/'''
+
'''/*-----( Declare Constants and Pin Numbers )-----*/'''
 
#define your pin numbers here. See if you have any conflicts. Verify
 
#define your pin numbers here. See if you have any conflicts. Verify
  
'''/*-----( Declare objects )-----*/'''
+
'''/*-----( Declare objects )-----*/'''
 
Example: your "EthernetServer server(80);" Verify. See if any conflicts here.
 
Example: your "EthernetServer server(80);" Verify. See if any conflicts here.
  
'''/*-----( Declare Variables )-----*/'''<br /> <br />  Copy in both sets of variables, check for conflicts, Verify.<br />
+
'''/*-----( Declare Variables )-----*/'''<br /> <br />  Copy in both sets of variables, check for conflicts, Verify.<br />
 
----
 
----
 
  Now, start copying sections of '''code.'''
 
  Now, start copying sections of '''code.'''
Line 31: Line 33:
 
void loop() // '''LOOP: RUNS CONSTANTLY'''
 
void loop() // '''LOOP: RUNS CONSTANTLY'''
  
Now, '''this is the hard part''', because the code and logic has to work in the right sequence and with workable timing.<br /> <br />  Really think through the code in each sketch. Figure out how it can work together. You MAY have to change the way some things are done. NonTrivial!<br /> <br /> '''/*-----( Declare User-written Functions )-----*/'''<br /> <br />  User Written Functions can go After "LOOP". I suggest you copy the functions you may have, like "void readTag() " into this area to keep things organized. These are functions that were originally OUTSIDE of both SETUP and LOOP.<br /> <br />  Personally, I like to have LOOP just contain three functions as shown in this example:<br /> <br /> http://arduinoinfo.mywikis.net/wiki/YourDuinoStarter_AutomationExample<br /> <br />  OK, this is difficult, but I hope this will help you stay organized and find problems one at a time.<br /> <br /> [http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html HERE] is another helpful article on this subject.<br /> <br />  Anyone with suggestions or pointers to other related guidelines, please mail [mailto:terry@yourduino.com terry@yourduino.com]<br /> <br />  Divide and Conquer! </div>
+
Now, '''this is the hard part''', because the code and logic has to work in the right sequence and with workable timing.
 +
 
 +
Really think through the code in each sketch. Figure out how it can work together. You MAY have to change the way some things are done. NonTrivial!
 +
 
 +
'''/*-----( Declare User-written Functions )-----*/'''
 +
 
 +
User Written Functions can go After "LOOP".  
 +
 
 +
I suggest you copy the functions you may have, like "void readTag() " into this area to keep things organized. These are functions that were originally OUTSIDE of both SETUP and LOOP.  
 +
 
 +
Personally, I like to have LOOP just contain three functions as shown in this example:
 +
 
 +
http://arduinoinfo.mywikis.net/wiki/YourDuinoStarter_AutomationExample
 +
 
 +
OK, this is difficult, but I hope this will help you stay organized and find problems one at a time.
 +
 
 +
[http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html HERE] is another helpful article on this subject.<br /> <br />  Anyone with suggestions or pointers to other related guidelines, please mail [mailto:terry@yourduino.com terry@yourduino.com]<br /> <br />  Divide and Conquer! </div>

Revision as of 22:46, 4 November 2018

Combining Arduino Sketches

This is a very common problem: adding two or more working sketches into one sketch.

Putting a bunch of code together and then getting a Zillion error messages is No Fun!

FIRST! Start by understanding both sketches and the resources and libraries they use before attempting to stitch them together. If there is any part of either sketch you don't understand, that will be the part that causes you trouble.

See the Sketch Template here Copy and Paste that into a blank IDE window.

  • Copy the sections from ONE of your existing sketches into the sections of that tempplate
  • Verify that to make sure it is all OK. Save with a name.
  • Get another blank window and sketch template. Copy your second working sketch into that and make sure it verifies OK. Save with name.

Now, get ANOTHER blank window and sketch template. Save it with a name for your actual project. Start copying the sections, one by one, and run Verify after each thing you add: Here are the sections:

/*-----( Import needed libraries )-----*/ Copy the #define statements for libraries one at a time and Verify after each one. If you have obvious Library Conflicts you'll know that right here.

/*-----( Declare Constants and Pin Numbers )-----*/

  1. define your pin numbers here. See if you have any conflicts. Verify

/*-----( Declare objects )-----*/ Example: your "EthernetServer server(80);" Verify. See if any conflicts here.

/*-----( Declare Variables )-----*/

Copy in both sets of variables, check for conflicts, Verify.


Now, start copying sections of code.

void setup() / SETUP: RUNS ONCE Start adding statements in SETUP from both sketches, Verify often.


void loop() // LOOP: RUNS CONSTANTLY

Now, this is the hard part, because the code and logic has to work in the right sequence and with workable timing.

Really think through the code in each sketch. Figure out how it can work together. You MAY have to change the way some things are done. NonTrivial!

/*-----( Declare User-written Functions )-----*/

User Written Functions can go After "LOOP".

I suggest you copy the functions you may have, like "void readTag() " into this area to keep things organized. These are functions that were originally OUTSIDE of both SETUP and LOOP.

Personally, I like to have LOOP just contain three functions as shown in this example:

http://arduinoinfo.mywikis.net/wiki/YourDuinoStarter_AutomationExample

OK, this is difficult, but I hope this will help you stay organized and find problems one at a time.

HERE is another helpful article on this subject.

Anyone with suggestions or pointers to other related guidelines, please mail terry@yourduino.com

Divide and Conquer!