Creating new classes and add them to your library


Arduino comes with a library full of useful classes, but at some point you will consider writing your own classes rather than copying code again and again. When you are looking for guidelines on how to write classes of your own you get advised first to write and test your code in a normal include file. That's always a good idea.

From a technical point of view, writing a C++ class is like filling in a form; just get yourself a template, replace everything by your own code, place it in a newly created directory next to the other libs, and write a sketch to test it. (In case you are going to publish your class make sure your test file is located in an examples directory in the library.)
Actually, back in 1970, Brian Kernighan and Dennis Ritchie decided to force you to produce a separate header file. (James Gosling got rid of it, but that's another story.) It's not always easy to decide which part of your code goes to the cpp-file respectively the h-file.

In most cases, you will get compile errors, or the code won't do what it was meant to do. So you probably have to edit it more than once. Depending on your operating system and your user status, you might not have the right to edit files located in the Arduino/libraries directory. Also, you need an Arduino Window to edit your test program. Not to get confused, better use different editor to edit the cpp file and the header file, e.g. just use Windows notepad.

Always keep in mind: your sketches, even the test program can use only the library as it was found when starting the IDE. But the contents of the lib files will only be read when compiling. So you may update existing files at any time without restarting the IDE.

Now the most important hint:
If your operating system prevents you from saving your files in the Arduino/libraries directory temporarily just use a different drive instead, e.g. a pen drive. After saving a new version to that drive simply use the explorer to copy (not move) this file(s) into the Arduino/libraries directory. Of course you will be prompted for replacing and admin rights.

If you (or members of your group) are developing sketches using different computers at different places make sure they all have access to the same libraries. Updating a library only at one place will have dramatic consequences.




contact: nji(at)gmx.de