Advertisement
Guest User

Untitled

a guest
Nov 25th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.81 KB | Source Code | 0 0
  1. \#include <FastLED.h>
  2.  
  3. // LED configuration  
  4. \#define LED\_PIN     6  
  5. \#define NUM\_LEDS    164  // 41 x 4  
  6. \#define BRIGHTNESS  50  
  7. \#define COLOR\_ORDER GRB
  8.  
  9. CRGB leds\[NUM\_LEDS\];
  10.  
  11. // Grid size  
  12. \#define WIDTH  41  
  13. \#define HEIGHT 4
  14.  
  15. // Text settings  
  16. const char\* text = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z  0 1 2 3 4 5 6 7 8 9";  
  17. int scrollSpeed = 100; // Speed of scrolling (ms)
  18.  
  19. // 4x5 font data (A-Z, 0-9, space)  
  20. const uint8\_t font\[\]\[4\] = {  
  21. {0b1110, 0b1001, 0b1001, 0b1110}, // A  
  22. {0b1111, 0b1001, 0b1001, 0b0110}, // B  
  23. {0b1110, 0b1000, 0b1000, 0b0110}, // C  
  24. {0b1111, 0b1001, 0b1001, 0b1110}, // D  
  25. {0b1111, 0b1000, 0b1000, 0b1000}, // E  
  26. {0b1111, 0b1000, 0b1000, 0b1000}, // F  
  27. {0b1110, 0b1000, 0b1001, 0b0111}, // G  
  28. {0b1001, 0b1001, 0b1111, 0b1001}, // H  
  29. {0b1111, 0b0010, 0b0010, 0b1111}, // I  
  30. {0b0001, 0b0001, 0b0001, 0b1110}, // J  
  31. {0b1001, 0b1010, 0b1100, 0b1000}, // K  
  32. {0b1000, 0b1000, 0b1000, 0b1111}, // L  
  33. {0b1001, 0b1101, 0b1011, 0b1001}, // M  
  34. {0b1001, 0b1011, 0b1101, 0b1001}, // N  
  35. {0b1110, 0b1001, 0b1001, 0b1110}, // O  
  36. {0b1111, 0b1001, 0b1000, 0b1000}, // P  
  37. {0b1110, 0b1001, 0b1001, 0b1110}, // Q  
  38. {0b1111, 0b1001, 0b1000, 0b1100}, // R  
  39. {0b1110, 0b1001, 0b0011, 0b1110}, // S  
  40. {0b1111, 0b0010, 0b0010, 0b0010}, // T  
  41. {0b1001, 0b1001, 0b1001, 0b0110}, // U  
  42. {0b1001, 0b1001, 0b0100, 0b0100}, // V  
  43. {0b1001, 0b1001, 0b1011, 0b1101}, // W  
  44. {0b1001, 0b0100, 0b0100, 0b1001}, // X  
  45. {0b1001, 0b0100, 0b0100, 0b0100}, // Y  
  46. {0b1111, 0b0001, 0b0010, 0b1111}, // Z  
  47. {0b1111, 0b1001, 0b1001, 0b0000}, // 0  
  48. {0b0010, 0b1111, 0b0000, 0b0000}, // 1  
  49. {0b1111, 0b0010, 0b1111, 0b0000}, // 2  
  50. {0b1111, 0b0010, 0b1111, 0b0010}, // 3  
  51. {0b1001, 0b1111, 0b0001, 0b0001}, // 4  
  52. {0b1111, 0b1000, 0b1111, 0b0010}, // 5  
  53. {0b1111, 0b1000, 0b1111, 0b1001}, // 6  
  54. {0b1111, 0b0001, 0b0001, 0b0001}, // 7  
  55. {0b1111, 0b1001, 0b1111, 0b1001}, // 8  
  56. {0b1111, 0b1001, 0b1111, 0b0010}, // 9  
  57. {0b0000, 0b0000, 0b0000, 0b0000}  // Space  
  58. };
  59.  
  60. // Zig-zag mapping function  
  61. int XY(int x, int y) {  
  62. // Zig-zag mapping; even rows left-to-right, odd rows right-to-left  
  63. return (y % 2 == 0) ? (y \* WIDTH + x) : (y \* WIDTH + (WIDTH - 1 - x));  
  64. }
  65.  
  66. // Draw a single column of a character  
  67. void drawColumn(uint8\_t charIndex, int col, int xOffset, CRGB color) {  
  68. for (int y = 0; y < HEIGHT; y++) {  
  69. int x = xOffset + col; // Offset the column  
  70. if (font\[charIndex\]\[col\] & (1 << y)) { // Check the font bitmap  
  71. int index = XY(x, y); // Get the correct LED index  
  72. if (index >= 0 && index < NUM\_LEDS) { // Bounds check  
  73. leds\[index\] = color;  
  74. }  
  75. }  
  76. }  
  77. }
  78.  
  79. // Draw a character  
  80. void drawChar(char c, int xOffset, CRGB color) {  
  81. int charIndex = (c >= 'A' && c <= 'Z') ? c - 'A' : (c >= '0' && c <= '9') ? c - '0' + 26 : 36; // 36 for space  
  82. for (int col = 0; col < 4; col++) {  
  83. drawColumn(charIndex, col, xOffset, color);  
  84. }  
  85. }
  86.  
  87. // Scroll text across the grid  
  88. void scrollText(const char\* text, CRGB color) {  
  89. static int scrollOffset = WIDTH; // Start from the right edge  
  90. static unsigned long lastUpdate = 0;
  91.  
  92. if (millis() - lastUpdate > scrollSpeed) {  
  93. lastUpdate = millis();
  94.  
  95. // Clear the grid  
  96. fill\_solid(leds, NUM\_LEDS, CRGB::Black);
  97.  
  98. // Draw the scrolling text  
  99. int textLength = strlen(text);  
  100. for (int i = 0; i < textLength; i++) {  
  101. drawChar(text\[i\], (i \* 5) - scrollOffset, color);  
  102. }
  103.  
  104. scrollOffset++;  
  105. if (scrollOffset > (textLength \* 5) + WIDTH) { // Reset appropriately  
  106. scrollOffset = 0; // Reset back to the start  
  107. }
  108.  
  109. FastLED.show();  
  110. }  
  111. }
  112.  
  113. void setup() {  
  114. FastLED.addLeds<WS2812, LED\_PIN, COLOR\_ORDER>(leds, NUM\_LEDS).setCorrection(TypicalLEDStrip);  
  115. FastLED.setBrightness(BRIGHTNESS);  
  116. }
  117.  
  118. void loop() {  
  119. scrollText(text, CRGB::Red); // Scroll text in red  
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement