#include <LedControl.h>
#define MAX7219_NUM 4 // 点阵屏数量
const int MAX7219_DIN_PIN = 12; // 数据引脚
const int MAX7219_CS_PIN = 11; // 片选脚
const int MAX7219_CLK_PIN = 10; // 时钟引脚
LedControl max7219(MAX7219_DIN_PIN, MAX7219_CLK_PIN, MAX7219_CS_PIN, MAX7219_NUM); // lc是建立的对象
// 定义不同屏幕的动画图案
byte Frame1[8] = { 0x00,0x18,0x3c,0x1e,0x1e,0x3c,0x18,0x00 }; // 屏幕2的动画帧
byte Frame2[8] = { 0x38, 0x7c, 0x7e, 0x3f, 0x3f, 0x7e, 0x7c, 0x38 }; // 屏幕2的动画帧
void setup() {
Serial.begin(9600);
// 初始化所有点阵屏幕
for (int i = 0; i < MAX7219_NUM; i++) {
max7219.shutdown(i, false); // 启动屏幕
max7219.setIntensity(i, 8); // 设置亮度
max7219.clearDisplay(i); // 清除屏幕
}
}
void loop() {
// 每块屏幕分别播放不同动画
byte *screen1Frames[] = { Frame1, Frame2 };
playAnimationOnScreen(0, screen1Frames, 2, 200);
playAnimationOnScreen(1, screen1Frames, 2, 300);
playAnimationOnScreen(2, screen1Frames, 2, 400);
playAnimationOnScreen(3, screen1Frames, 2, 500);
}
// 点阵显示函数
void printByte(byte character[], int screen) {
for (int i = 0; i < 8; i++)
max7219.setRow(screen, i, character[i]);
}
// 屏幕动态动画函数
void playAnimationOnScreen(int screen, byte *frames[], int frameCount, int frameDelay) {
static int currentFrame[4] = { 0, 0, 0, 0 }; // 每块屏幕的帧索引
static unsigned long lastUpdate[4] = { 0, 0, 0, 0 }; // 每块屏幕的更新时间
if (millis() - lastUpdate[screen] >= frameDelay) {
lastUpdate[screen] = millis();
printByte(frames[currentFrame[screen]], screen); // 显示当前帧
currentFrame[screen] = (currentFrame[screen] + 1) % frameCount; // 切换到下一帧
}
}