Creating Sketch with multiple .ino files

Hello,

I have 2 .ino files for which I need to create a combined sketch of both into one (because I want a single .bin file)
Is it possible? If yes, how to do it? Please help....

Thanks,
Leena

Of course it is possible to combine 2 .ino files and to create a single sketch

Of course, there can only be one setup() function and one loop() function, each pin must only be used for one purpose and variables with the same scope must have unique names

What does each of the sketches do and what do you want the combines sketch to do ?

Post the 2 sketches and your best effort at combining them

1 Like

start by understanding what both programs do, in details
then build your own. May be this can be a useful read: Planning and Implementing a Program

1 Like

There is not one answer to that question. It depends on how the code in the two sketches is structured. It could be as simple as combining the setup() and loop() functions or as complex as restructuring one or both sketches. You may also have hardware conflicts which may be easy to resolve or which may require you to use different hardware.

Without seeing the sketches and hardware we cannot know.

1 Like

The fast and dirty way is to put the two sketches into 'tabs', rename the setup() and loop() in the two tabs to setup1()/loop1() in one tab and setup2()/loop2() in the other.

Then use this main sketch:

void setup()
{
  setup1();
  setup2();
}
void loop()
{
  loop1();
  loop2();
}

If you are lucky there will be no name conflicts or pin conflicts and both sketches will take turns so fast they will appear to be running 'at the same time'. If you are unlucky, you will have to resolve conflicts and re-write each sketch that hogs the CPU.

2 Likes

Thanks a lot ! I will try it out and let you know....actually I cannot share the sketches due to NDA policy.

No problem! Maybe they will have no problems and will just work.

Problems to look for:

Name Conflicts: Two or more sketches defining global variables or functions of the same name. You will have to change the names until there are no conflicts.

Pin conflicts: Two or more sketches using the same pin number for different purposes. You will have to change pin assignments to avoid conflicts.

Hardware Usage Conflicts: Two or more sketches that use the same hardware feature in different ways. For example, using the Serial port at different data rates. If you can get them to agree on a data rate you can replace the several Serial.begin(rate) calls with one at the top of setup().

Library Conflicts: Some libraries don't work together because they use the the same hardware for different purposes. For example the Servo library uses Timer1 and so does the IRremote library. If you are lucky you will get an error message when they both try to use the same hardware interrupt. If you are not lucky, things just won't work.

1 Like

Also look if the delay() function is used in one or even both sketches. If so, you will most probably run into problems when simply combining them.

1 Like

Thanks a lot :smiley: That seems really helpful.....I will keep you updated !

Or some 'while' loops. Or some 'for' loops.

Thank you !

Hello, My sketches were pretty simple and just copy pasting them one below other worked fine.....no compilation errors.

Still, asking this doubt on the forum was really useful for me to apply while dealing with complex sketches.

Thank you so much for all the quick responses :smiley:
Keep helping newbees like me ! That really means a lot.