Write better Arduino code with advanced C++ features

Frank Buss
Oct 3, 2018 · 1 min read

The Arduino IDE allows to use advanced C++ features like classes and operator overloading. There are not many Arduino sketches that uses these features, but it can help to make your code more readable and maintainable.

This is an example how to make the standard “blink” example easier to read:

So instead of writing digitalWrite(LED_BUILTIN, HIGH) whenever you want to light up the LED, now you can just write led = 1. You can create more objects for other pins as well, which makes your program much easier to read.

Left as an exercise to the reader: implement a class for reading pins.

You could move the class to a separate library, then you need just one include to use the class, without always copying the class declaration in your sketch.

This example was inspired by the PortOut class of the mbed framework.

Frank Buss

Written by

Software, embedded systems and FPGA developer.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade