請參考家豪教學網
矩陣 LED 跑馬燈
範例一:
寫一程式在 8x8 led 上顯示阿拉伯數字 0 至 9。
電路圖:


程式碼:
const char N = 8;
const int led[2][N]={{19,18,5,17,16,4,2,15},{13,12,14,27,26,25,33,32}};
const char num[10][N*N]={
 {1,1,1,0,0,0,1,1,//0
  1,1,0,1,1,0,1,1,
  1,1,0,1,1,1,0,1,
  1,0,1,1,1,1,0,1,
  1,0,1,1,1,1,0,1,
  1,0,1,1,1,0,1,1,
  1,0,1,1,1,0,1,1,
  1,1,0,0,0,1,1,1},
 {1,1,1,1,0,1,1,1,//1
  1,1,1,0,0,1,1,1,
  1,1,1,1,0,1,1,1,
  1,1,1,1,0,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,1,0,1,1,1,1},
 {1,1,0,0,0,0,0,1,//2
  1,0,1,1,1,1,1,0,
  1,1,1,1,1,1,0,1,
  1,1,1,1,1,0,1,1,
  1,1,1,1,0,1,1,1,
  1,1,0,0,1,1,1,1,
  1,0,1,1,1,1,1,1,
  0,0,0,0,0,0,1,1},
 {1,1,1,0,0,0,0,1,//3
  1,1,0,1,1,1,1,0,
  1,1,1,1,1,0,0,1,
  1,1,1,1,0,1,1,1,
  1,1,0,0,0,0,1,1,
  1,1,1,1,1,0,1,1,
  1,1,1,1,0,1,1,1,
  0,0,0,0,1,1,1,1},
 {1,1,1,1,0,0,0,1,//4
  1,1,1,0,1,1,0,1,
  1,1,0,1,1,0,1,1,
  1,0,1,1,1,0,1,1,
  0,1,1,1,0,1,1,1,
  0,0,0,0,0,0,0,1,
  1,1,1,0,1,1,1,1,
  1,1,0,1,1,1,1,1},
 {1,1,1,0,0,0,0,0,//5
  1,1,1,0,1,1,1,1,
  1,1,0,1,1,1,1,1,
  1,1,0,0,0,0,0,1,
  1,1,1,1,1,1,0,1,
  1,1,1,1,1,0,1,1,
  1,1,1,1,0,1,1,1,
  0,0,0,0,1,1,1,1},
 {1,1,1,1,1,0,0,1,//6
  1,1,1,0,0,1,1,0,
  1,1,0,1,1,1,1,1,
  1,1,0,0,0,1,1,1,
  1,0,1,1,1,0,1,1,
  1,0,1,1,1,0,1,1,
  1,0,1,1,1,0,1,1,
  1,1,0,0,0,1,1,1},
 {1,1,0,0,0,0,0,1,//7
  1,1,1,1,1,1,0,1,
  1,1,1,1,1,0,1,1,
  1,1,1,1,0,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,0,1,1,1,1,1,
  1,1,0,1,1,1,1,1},
 {1,1,0,0,0,0,0,1,//8
  1,0,1,1,1,1,1,0,
  1,0,1,1,1,1,0,1,
  1,1,0,0,0,0,1,1,
  1,0,1,1,1,1,0,1,
  0,1,1,1,1,1,0,1,
  0,1,1,1,1,1,0,1,
  1,0,0,0,0,0,1,1},
 {1,1,1,1,0,0,0,1,//9
  1,1,0,0,1,1,0,1,
  1,0,1,1,1,1,0,1,
  1,0,1,1,1,0,1,1,
  1,0,0,0,0,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,0,1,1,1,1,1,
  1,1,0,1,1,1,1,1}
};

char buff[N*N];
char number=0;
int Len;//整個表格長度 單位( 8 )
hw_timer_t * ClockTimer = NULL;//宣告 timer 物件
void show(){
    int i,j;
    for(i=0;i<N;i++) digitalWrite(led[0][i], LOW);
    for(i=0;i<N;i++){
      digitalWrite(led[0][i], HIGH);
      if(i>0)
        digitalWrite(led[0][i-1], LOW);
      for(j=0;j<N;j++)
        digitalWrite(led[1][j], buff[i*N+j]);
      delay(1);
    }
}
void IRAM_ATTR moveBuf(){//0.3秒觸發一次
  int i;
  for(i=0;i<N*N;i++){
      buff[i]=num[number][i];
  }
  number++;
  number%=10;
}
void begin(int n){
  Len = n*8;
}
void setup() {
  Serial.begin(115200);
  for(int i=0;i<2;i++)
    for(int j=0;j<8;j++)
      pinMode(led[i][j], OUTPUT);
  ClockTimer = timerBegin(0, 80, true);
  timerAttachInterrupt(ClockTimer, &moveBuf, true);//timer 物件觸發後執行 onTimer 程式
  timerAlarmWrite(ClockTimer, 1000000, true);//0.3 秒後觸發 (300000 微秒 = 0.3 秒)
  timerAlarmEnable(ClockTimer);
  Serial.println(sizeof(num));
  moveBuf();
}

void loop() {
  show();
}

另一種編碼:
const char num[10][N*N]={
 {1,1,1,0,0,0,1,1,//0
  1,1,0,1,1,0,1,1,
  1,1,0,1,1,1,0,1,
  1,0,1,1,1,1,0,1,
  1,0,1,1,1,1,0,1,
  1,0,1,1,1,0,1,1,
  1,0,1,1,1,0,1,1,
  1,1,0,0,0,1,1,1},
 {1,1,1,1,0,1,1,1,//1
  1,1,1,0,0,1,1,1,
  1,1,1,1,0,1,1,1,
  1,1,1,1,0,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,1,0,1,1,1,1},
 {1,1,0,0,0,0,0,1,//2
  1,0,1,1,1,1,1,0,
  1,1,1,1,1,1,0,1,
  1,1,1,1,1,0,1,1,
  1,1,1,1,0,1,1,1,
  1,1,0,0,1,1,1,1,
  1,0,1,1,1,1,1,1,
  0,0,0,0,0,0,1,1},
 {1,1,1,0,0,0,0,1,//3
  1,1,0,1,1,1,1,0,
  1,1,1,1,1,0,0,1,
  1,1,1,1,0,1,1,1,
  1,1,0,0,0,0,1,1,
  1,1,1,1,1,0,1,1,
  1,1,1,1,0,1,1,1,
  0,0,0,0,1,1,1,1},
 {1,1,1,1,0,0,0,1,//4
  1,1,1,0,1,1,0,1,
  1,1,0,1,1,0,1,1,
  1,0,1,1,1,0,1,1,
  0,1,1,1,0,1,1,1,
  0,0,0,0,0,0,0,1,
  1,1,1,0,1,1,1,1,
  1,1,0,1,1,1,1,1},
 {1,1,1,0,0,0,0,0,//5
  1,1,1,0,1,1,1,1,
  1,1,0,1,1,1,1,1,
  1,1,0,0,0,0,0,1,
  1,1,1,1,1,1,0,1,
  1,1,1,1,1,0,1,1,
  1,1,1,1,0,1,1,1,
  0,0,0,0,1,1,1,1},
 {1,1,1,1,1,0,0,1,//6
  1,1,1,0,0,1,1,0,
  1,1,0,1,1,1,1,1,
  1,1,0,0,0,1,1,1,
  1,0,1,1,1,0,1,1,
  1,0,1,1,1,0,1,1,
  1,0,1,1,1,0,1,1,
  1,1,0,0,0,1,1,1},
 {1,1,0,0,0,0,0,1,//7
  1,1,1,1,1,1,0,1,
  1,1,1,1,1,0,1,1,
  1,1,1,1,0,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,0,1,1,1,1,1,
  1,1,0,1,1,1,1,1},
 {1,1,0,0,0,0,0,1,//8
  1,0,1,1,1,1,1,0,
  1,0,1,1,1,1,0,1,
  1,1,0,0,0,0,1,1,
  1,0,1,1,1,1,0,1,
  0,1,1,1,1,1,0,1,
  0,1,1,1,1,1,0,1,
  1,0,0,0,0,0,1,1},
 {1,1,1,1,0,0,0,1,//9
  1,1,0,0,1,1,0,1,
  1,0,1,1,1,1,0,1,
  1,0,1,1,1,0,1,1,
  1,0,0,0,0,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,0,1,1,1,1,1,
  1,1,0,1,1,1,1,1}
};


範例:
寫一程式讓數字 0~9 往上捲動。
(電路圖同上)
程式碼:
const char N = 8;
const int led[2][N]={{19,18,5,17,16,4,2,15},{13,12,14,27,26,25,33,32}};
const char num[11*N*N]={
  1,1,1,0,0,0,1,1,//0
  1,1,0,1,1,0,1,1,
  1,1,0,1,1,1,0,1,
  1,0,1,1,1,1,0,1,
  1,0,1,1,1,1,0,1,
  1,0,1,1,1,0,1,1,
  1,0,1,1,1,0,1,1,
  1,1,0,0,0,1,1,1,
  1,1,1,1,0,1,1,1,//1
  1,1,1,0,0,1,1,1,
  1,1,1,1,0,1,1,1,
  1,1,1,1,0,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,0,0,0,0,0,1,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
  1,1,0,0,1,1,0,1,
  1,0,1,1,1,1,0,1,
  1,0,1,1,1,0,1,1,
  1,0,0,0,0,1,1,1,
  1,1,1,0,1,1,1,1,
  1,1,0,1,1,1,1,1,
  1,1,0,1,1,1,1,1,
  1,1,1,0,0,0,1,1,//0
  1,1,0,1,1,0,1,1,
  1,1,0,1,1,1,0,1,
  1,0,1,1,1,1,0,1,
  1,0,1,1,1,1,0,1,
  1,0,1,1,1,0,1,1,
  1,0,1,1,1,0,1,1,
  1,1,0,0,0,1,1,1
};
char buff[N*N];
int number;
int Len;//整個表格長度 單位( 8 )
hw_timer_t *ClockTimer = NULL;//宣告 timer 物件

void show(){
    int i,j;
    for(i=0;i<N;i++) digitalWrite(led[0][i], LOW);
    for(i=0;i<N;i++){
      digitalWrite(led[0][i], HIGH);
      if(i>0)
        digitalWrite(led[0][i-1], LOW);
      for(j=0;j<N;j++)
        digitalWrite(led[1][j], buff[i*N+j]);
      delay(1);
    }
}
void nextMove(int c){
  switch(c){
    case 0://每次跳一個數字
      number+=Len;
      break;
    case 1://向上捲動
      number+=N;
      break;
  }
  number%=10*Len;
}
void IRAM_ATTR moveBuf(){//0.3秒觸發一次
  int i;
  for(i=0;i<Len;i++){
      buff[i]=num[number+i];
  }
  nextMove(1);
}
void begin(){
  number = 0;
  Len = N*N;
}
void setup() {
  Serial.begin(115200);
  begin();
  for(int i=0;i<2;i++)
    for(int j=0;j<8;j++)
      pinMode(led[i][j], OUTPUT);
  ClockTimer = timerBegin(0, 80, true);
  timerAttachInterrupt(ClockTimer, &moveBuf, true);//timer 物件觸發後執行 onTimer 程式
  timerAlarmWrite(ClockTimer, 200000, true);//0.2 秒後觸發 (300000 微秒 = 0.2 秒)
  timerAlarmEnable(ClockTimer);
  Serial.print("Length=");
  Serial.println(sizeof(num));
  moveBuf();
}

void loop() {
  show();
}