BEGINNERS: Combining two codes - doesn't work !

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
  • Blocking code

  • and more

[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.

This is why God gave us object orientated programming.

Learn how to use h files to define classes and reusable code and your life will be way easier.

Whether you put your declarations in an h file and your definitions in a cpp file are up to you.

Personally, it depends on the complexity.

If I want a quick way to see the various methods and variable names and don't care about the implementation, then I use the h + cpp file way. For example, my i2c display class uses an h file and a cpp file so I don't have to dig through hundreds of lines of code to find method names. They're all neatly listed in the h file.

If it's just a simple self contained class then I just use an h file. For example, the map class for my platformer is just a single h file since it has only a couple functions. I don't really need to break the method implementations out into a separate cpp file at this point.

Learn how to use h files to define classes and reusable code and your life will be way easier.

Valid observations, but probably not for the target audience of “Combining two codes - doesn't work !”

good comments, still I think combining codes is Loooooong learning curve depending on complexity of the code. I am not sure if there is or can be a marketplace here for code-fusers...this may help beginners like Goldenshuttle, get their projects expedited at reasonable cost(i hope) ..and for experienced members challenge their & sharpen skills.

COMBINING CODES Unfortunately, there is no easy or repeatable way to ‘combine codes’. The only partially successful strategy - is to put each of the original setup() and loop() into their own new functions [... setup1() and setup2(), loop1() and loop2() etc], and call those from within the ‘real’ setup() and loop()...

This only goes perhaps 30% of the way to realising compilable, workable code in simple projects. There are plenty of other elements that need to considered, resolved and changed.

There is no shortcut to understanding ‘what’ each line of code does, and merging or rewriting the solution to accommodate the end result.

Begginners: before you attempt to "combine codes", first explain clearly what the combined sketch is supposed to do.

Why do you want to combine codes??

Why do you want to combine codes??

Often. beginners try to raise the functionality of several examples they’ve found - to achieve something more substantial.

A simple example might be using two buttons to start and stop some sequence of flashing LEDs, then adding a buzzer.. Drawn from examples, this appears easy to the accomplished coder, but to beginners can be quite daunting.

Pin numbers, pin modes, scope, delays, pull-up and pull down, millis(), blocking, and so on.

Just mashing two blocks of code together without understanding what & how it works - is almost obliged to fail. Programming requires some subtlety and refinement, and this is what takes time to learn.

Crawl, then walk before running !