Fork me on GitHub

Project Notes

#486 UsingTemplates

Learning how to use C++ template functions in embedded projects with an Arduino.

Build

Notes

LuckyResistor has just published C++ Templates For Embedded Code, the first in a series on the subject of C++ templates.

This is great! Although I’ve dabbled in C++ for years, I’ve never fully taken templates on board. Proving that they can be beneficial for embedded development is a bonus.

The First Example - circularShift

I’ve taken the circularShift example from the article, and extended the code a little to toggle the LED on pin 13 everytime circularShift wraps a 1 around.

One important point to note if you are copying code from the article:

The declaration and implementation of a function template have to be in a header file

With Arduino IDE 1.8.2, I get a compiler error like this if I try and put templates in the main sketch file:

UsingTemplates:12: error: variable or field 'circularShift' declared void
 void circularShift(Value &value) {
                    ^
UsingTemplates:12: error: 'Value' was not declared in this scope
UsingTemplates:12: error: 'value' was not declared in this scope
 void circularShift(Value &value) {
                           ^
exit status 1
variable or field 'circularShift' declared void

Putting the template in a header file fixes this. Also, Arduino IDE 1.8.9 does not have this issue - it can compile templates in the sketch file.

Test Run

From the console, the first iterations checkout - correct right shift on both values (should improve this to include leading 0s in the output)

10101011110011010000000100100011
100010101000101
11010101111001101000000010010001
1010001010100010
11101010111100110100000001001000
101000101010001
1110101011110011010000000100100
1010100010101000

console

Construction

Breadboard

Schematic

Build

Credits and References

About LEAP#486 ArduinoC++

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

Project Source on GitHub Return to the LEAP Catalog
About LEAP

LEAP is my personal collection of electronics projects - usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting.

Projects are often inspired by things found wild on the net, or ideas from the many great electronics podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know or send a pull-request.

NOTE: For a while I included various scale modelling projects here too, but I've now split them off into a new repository: check out LittleModelArt if you are looking for these projects.

Project Gallery view the projects as an image gallery Notebook reference materials and other notes Follow the Blog follow projects and notes as they are published in your favourite feed reader