If you're coming to Arduino from programming in C on other platforms then your skills are directly transferable of course as Arduino programming is C and C++. Here's our notes on issues to consider:

(Yes an Arduino Project is a sketch – sorry but we call such things projects so you'll find we refer to project here and elsewhere where more correctly for Arduino we should say sketch)

Arduino uses spaces instead of tabs

By default Arduino uses 2 spaces instead of a tab. For us this is horrible as we prefer tabs and if not tabs then 4 spaces to make the indentation clearer and easier to follow in large blocks of code. It can be changed in the preferences.txt file (see File > Preferences for its location) but it seems sensible to stay with the Arduino default spacing method to ensure code is nicely portable to other Arduino users and the 2  space approach is also often used for PHP and JavaScript in applications like Dreamweaver so its just a difference of opinion rather than a horrible action they've taken!

Change hyphens to underscores in file names

Arduino doesn't like hyphens in file names.  It can accept files with them but it won't let you use them when renaming or creating files.  Use underscore instead. Apparently its something to do with a historical compiler issue.

Use the .cpp extension instead of .c

Arduino will accept both files types but can throw odd errors for straight C code in a .C file where it really shouldn't. We find that just renaming .c files to .cpp solves the problem without any need to actually use C++ within the files.

Don't just copy files into Arduino projects

You can do this for files you simply want to include, but to add existing files to an Arduiono project where you want to open those files within the Arduino IDE instead copy them to somewhere temporary outside of the project and then use Menu > Sketch > Add File.  The files will be copied into the project and appear in the IDE tabs.  If you copy the file into the project and then add it you'll end up with the file deleted as, at least for now, the IDE isn't intelligent enough to deal with this.

Yes you can use .c, .cpp and .h files as in a normal C/C++ development environment, but

Arduino includes aren't exactly like other C environments in that libraries are not necessarily included where you assume. This is how we use the Arduino IDE in case its helpful (you can download our example project here):

Our main .ino sketch file is our equivalent to a main.cpp file in other projects.  It has the setup() and loop() functions.

We have a file ap__main.h which acts as it's header file so we can include it in other files when we want to share variables and functions etc.

We have a file called global.h in which we have common typdefs etc that we include in all .cpp files in our project. Its not necessary if you only use Arduino variable types, but its very handy when adding in code that uses other types which need typedef'ing and for creating handy typedef unions etc.

We then add .cpp and .h file pairs for functionality we want to separate out so a large project becomes more easily manageable and include the .h files in other .cpp files and the main .ino file where we want to access its functions or variables.

When we use a library such as say LiquidCrystal within the .cpp and .h files we add the "#include <LiquidCrystal.h>" to the top of .cpp file using it, but also to the top of main project .ino file.  Without this you get errors:- the main .ino file needs it so the compiler includes it properly and the .cpp file needs it because it is referring to things in it.

The Arduino IDE isn't brilliant in areas

Get over it – it is what it is. As a platform which has brought people together and created an amazing community it is fantastic so forgive its annoyances in places compared to other IDE's. If you really hate aspects of it maybe join the development effort to help improve it.

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

  1. Petirrojo

    9 years ago

    I don not who You are because Yo had not wrtten your name as autor in the post, but I want to thank you very much…. I am a PIC programmer for a long time; for two weeks I was trying to include c file into Arduino sketch with a simple EEPROM routine (read and write), but IDE errors were pulling me out my nerves; with 12th paragraph advice, my code is working thanks to you :)

Comments

Your email address will not be published. Required fields are marked *