|
SBK_BarDrive Library 2.2.0
LED bar meter control and queued animations for Arduino
|
High-level Arduino library for controlling animated LED bar meters using MAX7219/MAX7221 or HT16K33 drivers. Ideal for prop-making, visual meters, and signal-driven lighting effects.
Version 2.2.0 adds loopQueue() and noLoopQueue() for repeating whole animation sequences. Pauses now freeze all animation and queue timers. Center and edge bounces reset their tracker correctly at phase transitions, avoiding invalid pixel requests and a wasted clearing interval.
Queue entries retain their slots for the entire pass, even when not looping. This enables repetition to be switched on during playback; applications that previously reused consumed slots should account for the retained capacity.
Version 2.1.2 expands animation scheduling with universal forTime() limits, state-preserving enqueueReverseAnim() transitions, and timed graceful block shutdown through stopBlockEmissionAfter(). Queue waits are now always non-looping, and the timing modifiers work with direct animations as well as queued entries.
Version 2.1.1 fixes the generic rows-by-columns matrix constructor so it initializes the logical segment count. Generic matrix displays now work with pixel operations and animations as documented.
Version 2.1.0 adds configurable, fixed-storage animation queues, queue index inspection and skipping, non-blocking wait(), single-pixel blinking, safer animation workspace allocation, and AVR-focused RAM and flash optimizations. See CHANGELOG.md for the complete development history. Potential future architecture work is recorded in ROADMAP.md.
Versions prior to 2.0.0 remain deprecated because of internal changes to offset handling and multi-device support.
This library is compatible with any LED matrix or bar display using MAX7219, MAX7221, or HT16K33 drivers — as long as a valid [dev, row, col], aka [device index, anode,cathode], segment mapping is provided or configured using a built-in preset.
When using custom segment mappings with SBK_BarDrive, each segment is defined as a {dev, row, col} — representing the physical LED connection:
{dev, row, col} = {device index, anode, cathode}
This matches the wiring convention of common LED driver ICs:
| Driver | Row (Anode / V+) | Column (Cathode / GND) |
|---|---|---|
| MAX72xx | SEGx (source, V+) | DIGx (sink, GND) |
| HT16K33 | Rx (source, V+) | Cx (sink, GND) |
SBK offers purpose-built PCBs to simplify bar meter wiring, driver integration, and display control. These include:
Some models are available via the SBK Tindie Store or upon request by emailing SmartBuildsKits@gmail.com.
📁 For schematics, 3D models, and PCB source files, visit:
🔗 https://github.com/sbarabe/SBK_PCBs
The SBK_BarDrive library depends on one of the following display driver libraries to function:
| Dependency | Description | Required For |
|---|---|---|
| SBK_MAX72xx | Hardware and software SPI drivers for MAX7219/MAX7221 LED drivers | MAX72xx-based displays |
| SBK_HT16K33 | I²C driver for HT16K33 LED driver (8x16 matrices or bar displays) | HT16K33-based displays |
You must install at least one of these drivers depending on your hardware.
If using PlatformIO or Arduino Library Manager, these will be installed automatically as dependencies.
To manually install:
Then place them in your Arduino libraries folder.
Download or clone the library into your Arduino libraries folder:
In your Arduino sketch, enable features as needed:
Each SBK_BarDrive animation controller sizes its working buffers from the actual number of segments and allocates them once during construction. The same buffers are reused by every animation, so changing animations does not allocate or free heap memory. Block storage scales with the bar size and keeps the existing maximum of 64 simultaneous blocks.
See animationQueueDemo for a four-entry queue demonstrating a hard time limit, state-preserving logic reversal, graceful block-emission shutdown, and a non-blocking wait() entry.
If you want full control over how segments map to physical LED positions, you can supply a custom [device, row, col] mapping array. This is ideal for irregular layouts or displays spanning multiple devices.
These functions start animations and can be chained with modifiers like .loop() or .pause().
These methods allow you to control, manipulate, or conditionally alter animations at runtime. Most are event-driven triggers—ideal for reacting to user input, state transitions, or timed events.
You can chain these calls because they return a reference to the active animation controller:
pause() freezes the current animation and preserves both its state and the visible pixels so that resume() can continue it. stop() fully terminates the current animation, disables looping, clears the pending queue, and clears the mapped pixels in the driver's buffer. Call bar.show() afterward when the cleared buffer must be sent to the display immediately.
All helper functions return a reference to the SBK_BarMeterAnimations object, allowing chainable expressions like:
When using a custom timestamp with update(syncTime), pass the same clock to pause(syncTime) and resume(syncTime). No-argument calls use millis().
Use loopQueue() before startQueue() to repeat the entire sequence:
Call bar.animations().noLoopQueue() to finish the current pass and then stop. Queue repetition is disabled by default and is independent of each animation's loop() / noLoop() setting. An endless animation still needs a time limit for the queue to advance. loopQueue() can also enable repetition during an active pass. stop() and clearQueue() discard the sequence and disable queue repetition. skipCurrent() on the final entry starts the next pass when looping.
Entries occupy their queue slots until the sequence ends or is cleared, so replay needs no extra entry array or dynamic allocation. queuedAnimations() counts only entries remaining after the active entry in the current pass. The first entry starts with fresh runtime state on each replay; state-preserving transitions within the sequence retain their usual behavior. pause() / resume() freeze and resume playback and its timers, including while the queue repeats.
Up to SBK_BARDRIVE_QUEUE_CAPACITY configured animations can be queued without dynamic allocation. The default capacity is four. Configure an animation, apply any modifiers, and capture it with enqueue():
Calling update() automatically starts the next entry when the current one finishes. queuedAnimations() reports the number still waiting, isQueueRunning() reports whether a queue is active, and queueOverflowed() reports an attempt to exceed the configured capacity. currentQueueIndex() returns the physical queue slot currently playing, or NO_QUEUE_INDEX when none is active. isQueueIndexPlaying(index) provides a direct test, for example:
Use the universal forTime(duration) modifier when a direct, looping, or continuous animation needs a hard deadline. It can be applied before or after enqueue(); enqueueFor(duration) remains an equivalent convenience API. enqueueReverseAnim() copies the preceding entry, reverses its supported logic, and preserves its runtime state at the handoff. Configure looping before the command and timing after it with the standard modifiers:
Here the colliding animation loops for three seconds. The reversed entry then turns its in-flight blocks into exploding blocks and may loop until its three-second emission deadline. It then advances after the remaining blocks have left the display.
For an ordinary queued emitting-block animation, use the specialized modifier to stop new emissions after a deadline and let active blocks drain naturally:
After a direct animation starter, stopBlockEmissionAfter() schedules that active animation. Before enqueue(), it is captured by the new entry; after an enqueue operation, it modifies the most recently queued entry. It has no effect on animation types that do not emit blocks and, unlike enqueueFor(), does not force an immediate transition at the deadline.
Use wait(duration) to add a non-blocking delay that preserves the current pixel states:
An individually looped or continuous animation intentionally prevents later entries from starting automatically. Call skipCurrent() to abandon it and immediately continue with the next queued animation. Unlike stop(), skipping does not clear pending entries.
| Class | Purpose |
|---|---|
| SBK_BarMeter | Handles segment mapping and direction logic |
| SBK_BarDrive | Wrapper that adds animation support |
| SBK_BarMeterAnimations | Provides animation control interface |
| SBK_MAX72xx | Hardware/software SPI drivers for MAX7219/MAX7221 |
| SBK_HT16K33 | I2C driver for HT16K33 8x16 LED matrices |
Licensed under the MIT License.
Licensed under Creative Commons Attribution 4.0 (CC BY 4.0).
You are free to share and adapt the material, provided you give appropriate credit.
Library by Samuel Barabé (Smart Builds & Kits).