Go Down

Topic: BEGINNERS: Combining two codes - doesn't work ! (Read 592 times) previous topic - next topic

lastchancename

Jul 04, 2019, 12:57 am Last Edit: Jul 04, 2019, 07:39 am by lastchancename
There are many 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

Probably the simplest approach - but not always possible, 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.
Make sure it works after your changes - BEFORE you add new stuff to it!

Now, do the same with your second program - separately - 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 untouched 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.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

bkucenski

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.




lastchancename

Quote
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 !"
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

Goldenshuttle

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.
Live to learn & make

lastchancename

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.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

Go Up