all leds are individually responding well

This commit is contained in:
2024-11-30 15:07:07 +01:00
parent 8808d86305
commit c45a673fc0

View File

@@ -1,60 +1,61 @@
#include <FastLED.h>
#include <Adafruit_GFX.h>
#include <Fonts/FreeSerif9pt7b.h> // Optional: Use any font from Adafruit_GFX
#define DATA_PIN D2 // Data pin for the matrix
#define NUM_LEDS 256 // Total LEDs (8 rows x 32 columns)
#define LED_TYPE WS2812 // LED type
#define COLOR_ORDER GRB // Color order
// Params for width and height
const uint8_t kMatrixWidth = 32;
const uint8_t kMatrixHeight = 16;
CRGB leds[NUM_LEDS]; // Array to store the LED colors
Adafruit_GFX *gfx; // Pointer to the graphics object
int textX = 32; // Starting X position for scrolling text
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
#define DATA_PIN D2
#define LAST_VISIBLE_LED 511
CRGB leds[NUM_LEDS];
uint16_t XY(uint8_t x, uint8_t y) {
// any out of bounds address maps to the first hidden pixel
if ((x >= kMatrixWidth) || (y >= kMatrixHeight)) {
return (LAST_VISIBLE_LED + 1);
}
const int XYTable[] = {
248, 247, 232, 231, 216, 215, 200, 199, 184, 183, 168, 167, 152, 151, 136, 135, 120, 119, 104, 103, 88, 87, 72, 71, 56, 55, 40, 39, 24, 23, 8, 7,
249, 246, 233, 230, 217, 214, 201, 198, 185, 182, 169, 166, 153, 150, 137, 134, 121, 118, 105, 102, 89, 86, 73, 70, 57, 54, 41, 38, 25, 22, 9, 6,
250, 245, 234, 229, 218, 213, 202, 197, 186, 181, 170, 165, 154, 149, 138, 133, 122, 117, 106, 101, 90, 85, 74, 69, 58, 53, 42, 37, 26, 21, 10, 5,
251, 244, 235, 228, 219, 212, 203, 196, 187, 180, 171, 164, 155, 148, 139, 132, 123, 116, 107, 100, 91, 84, 75, 68, 59, 52, 43, 36, 27, 20, 11, 4,
252, 243, 236, 227, 220, 211, 204, 195, 188, 179, 172, 163, 156, 147, 140, 131, 124, 115, 108, 99, 92, 83, 76, 67, 60, 51, 44, 35, 28, 19, 12, 3,
253, 242, 237, 226, 221, 210, 205, 194, 189, 178, 173, 162, 157, 146, 141, 130, 125, 114, 109, 98, 93, 82, 77, 66, 61, 50, 45, 34, 29, 18, 13, 2,
254, 241, 238, 225, 222, 209, 206, 193, 190, 177, 174, 161, 158, 145, 142, 129, 126, 113, 110, 97, 94, 81, 78, 65, 62, 49, 46, 33, 30, 17, 14, 1,
255, 240, 239, 224, 223, 208, 207, 192, 191, 176, 175, 160, 159, 144, 143, 128, 127, 112, 111, 96, 95, 80, 79, 64, 63, 48, 47, 32, 31, 16, 15, 0,
256, 271, 272, 287, 288, 303, 304, 319, 320, 335, 336, 351, 352, 367, 368, 383, 384, 399, 400, 415, 416, 431, 432, 447, 448, 463, 464, 479, 480, 495, 496, 511,
257, 270, 273, 286, 289, 302, 305, 318, 321, 334, 337, 350, 353, 366, 369, 382, 385, 398, 401, 414, 417, 430, 433, 446, 449, 462, 465, 478, 481, 494, 497, 510,
258, 269, 274, 285, 290, 301, 306, 317, 322, 333, 338, 349, 354, 365, 370, 381, 386, 397, 402, 413, 418, 429, 434, 445, 450, 461, 466, 477, 482, 493, 498, 509,
259, 268, 275, 284, 291, 300, 307, 316, 323, 332, 339, 348, 355, 364, 371, 380, 387, 396, 403, 412, 419, 428, 435, 444, 451, 460, 467, 476, 483, 492, 499, 508,
260, 267, 276, 283, 292, 299, 308, 315, 324, 331, 340, 347, 356, 363, 372, 379, 388, 395, 404, 411, 420, 427, 436, 443, 452, 459, 468, 475, 484, 491, 500, 507,
261, 266, 277, 282, 293, 298, 309, 314, 325, 330, 341, 346, 357, 362, 373, 378, 389, 394, 405, 410, 421, 426, 437, 442, 453, 458, 469, 474, 485, 490, 501, 506,
262, 265, 278, 281, 294, 297, 310, 313, 326, 329, 342, 345, 358, 361, 374, 377, 390, 393, 406, 409, 422, 425, 438, 441, 454, 457, 470, 473, 486, 489, 502, 505,
263, 264, 279, 280, 295, 296, 311, 312, 327, 328, 343, 344, 359, 360, 375, 376, 391, 392, 407, 408, 423, 424, 439, 440, 455, 456, 471, 472, 487, 488, 503, 504
};
int i = (y * kMatrixWidth) + x;
int j = XYTable[i];
return j;
}
void setup() {
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS); // Set up FastLED
FastLED.setBrightness(50); // Set brightness level (0-255)
gfx = new Adafruit_GFX(32, 8); // Initialize the graphics object for a 32x8 matrix
// Initialize the matrix (clear it first)
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.clear();
FastLED.show();
}
void loop() {
// Message to scroll
String message = "Hello World! ";
// Scroll the message across the screen
for (int x = textX; x >= -message.length() * 6; x--) {
// Clear the previous frame
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Draw the text at the current position (x)
gfx->setCursor(x, 0);
gfx->setTextColor(CRGB::White); // Set text color
gfx->setTextSize(1); // Set text size
gfx->print(message); // Print the message
// Copy the graphics buffer to the LED array
drawMatrixToLEDs();
// Show the updated LEDs
for (int y = 0; y < kMatrixHeight; y++) {
for (int x = 0; x < kMatrixWidth; x++) {
int index = XY(x, y);
if (index <= LAST_VISIBLE_LED) {
leds[index] = CRGB::White;
FastLED.show();
delay(100); // Adjust the speed of the scroll
}
// Reset the starting point for the scroll after it goes off-screen
textX = 32;
}
// Function to copy the Adafruit_GFX buffer to the LED array
void drawMatrixToLEDs() {
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 8; j++) {
int pixelColor = gfx->getTextColor(); // Get the color of the pixel (white or black)
leds[j + i * 8] = (pixelColor == CRGB::White) ? CRGB::White : CRGB::Black;
delay(10);
leds[index] = CRGB::Black;
}
}
}
}