SBK_BarDrive Library 2.2.1
LED bar meter control and queued animations for Arduino
Loading...
Searching...
No Matches
segmentCountDemo.ino
Go to the documentation of this file.
1/**
2 * @file segmentCountDemo.ino
3 * @brief Demonstrates SBK_BarDrive using only segment count for 1D bar meter type (auto-mapped).
4 *
5 * When the bar meter is not based on a 2D matrix layout, the SBK_BarDrive instance
6 * can be created using only the segment count — as long as the physical wiring
7 * follows the expected linear (sequential) order.
8 *
9 * ⚠️ Important: This approach assumes that the LEDs are wired sequentially from
10 * the driver's output pins, following the native segment order:
11 *
12 * - For MAX72xx: SEG0/COL0 → SEG0/COL1 → ... → SEG70/COL7, then SEG1/COL0, etc.
13 * - For HT16K33: ROW0/COL0 → ROW0/COL1 → ... → ROW0/COL7, then ROW1/COL0, etc.
14 *
15 * Ensure your bar meter is wired to match this order for correct auto-mapping behavior.
16 *
17 * Requirements:
18 * - Supported driver with complatible library (SBK_MAX72xx or SBK_HT16K33 libraries)
19 * - Bar meter display or leds array wired to driver
20 *
21 * @author
22 * Samuel Barabé (Smart Builds & Kits)
23 *
24 * @version 2.2.0
25 * @license MIT
26 */
27
28#include <Arduino.h>
29
30// ──────────────────────────────────────────────
31// SBK BarDrive Configuration Flags
32// ──────────────────────────────────────────────
33#define SBK_BARDRIVE_WITH_ANIM ///< Give access to preset animations and controls.
34
35// ──────────────────────────────────────────────
36// SELECT YOUR DRIVER SETUP
37// Uncomment one of the following driver configurations
38// ──────────────────────────────────────────────
39
40/* === [A] Using MAX7219/MAX7221 via SOFTWARE SPI (any 3 digital pins) === */
41// #define DIN_PIN A4 ///< Define software SPI Data In pin
42// #define CLK_PIN A5 ///< Define software SPI Clock pin
43// #define CS_PIN A3 ///< Define SPI Chip Select pin
44// #include <SBK_MAX72xxSoft.h>
45// SBK_MAX72xxSoft driver(DIN_PIN, CLK_PIN, CS_PIN, 1); ///< Construct MAX72xx software SPI driver instance for 1 device : (DataIn pin, Clock pin, Chip Select pin, devices number)
46// #include <SBK_BarDrive.h>
47// SBK_BarDrive<SBK_MAX72xxSoft> bar(&driver, 0, 24); ///< Construct using segment count (auto-mapped layout) : (driver, device index, number of bar segments)
48
49/* === [B] Using MAX7219/MAX7221 via HARDWARE SPI (dedicated MCU SPI pins) === */
50// #define CS_PIN A3 ///< Define SPI Chip Select pin
51// #include <SBK_MAX72xxHard.h>
52// SBK_MAX72xxHard driver(CS_PIN, 1); ///< Construct MAX72xx hardware SPI driver instance for 1 device : (Chip Select pin, devices number)
53// #include <SBK_BarDrive.h>
54// SBK_BarDrive<SBK_MAX72xxHard> bar(&driver, 0, 24); ///< Construct using segment count (auto-mapped layout) : (driver, device index, number of bar segments)
55
56/* === [C] Using HT16K33 via I2C === */
57#include <SBK_HT16K33.h>
58const uint8_t NUM_DEV = 1; ///< Only one device : DEV0
59const uint8_t DEV0_IDX = 0; ///< Device DEV0 index
60const uint8_t DEV0_ADD = 0x70; ///< I2C Address (typically 0x70–0x77)
61const uint8_t DEV0_NUM_ROWS = 8; ///< 20-SOP HT16K33 with only 8 rows, 24-SOP has 12 rows, 28-SOP has 16 rows
62SBK_HT16K33 driver(NUM_DEV);
63#include <SBK_BarDrive.h>
64SBK_BarDrive<SBK_HT16K33> bar(&driver, 0, 24); ///< Construct using segment count (auto-mapped layout) : (driver, device index, number of bar segments)
65
66/*
67 * Segment mapping explanation:
68 *
69 * When using a segment count constructor (e.g., 24), the library auto-generates a
70 * row-column mapping assuming a default 8×8 LED matrix layout.
71 *
72 * Segments are assigned left to right, top to bottom:
73 * - Segment index 0 → [0,0]
74 * - Segment index 1 → [0,1]
75 * - ...
76 * - Segment index 7 → [0,7]
77 * - Segment index 8 → [1,0]
78 * - Segment index 9 → [1,1]
79 * - Segment index 10 → [1,2]
80 * - ...
81 * - Segment index 23 → [2,7]
82 *
83 * This layout is convenient for generic displays or matrix testing,
84 * but may not your actual bar meter wiring with the IC chip driver.
85 * For this layout to work, you must wire the bar meter display to
86 * the IC driver chip filling the row in order.
87 *
88 * Examaple : to match this mapping with 3× B08 bar meter displays (3×8 = 24 segments)
89 * using a MAX7219/MAX7221 IC, you must wire each display such that **columns fill first**:
90 *
91 * First B08 bar meter (SEG 0–7 wired to DIG_0 aka row 0)):
92 * SEG 0 ---SEG_DP---|>|--- DIG_0
93 * SEG 1 ---SEG_A----|>|--- DIG_0
94 * SEG 2 ---SEG_B----|>|--- DIG_0
95 * ... ...
96 * SEG 7 ---SEG_G----|>|--- DIG_0
97 *
98 * Second B08 bar meter (SEG 8–15 wired to DIG_1 aka row 1 ):
99 * SEG 8 ---SEG_DP---|>|--- DIG_1
100 * ...
101 * SEG 15 ---SEG_G----|>|--- DIG_1
102 *
103 * Third B08 bar meter (SEG 16–23 wired to DIG_2 aka row 2):
104 * SEG 16 ---SEG_DP---|>|--- DIG_2
105 * ...
106 * SEG 23 ---SEG_G----|>|--- DIG_2
107 *
108 * ✅ Use a MatrixPreset or custom mapping array if your wiring does not
109 * follow this column-first convention.
110 */
111
112void setup()
113{
114#ifdef SBK_HT16K33_IS_DEFINED
115 // HT16K33 driver instance setup (demo uses a single device)
116 driver.setAddress(DEV0_IDX, DEV0_ADD); ///< Set I2C address for device 0
117 driver.setDriverRows(DEV0_IDX, DEV0_NUM_ROWS); ///< Set number of active anode outputs (rows)
118#endif
119
120 driver.begin();
121 driver.setBrightness(0, 10);
122
123 bar.setDirection(BarDirection::FORWARD);
124
125 bar.animations().animInit().fillUpIntv(25).loop();
126}
127
128void loop()
129{
130 bar.animations().update();
131 bar.show();
132}