#include <SPI.h>
#define CLK 52
#define DIN 51
#define CS 53
#define X_SEGMENTS 4
#define Y_SEGMENTS 1 // Adjusted to 1 because we're using a single row of 4 segments
#define NUM_SEGMENTS (X_SEGMENTS * Y_SEGMENTS)
// A framebuffer to hold the state of the entire matrix of LEDs
byte fb[8 * NUM_SEGMENTS];
const byte alphabet[5][8] = {
{0b01111110, 0b10001001, 0b10001001, 0b10001001, 0b10001001, 0b01111110, 0b00000000, 0b00000000}, // H
{0b00000000, 0b00000000, 0b00000000, 0b11111111, 0b00000000, 0b00000000, 0b00000000, 0b00000000}, // E
{0b01111100, 0b10000010, 0b10000001, 0b10000001, 0b10000001, 0b10000010, 0b01111100, 0b00000000}, // L
{0b01111100, 0b10000010, 0b10000001, 0b10000001, 0b10000001, 0b10000010, 0b01111100, 0b00000000}, // L
{0b01111100, 0b10000010, 0b10000001, 0b10000001, 0b10000001, 0b10000010, 0b01111100, 0b00000000} // O
};
void shiftAll(byte send_to_address, byte send_this_data) {
digitalWrite(CS, LOW);
for (int i = 0; i < NUM_SEGMENTS; i++) {
SPI.transfer(send_to_address);
SPI.transfer(send_this_data);
}
digitalWrite(CS, HIGH);
}
void setup() {
Serial.begin(115200);
pinMode(CLK, OUTPUT);
pinMode(DIN, OUTPUT);
pinMode(CS, OUTPUT);
SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE0));
// Setup each MAX7219
shiftAll(0x0f, 0x00); //display test register - test mode off
shiftAll(0x0b, 0x07); //scan limit register - display digits 0 thru 7
shiftAll(0x0c, 0x01); //shutdown register - normal operation
shiftAll(0x0a, 0x0f); //intensity register - max brightness
shiftAll(0x09, 0x00); //decode mode register - No decode
// Clear the framebuffer
memset(fb, 0, sizeof(fb));
// Load "HELLO" into the framebuffer
displayText("HELLO");
show(); // Display the initial text
}
void displayText(const char* text) {
int length = strlen(text);
for (int i = 0; i < length; i++) {
char c = text[i];
int index = c - 'H'; // Index based on the ASCII value of 'H'
if (index >= 0 && index < 5) { // 'H', 'E', 'L', 'L', 'O' are the first 5 letters in our array
for (int row = 0; row < 8; row++) {
fb[i * 8 + row] = alphabet[index][row];
}
}
}
}
void loop() {
// Add scrolling or other dynamic effects here, if desired.
}
// Send the framebuffer data to the display
void show() {
for (byte row = 0; row < 8; row++) {
digitalWrite(CS, LOW);
byte segment = NUM_SEGMENTS;
while (segment--) {
byte x = segment % X_SEGMENTS;
byte addr = row * X_SEGMENTS;
SPI.transfer(row + 1); // Send the row number
SPI.transfer(fb[addr + x]); // Send the corresponding data
}
digitalWrite(CS, HIGH);
}
}