How it works
Write your own Arduino library is an intermediate course in C++ on an Arduino Uno. Every lesson runs on the gallery's doomsday device: a TM1637 four-digit display on A0 (CLK) and A1 (DIO), three buttons on pins 6, 7 and 8, and three lamps on pins 2, 3 and 4.
The first lesson opens one tab. From the second on, the board opens with three:
TinyDigits.h, TinyDigits.cpp and the sketch, as a real library and the
sketch that uses it. You run it, then change the library and run again. Some
checks read the serial monitor, and some count the display's lit segments, so
a step about drawing 01:30 is only passed when 01:30 is really on the glass.
The lessons
- Header and .cpp: a driver bit-banged from the datasheet (start, stop,
eight bits LSB first and an ACK on the ninth clock), split into a header
that says what it does and a .cpp that says how. Include guards,
staticfor private, andexternfor the one-definition rule. - Class design: TinyDigits as a class. A constructor that touches no
hardware,
begin()that reports whether a chip answered, methods that refuse what they cannot do, a new method, a default argument, and a second display on pins with nothing wired. - Examples: a kitchen-sink demo replaced by three examples that each teach one thing: HelloDigits, a button Countdown and a SelfTest.
- Docs and keywords: doc comments that state ranges and failures, help
printed from the library's own constants, a doc that promised -9999 when
four digits stop at -999, and
keywords.txtfor the IDE. - Versioning: MAJOR.MINOR.PATCH as numbers a sketch tests in
#if, a patch for a tidy-up that blanked zero, and a breaking change tosetBrightnessthat earns 2.0.0. - Zip and share: the Arduino library layout,
library.propertiesprinted from the header so the version lives in one place, a release check that refuses to ship, and the zip installed with + Add library in Mokxi and Add .ZIP Library in the Arduino IDE.
The project: a mission clock
A launch countdown on the finished library, with START, HOLD and RESET
buttons, lamps for counting, liftoff and hold, and a millis() beat that never
drifts. You add showText() to the library as a minor release (2.1.0), make
the clock say GO at liftoff, then make it yours: a minute count with the
colon, a pulsing last three seconds, or a second display.
What the Uno does differently
- A sketch tab gets Arduino's names for free; a
.cpptab has to start with#include <Arduino.h>, as in the Arduino IDE. - The TM1637 is not I2C, so the driver uses two ordinary pins. A0 and A1 work as digital pins 14 and 15.
- The library uses the chip's own pull-ups (
INPUT_PULLUP) to let the lines float high, so a pin with nothing on it reads high andbegin()sees no ACK. - An
intis 16 bits on the Uno, soshowNumberkeeps its digits in anunsigned intand its range inside -999 to 9999.
Before and after
C++ for real programs covers the classes, references and headers this course builds on. The finished library works in any project: add the zip under Libraries and include it with angle brackets.