4

I would like to create a program with separated modules, is this this possible using Arduino IDE?

1

5 Answers 5

3

While you can create your own libraries for the Arduino, a simple way to make reusable code is to create a header (.h) file that contains the code you would like to make reusable.

Specifically, in the Arduino IDE create a new tab with some name foo.h, and in your main tab, add #include "foo.h" at the top of your code. What this does is essentially copy paste the code from foo.h into your code during the build process. While simple in execution, it really helps organize more complex code, and in your case, helps with reusing code.

You can alternativly use "Normal Arduino code files (no visible extension), C files (.c extension), C++ files (.cpp), or header files (.h)."

And one extra note to help you - the header files do not have access to some of the typical Arduino commands and functionality, such as delay() or Serial unless you add #include "Arduino.h". However, for IDE versions less than 1.0, use #include "WProgram.h". This will give you access to those functions.

2

Arduino sketches can include Libraries (see https://www.arduino.cc/en/Guide/Libraries).

In order to work with the Arduino IDE, such a library is required to adhere to a certain folder structure and naming convention (as described in the above guide).

Other than that, it's similar to modularized code in any other language. The canonical language in Arduino libraries is C++.

Since a regular Arduino IDE installation already comes preinstalled with a multitude of libraries, you can take a look and go from there.

I recommend that you look through the example sketches (from the "Examples" menu option). Whenever you see an #include statement at the top of the sketch, you have a hint where to look next in order to gain a better understanding about the inner works.

0

The sketch code can be easily split into several files by creating a new tab (file) without file name extension or .ino (source). No need for #includes as the Arduino IDE combines these files for the build process.

For proper modularisation make a Library or see the link above for further information on using .h/.c/.cpp as modules.

0

I would like to create a program with separated modules, is this this possible using Arduino IDE?

the answer depends on your code.

writing modular code is mostly a thought process:

1) you have to design your code to a logic model: rather than setting an led on pin 2, you design your code to set a pin, use that code to set a pin specified as LED_PIN, and then define LED_PIN to be pin 2. With this approach, you can simply redefine LED_PIN in your new project to use the same code.

2) you have to design your code top-down: rather than start writing your code right away, break it down to logic blocks, design the input and output of each block so that they work together cohesively. this is the toughest part of writing modular code.

after that, breaking the code down to .c/.h files is a piece of cake.

0

Well.... The answers are interessting but depend on what JuanV is thinking about when he say "separated modules"... For me, as an "old timer software dev", "separated modules" mean I build a central bloc of code (a nutshell) and the user download module which are automaticly "plugged" to the nutshell, expanding the feature. A bit like a printer driver you add to your system, and which add feature for all software. When I look at my past dev using this, I must admit I've some problems thinking of that on Arduino. If you are looking for that you must think about various problems:

1) how to update? Maybe with an Android App getting the code from the Web and sending these data to the Arduino via Bluetooth. You can also think of adding a Wifi interface on the Arduino

2) where to store the new piece of code... It's seem you can write on flash "on the fly" but seems to be complicated

Due to this second point, maybe the easiest way would be to create a nutshell which will "interpret" data (so which read a kind of small script) and then you'll have just to send the data. I suggest to have a look at the CNC made with Arduino. In most cases they use a GCode interpreter. GCode is a descriptif langage for CNC machine. So the interpreter read the code and according to the code, send "orders" to the machine. Maybe such a thing would be a good starting point.

On the other way, if what you call "modular approach" is just the way of building lib and re-use them, Lord Gonk answer is the way :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.