There are [u]many[/u] threads with the above subject.
How can I combine two codes, How can I combine two sketches... etc
The trick is - rarely can you make it that simple. There are a number of parametes that need to be considered.
- Memory size (Flash & RAM)
- Performance & Timing
- I/O overlaps & variable naming
[u]Probably[/u] the simplest approach - but [u]not always possible[/u], is to start with one program, (probably the more complex), then look how you can add the functionality from the second.
This doesn't mean cut & paste chunks of code until they fit and look good, but analyse how the larger program works - and optimise it if you can... remove delay() etc. [u]Make sure it works after your changes[/u] - BEFORE you add new stuff to it!
Now, do the same with your second program - [u]separately[/u] - and find if/how you can break it into function()s... This is the most important part, because now you're going to call those separate added function()s at the appropriate times within your optimised 'first' program.
(Remember to keep copies of your [u]untouched[/u] working programs - so you can go back and review how they work when they don't play well together.)
Already noted, this method only works on some/simple program pairs. *No matter what you want to do - you MUST understand how the source programs work, and what the various functions/variables achieve... *
REMINDERS: Using delay() and other uncorralled for/while() loops can 'block' your code from being responsive or interactive. The 'scope' of variables can save a lot of RAM if it is understood and applied correctly. Use meaningful variable_Names along with plenty of // COMMENTS or /* comment blocks / **Indentation* and following some 'style' wil make your code easier to follow and debug when you need.
In reality - to make merged functionality work properly & reliably - it is often better to use that functional knowledge yuu learned about the two 'donor' codes - to write the new program from scratch. If you did your preparation properly, those function()s you derived above can be stitched together to create a new integrated program that you need.