How to combine code?

Hi. I have a project about bluetooth excavator. I wonder if I can combine the code of the Motor driver L298N, the servo SG90 and the HC-06 in one Arduino Uno R3? If you know how to do or you have made the excavator before, please help me. Thank you

Hello
Take a search engine of your preference and ask the WWW for
How to combine code?

1 Like

In principal it's simple :wink:

You put all the stuff from above the setup()s of the 3 sketches (pin number declarations and so on) into the area above setup() in a new sketch. Make sure the pins don't clash and that you don't try to use variable names more than once.

Then put all the stuff from the 3x setup()s into a new setup()- pinMode()s and so on.

That's the easy part.

You will almost never be able to just bolt the 3 loop()s together in one resulting loop() in a new sketch- before you can do that you need to define what the finished system is supposed to do. That will almost certainly result in a new design of loop() according to what the final design is, probably with code in various functions called from the new loop().

Nobody can help unless you try first and also define exactly what the result is supposed to be. You write of "the excavator" as if it's a well known sort of "standard" project... is it?

You may find this and this helpful.

1 Like

Or this
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

But from the looks of things you will have to use a state machine to make it do what you want, not just run one code after another. See
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

Or the blink without delay example in the IDE.

When you do this you will no longer be a beginner.

1 Like

If the separate sketches have been written as state machines its simple to just call each state machine in succession from the new loop() function.

If each is adhoc, its much more painful - in fact its best to rewrite each as a state machine first.

And you have to check for hardware resource clashes - for instance the Servo library uses timer1, making pins 9 and 10 unavailable for PWM.

But I don't understand why you are talking about combining code, since you seem to be wanting to code up an RC excavator from scratch?

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.