Chapter 11. Custom Libraries

We’ve already seen how to include the headers of useful libraries that come with the Arduino IDE as well as how to add third-party libraries for some of the more interesting peripherals and sensors. But as you build up your catalog of microcontroller projects, you’re likely to create some code that you reuse. I regularly employ the maxim that there are only three numbers in the universe: 0, 1, and many. If you find a chunk of code you use a second time, you’re in that “many” category and it is probably time to consider making your own library.

That statement may seem dramatic. It certainly sounds grandiose. Happily, it is a fairly straightforward process and really does make reuse a snap down the road.

I do want to admit that the project in this chapter is bigger than our past projects in every sense. If remote-control robotic cars don’t spike your interest, feel free to skip the bulk of this chapter. I would still recommend reading “Multifile Projects” and “Creating the Library” to get a sense of the steps involved in writing a library for your own use within the Arduino IDE. You can safely skip this chapter in favor of exploring an IoT project in Chapter 12.

Creating Your Own Library

To get started on a custom library, you’ll need some code to reuse. The best way to find reusable code is to create some usable code first. We can kick off a normal project and then extract the parts that seem likely to work well in other projects. ...

Get Smaller C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.