Here's a modular Arduino code that:
- Controls a Pixel LED Seven Segment Display (e.g., made from WS2812 LEDs),
- Lets you change the number of digits and LEDs per segment via code,
- Uses debounce pushbuttons for:
- Increasing and decreasing the displayed number (2 buttons),
- Changing color (1 button),
- Uses the Serial Monitor for debugging/logs.
✅ Hardware Assumptions
- WS2812 LEDs (a.k.a. NeoPixels),
- Each digit = 7 segments,
- LEDs per segment = configurable,
- Buttons connected to GPIO pins with pull-down or pull-up resistors,
- Powered adequately (use capacitor + resistor as per NeoPixel specs).
✅ Libraries Required
Install via Library Manager:
- Adafruit_NeoPixel.h
✅ Wiring Example
- DIN (Data In) of LED → D6
- INC Button → D2
- DEC Button → D3
- Color Button → D4
✅ Arduino Code
#include <Adafruit_NeoPixel.h>
// === CONFIGURATION ===
#define LED_PIN 6 // Data pin
#define NUM_DIGITS 2 // Total number of digits
#define LEDS_PER_SEGMENT 4 // Number of LEDs in each segment
#define COLOR_BUTTON 4
#define INC_BUTTON 2
#define DEC_BUTTON 3
#define DEBOUNCE_DELAY 50
const uint8_t segmentsPerDigit = 7;
const uint8_t totalSegments = NUM_DIGITS * segmentsPerDigit;
const uint16_t totalLEDs = totalSegments * LEDS_PER_SEGMENT;
Adafruit_NeoPixel strip(totalLEDs, LED_PIN, NEO_GRB + NEO_KHZ800);
unsigned long lastDebounceTimeInc = 0;
unsigned long lastDebounceTimeDec = 0;
unsigned long lastDebounceTimeColor = 0;
int currentNumber = 0;
int currentColorIndex = 0;
uint32_t colors[] = {
strip.Color(255, 0, 0),
strip.Color(0, 255, 0),
strip.Color(0, 0, 255),
strip.Color(255, 255, 0),
strip.Color(0, 255, 255),
strip.Color(255, 0, 255),
strip.Color(255, 255, 255)
};
const int totalColors = sizeof(colors) / sizeof(colors[0]);
// === 7-Segment LED patterns ===
// a, b, c, d, e, f, g
const uint8_t digitSegments[10][7] = {
{1,1,1,1,1,1,0}, // 0
{0,0,1,1,0,0,0}, // 1
{0,1,1,0,1,1,1}, // 2
{0,1,1,1,1,0,1}, // 3
{1,0,1,1,0,0,1}, // 4
{1,1,0,1,1,0,1}, // 5
{1,1,0,1,1,1,1}, // 6
{0,1,1,1,0,0,0}, // 7
{1,1,1,1,1,1,1}, // 8
{1,1,1,1,1,0,1} // 9
};
// === SEGMENT ORDER ===
// Segment index: f(0), a(1), b(2), c(3), d(4), e(5), g(6)
const int segmentOrder[7] = {0, 1, 2, 3, 4, 5, 6};
// === BUTTON STATE ===
bool lastIncState = LOW;
bool lastDecState = LOW;
bool lastColorState = LOW;
// === FUNCTION TO LIGHT A SEGMENT ===
void lightSegment(uint16_t segIndex, uint32_t color) {
uint16_t startLED = segIndex * LEDS_PER_SEGMENT;
for (uint16_t i = 0; i < LEDS_PER_SEGMENT; i++) {
strip.setPixelColor(startLED + i, color);
}
}
// === DISPLAY NUMBER ===
void displayNumber(int number) {
strip.clear();
for (int d = 0; d < NUM_DIGITS; d++) {
int digit = (number / (int)pow(10, NUM_DIGITS - 1 - d)) % 10;
for (int s = 0; s < 7; s++) {
if (digitSegments[digit][s]) {
int segIndex = d * segmentsPerDigit + segmentOrder[s];
lightSegment(segIndex, colors[currentColorIndex]);
}
}
}
strip.show();
Serial.print("Number: "); Serial.println(number);
}
// === SETUP ===
void setup() {
Serial.begin(9600);
pinMode(COLOR_BUTTON, INPUT_PULLUP);
pinMode(INC_BUTTON, INPUT_PULLUP);
pinMode(DEC_BUTTON, INPUT_PULLUP);
strip.begin();
strip.show();
displayNumber(currentNumber);
}
// === LOOP ===
void loop() {
bool incState = !digitalRead(INC_BUTTON);
bool decState = !digitalRead(DEC_BUTTON);
bool colorState = !digitalRead(COLOR_BUTTON);
unsigned long now = millis();
// === INCREMENT ===
if (incState && !lastIncState && now - lastDebounceTimeInc > DEBOUNCE_DELAY) {
lastDebounceTimeInc = now;
currentNumber++;
if (currentNumber >= pow(10, NUM_DIGITS)) currentNumber = 0;
displayNumber(currentNumber);
}
lastIncState = incState;
// === DECREMENT ===
if (decState && !lastDecState && now - lastDebounceTimeDec > DEBOUNCE_DELAY) {
lastDebounceTimeDec = now;
currentNumber--;
if (currentNumber < 0) currentNumber = pow(10, NUM_DIGITS) - 1;
displayNumber(currentNumber);
}
lastDecState = decState;
// === CHANGE COLOR ===
if (colorState && !lastColorState && now - lastDebounceTimeColor > DEBOUNCE_DELAY) {
lastDebounceTimeColor = now;
currentColorIndex = (currentColorIndex + 1) % totalColors;
displayNumber(currentNumber);
Serial.print("Color changed to index: "); Serial.println(currentColorIndex);
}
lastColorState = colorState;
}
✅ To Modify
- To change number of digits, update NUM_DIGITS,
- To change LEDs per segment, update LEDS_PER_SEGMENT,
- Adjust pin numbers if you use different buttons or LED data lines.
No comments:
Post a Comment