Skip to content

Instantly share code, notes, and snippets.

@roboter
Created December 9, 2024 18:52
Show Gist options
  • Save roboter/31d25b61edf7bf749b0a018d106c07c5 to your computer and use it in GitHub Desktop.
Save roboter/31d25b61edf7bf749b0a018d106c07c5 to your computer and use it in GitHub Desktop.
#include <FastLED.h>
#include "font.h"
#define DATA_PIN1 6 // Data pin for matrix 1
#define DATA_PIN2 8 // Data pin for matrix 2
#define DATA_PIN3 10 // Data pin for matrix 3
#define MATRIX_WIDTH 8 // Width of the 8x8 matrix
#define MATRIX_HEIGHT 8 // Height of the 8x8 matrix
#define NUM_LEDS MATRIX_WIDTH * MATRIX_HEIGHT // Total number of LEDs in one matrix
#define BRIGHTNESS 5 // LED brightness (0-255)
// LED arrays for each matrix
CRGB matrix1[NUM_LEDS];
CRGB matrix2[NUM_LEDS];
CRGB matrix3[NUM_LEDS];
uint8_t rotated_font[10][8] = {0};
// Function to display a digit on a given matrix
void displayDigit(CRGB* matrix, int digit) {
if (digit < 0 || digit > 9) return; // Ensure the digit is valid (0-9)
const uint8_t* font = rotated_font[digit]; // Get the font for the digit
// Loop through each row of the 8x8 matrix
for (int row = 0; row < MATRIX_HEIGHT; row++) {
for (int col = 0; col < MATRIX_WIDTH; col++) {
// Check if the pixel should be ON (1) or OFF (0)
if (font[row] & (1 << (7 - col))) {
matrix[row * MATRIX_WIDTH + col] = CRGB::Red; // Set to green
} else {
matrix[row * MATRIX_WIDTH + col] = CRGB::Black; // Turn off
}
}
}
FastLED.show(); // Update the matrix
}
void rotateFont(const uint8_t inputFont[10][8], uint8_t outputFont[10][8]) {
for (int digit = 0; digit < 10; digit++) {
for (int row = 0; row < 8; row++) {
uint8_t rotatedRow = 0;
for (int col = 0; col < 8; col++) {
// Extract bit from inputFont[digit][7 - col] (rotate clockwise)
uint8_t bit = (inputFont[digit][7 - col] >> row) & 0x01;
rotatedRow |= (bit << col);
}
outputFont[digit][row] = rotatedRow;
}
}
}
// Function to display a 3-digit number on the matrices
void displayNumber(int number) {
if (number < 0 || number > 999) return; // Ensure the number is valid (0-999)
int digits[3];
digits[0] = number / 100; // Hundreds place
digits[1] = (number / 10) % 10; // Tens place
digits[2] = number % 10; // Units place
// Display digits on respective matrices
displayDigit(matrix1, digits[0]); // Hundreds digit on the first matrix
displayDigit(matrix2, digits[1]); // Tens digit on the second matrix
displayDigit(matrix3, digits[2]); // Units digit on the third matrix
}
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN1>(matrix1, NUM_LEDS);
FastLED.addLeds<WS2812, DATA_PIN2, GRB>(matrix2, NUM_LEDS);
FastLED.addLeds<WS2812, DATA_PIN3, GRB>(matrix3, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear(); // Turn off all LEDs initially
// Rotate the font
rotateFont(basic_font, rotated_font);
}
void loop() {
// Example: Display numbers 0-999 in sequence
for (int i = 0; i <= 999; i++) {
displayNumber(i);
delay(1000); // Wait for 1 second
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment