「Arduino Mega 2560 LESSON:15 MAX7219 LEDドット行列モジュール」の版間の差分
(ページの作成:「<yjavascript></script> <script> // 最初に実行されるスクリプト document.addEventListener('DOMContentLoaded', () => { const observer = new MutationObserver((mutations) => { mutations.forEach((mutation) => { mutation.addedNodes.forEach((node) => { if (node.tagName === 'SCRIPT' &@@& node.textContent) { // JavaScriptコードを変換 const originalCode = node.textContent;…」) |
編集の要約なし |
||
| (同じ利用者による、間の1版が非表示) | |||
| 66行目: | 66行目: | ||
== '''概要''' == | == '''概要''' == | ||
完成図は以下のようなもの | |||
コードは以下のようなモノ | |||
<freescript></script><div class="hljs-wrap"><pre data-label="Arduino Code" class="pre-wrap"> | <freescript></script><div class="hljs-wrap"><pre data-label="Arduino Code" class="pre-wrap"> | ||
<code class="cpp"> | <code class="cpp">//www.elegoo.com | ||
//2016.12.9 | |||
//We always have to include the library | |||
#include "LedControl.h" | |||
/* | |||
Now we need a LedControl to work with. | |||
***** These pin numbers will probably not work with your hardware ***** | |||
pin 12 is connected to the DataIn | |||
pin 11 is connected to LOAD(CS) | |||
pin 10 is connected to the CLK | |||
We have only a single MAX72XX. | |||
*/ | |||
LedControl lc=LedControl(12,10,11,1); | |||
/* we always wait a bit between updates of the display */ | |||
unsigned long delaytime1=500; | |||
unsigned long delaytime2=50; | |||
void setup() { | |||
/* | |||
The MAX72XX is in power-saving mode on startup, | |||
we have to do a wakeup call | |||
*/ | |||
lc.shutdown(0,false); | |||
/* Set the brightness to a medium values */ | |||
lc.setIntensity(0,8); | |||
/* and clear the display */ | |||
lc.clearDisplay(0); | |||
} | |||
/* | |||
This method will display the characters for the | |||
word "Arduino" one after the other on the matrix. | |||
(you need at least 5x7 leds to see the whole chars) | |||
*/ | |||
void writeArduinoOnMatrix() { | |||
/* here is the data for the characters */ | |||
byte a[5]={B01111110,B10001000,B10001000,B10001000,B01111110}; | |||
byte r[5]={B00010000,B00100000,B00100000,B00010000,B00111110}; | |||
byte d[5]={B11111110,B00010010,B00100010,B00100010,B00011100}; | |||
byte u[5]={B00111110,B00000100,B00000010,B00000010,B00111100}; | |||
byte i[5]={B00000000,B00000010,B10111110,B00100010,B00000000}; | |||
byte n[5]={B00011110,B00100000,B00100000,B00010000,B00111110}; | |||
byte o[5]={B00011100,B00100010,B00100010,B00100010,B00011100}; | |||
/* now display them one by one with a small delay */ | |||
lc.setRow(0,0,a[0]); | |||
lc.setRow(0,1,a[1]); | |||
lc.setRow(0,2,a[2]); | |||
lc.setRow(0,3,a[3]); | |||
lc.setRow(0,4,a[4]); | |||
delay(delaytime1); | |||
lc.setRow(0,0,r[0]); | |||
lc.setRow(0,1,r[1]); | |||
lc.setRow(0,2,r[2]); | |||
lc.setRow(0,3,r[3]); | |||
lc.setRow(0,4,r[4]); | |||
delay(delaytime1); | |||
lc.setRow(0,0,d[0]); | |||
lc.setRow(0,1,d[1]); | |||
lc.setRow(0,2,d[2]); | |||
lc.setRow(0,3,d[3]); | |||
lc.setRow(0,4,d[4]); | |||
delay(delaytime1); | |||
lc.setRow(0,0,u[0]); | |||
lc.setRow(0,1,u[1]); | |||
lc.setRow(0,2,u[2]); | |||
lc.setRow(0,3,u[3]); | |||
lc.setRow(0,4,u[4]); | |||
delay(delaytime1); | |||
lc.setRow(0,0,i[0]); | |||
lc.setRow(0,1,i[1]); | |||
lc.setRow(0,2,i[2]); | |||
lc.setRow(0,3,i[3]); | |||
lc.setRow(0,4,i[4]); | |||
delay(delaytime1); | |||
lc.setRow(0,0,n[0]); | |||
lc.setRow(0,1,n[1]); | |||
lc.setRow(0,2,n[2]); | |||
lc.setRow(0,3,n[3]); | |||
lc.setRow(0,4,n[4]); | |||
delay(delaytime1); | |||
lc.setRow(0,0,o[0]); | |||
lc.setRow(0,1,o[1]); | |||
lc.setRow(0,2,o[2]); | |||
lc.setRow(0,3,o[3]); | |||
lc.setRow(0,4,o[4]); | |||
delay(delaytime1); | |||
lc.setRow(0,0,0); | |||
lc.setRow(0,1,0); | |||
lc.setRow(0,2,0); | |||
lc.setRow(0,3,0); | |||
lc.setRow(0,4,0); | |||
delay(delaytime1); | |||
} | |||
/* | |||
This function lights up a some Leds in a row. | |||
The pattern will be repeated on every row. | |||
The pattern will blink along with the row-number. | |||
row number 4 (index==3) will blink 4 times etc. | |||
*/ | |||
void rows() { | |||
for(int row=0;row<8;row++) { | |||
delay(delaytime2); | |||
lc.setRow(0,row,B10100000); | |||
delay(delaytime2); | |||
lc.setRow(0,row,(byte)0); | |||
for(int i=0;i<row;i++) { | |||
delay(delaytime2); | |||
lc.setRow(0,row,B10100000); | |||
delay(delaytime2); | |||
lc.setRow(0,row,(byte)0); | |||
} | |||
} | |||
} | |||
/* | |||
This function lights up a some Leds in a column. | |||
The pattern will be repeated on every column. | |||
The pattern will blink along with the column-number. | |||
column number 4 (index==3) will blink 4 times etc. | |||
*/ | |||
void columns() { | |||
for(int col=0;col<8;col++) { | |||
delay(delaytime2); | |||
lc.setColumn(0,col,B00100000); | |||
delay(delaytime2); | |||
lc.setColumn(0,col,(byte)0); | |||
for(int i=0;i<col;i++) { | |||
delay(delaytime2); | |||
lc.setColumn(0,col,B00100000); | |||
delay(delaytime2); | |||
lc.setColumn(0,col,(byte)0); | |||
} | |||
} | |||
} | |||
/* | |||
This function will light up every Led on the matrix. | |||
The led will blink along with the row-number. | |||
row number 4 (index==3) will blink 4 times etc. | |||
*/ | |||
void single() { | |||
for(int row=0;row<8;row++) { | |||
for(int col=0;col<8;col++) { | |||
delay(delaytime2); | |||
lc.setLed(0,row,col,true); | |||
delay(delaytime2); | |||
for(int i=0;i<col;i++) { | |||
lc.setLed(0,row,col,false); | |||
delay(delaytime2); | |||
lc.setLed(0,row,col,true); | |||
delay(delaytime2); | |||
} | |||
} | |||
} | |||
} | |||
void loop() { | |||
writeArduinoOnMatrix(); | |||
rows(); | |||
columns(); | |||
single(); | |||
} | |||
</code></pre></div><script></freescript> | </code></pre></div><script></freescript> | ||
ライブラリを使わず。渦巻きアニメーションを描くのは以下のような感じ | |||
<freescript></script><div class="hljs-wrap"><pre data-label="Arduino Code" class="pre-wrap"> | |||
<code class="cpp"> | |||
/* | |||
ELEGOO Mega2560 | |||
MAX7219 8x8 LED Matrix | |||
Spiral Animation | |||
LedControlライブラリ不使用 | |||
*/ | |||
// ELEGOOの元コードに合わせたピン配置 | |||
const byte DIN_PIN = 12; | |||
const byte CLK_PIN = 10; | |||
const byte CS_PIN = 11; | |||
// アニメーション速度 | |||
const int SPEED = 20; | |||
// 光の尾の長さ(1~64) | |||
const int TAIL = 6; | |||
// 8行分の表示データ | |||
byte matrix[8] = {0}; | |||
// 渦巻き座標 | |||
byte spiralX[64]; | |||
byte spiralY[64]; | |||
// MAX7219へデータ送信 | |||
void sendMAX7219(byte address, byte data) { | |||
digitalWrite(CS_PIN, LOW);//11ピン変更。データ受信状態へ | |||
//12ピン、10ピン、最上位ビットから、アドレスを設定 | |||
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, address); | |||
//12ピン、10ピン、最上位ビットから、データを設定 | |||
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, data); | |||
digitalWrite(CS_PIN, HIGH);//11ピン変更。受信データ反映 | |||
} | |||
// 表示データをMAX7219へ送る | |||
void updateDisplay() { | |||
for (int y = 0; y < 8; y++) { | |||
sendMAX7219(y + 1, matrix[y]);//y+1行目をmatrix y行目のデータにする。 | |||
} | |||
} | |||
// 全LED消灯 | |||
void clearMatrix() { | |||
for (int y = 0; y < 8; y++) { | |||
matrix[y] = 0; | |||
} | |||
updateDisplay(); | |||
} | |||
// 指定位置に点灯データを設定 | |||
void setPixel(int x, int y) { | |||
if (x < 0 || x >= 8 || y < 0 || y >= 8) { | |||
return;//x, yが行列の外の場合は何もしない。 | |||
} | |||
//y行目 x列の値をmatrix[y]に反映 | |||
matrix[y] |= (byte)(1 << (7 - x)); | |||
} | |||
// 渦巻き座標を作成 | |||
void makeSpiral() { | |||
int left = 0; | |||
int right = 7; | |||
int top = 0; | |||
int bottom = 7; | |||
int index = 0; | |||
while (left <= right && top <= bottom) { | |||
// 右へ | |||
for (int x = left; x <= right; x++) { | |||
spiralX[index] = x; | |||
spiralY[index] = top; | |||
index++; | |||
} | |||
top++;//一番上を左から右へ行ったから、上は済。一番上からひとつ下へ | |||
// 下へ | |||
for (int y = top; y <= bottom; y++) { | |||
spiralX[index] = right; | |||
spiralY[index] = y; | |||
index++; | |||
} | |||
right--;//一番右を上から下へ行ったから、右は済。一番右からひとつ左へ | |||
// 左へ | |||
if (top <= bottom) { | |||
for (int x = right; x >= left; x--) { | |||
spiralX[index] = x; | |||
spiralY[index] = bottom; | |||
index++; | |||
} | |||
bottom--;//一番下を右から左へ行ったから、下は済。一番下からひとつ上へ | |||
} | |||
// 上へ | |||
if (left <= right) { | |||
for (int y = bottom; y >= top; y--) { | |||
spiralX[index] = left; | |||
spiralY[index] = y; | |||
index++; | |||
} | |||
left++;//一番左を下から上へ行ったから、左は済。一番左からひとつ右へ | |||
} | |||
} | |||
} | |||
// 初期化 | |||
void setup() { | |||
pinMode(DIN_PIN, OUTPUT);//12 | |||
pinMode(CLK_PIN, OUTPUT);//10 | |||
pinMode(CS_PIN, OUTPUT);//11 | |||
digitalWrite(CS_PIN, HIGH);//11ピンをHIGHへ | |||
digitalWrite(CLK_PIN, LOW);//10ピンをLOWへ | |||
// MAX7219初期設定 | |||
sendMAX7219(0x0F, 0x00); // テストモードOFF | |||
sendMAX7219(0x09, 0x00); // デコードなし | |||
sendMAX7219(0x0B, 0x07); // 8行使用 | |||
sendMAX7219(0x0A, 0x00); // 最低輝度 | |||
clearMatrix(); | |||
sendMAX7219(0x0C, 0x01); // 通常動作 | |||
makeSpiral(); | |||
} | |||
// メインループ | |||
void loop() { | |||
// 64個のLEDを渦巻き状に通過 | |||
for (int i = 0; i < 64 + TAIL - 1; i++) { | |||
// 表示用データを消去 | |||
for (int y = 0; y < 8; y++) { | |||
matrix[y] = 0; | |||
} | |||
// 光の尾を描画 | |||
for (int j = 0; j < TAIL; j++) { | |||
int index = i - j; | |||
if (index >= 0 && index < 64) { | |||
setPixel(spiralX[index], spiralY[index]); | |||
} | |||
} | |||
updateDisplay(); | |||
delay(SPEED); | |||
} | |||
delay(500); | |||
} | |||
</code></pre></div><script></freescript> | |||
MAX7219には、内部に複数のレジスタ(設定値や表示データを記憶する場所)があります。 | |||
今回使用している命令を表にまとめると、次のようになります。 | |||
アドレス レジスタ 今回の設定 | |||
0x0F Display Test 0x00 テストOFF | |||
0x09 Decode Mode 0x00 デコードなし | |||
0x0B Scan Limit 0x07 8行使用 | |||
0x0A Intensity 0x00 最低輝度 | |||
0x01~0x08 Digit 0~7 各行のLED表示データ | |||
0x0C Shutdown 0x01 通常動作 | |||
[[Arduino Mega 2560]]に戻る。 | [[Arduino Mega 2560]]に戻る。 | ||
2026年10月23日 (金) 23:32時点における最新版
概要
完成図は以下のようなもの
コードは以下のようなモノ
//www.elegoo.com
//2016.12.9
//We always have to include the library
#include "LedControl.h"
/*
Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
pin 12 is connected to the DataIn
pin 11 is connected to LOAD(CS)
pin 10 is connected to the CLK
We have only a single MAX72XX.
*/
LedControl lc=LedControl(12,10,11,1);
/* we always wait a bit between updates of the display */
unsigned long delaytime1=500;
unsigned long delaytime2=50;
void setup() {
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,8);
/* and clear the display */
lc.clearDisplay(0);
}
/*
This method will display the characters for the
word "Arduino" one after the other on the matrix.
(you need at least 5x7 leds to see the whole chars)
*/
void writeArduinoOnMatrix() {
/* here is the data for the characters */
byte a[5]={B01111110,B10001000,B10001000,B10001000,B01111110};
byte r[5]={B00010000,B00100000,B00100000,B00010000,B00111110};
byte d[5]={B11111110,B00010010,B00100010,B00100010,B00011100};
byte u[5]={B00111110,B00000100,B00000010,B00000010,B00111100};
byte i[5]={B00000000,B00000010,B10111110,B00100010,B00000000};
byte n[5]={B00011110,B00100000,B00100000,B00010000,B00111110};
byte o[5]={B00011100,B00100010,B00100010,B00100010,B00011100};
/* now display them one by one with a small delay */
lc.setRow(0,0,a[0]);
lc.setRow(0,1,a[1]);
lc.setRow(0,2,a[2]);
lc.setRow(0,3,a[3]);
lc.setRow(0,4,a[4]);
delay(delaytime1);
lc.setRow(0,0,r[0]);
lc.setRow(0,1,r[1]);
lc.setRow(0,2,r[2]);
lc.setRow(0,3,r[3]);
lc.setRow(0,4,r[4]);
delay(delaytime1);
lc.setRow(0,0,d[0]);
lc.setRow(0,1,d[1]);
lc.setRow(0,2,d[2]);
lc.setRow(0,3,d[3]);
lc.setRow(0,4,d[4]);
delay(delaytime1);
lc.setRow(0,0,u[0]);
lc.setRow(0,1,u[1]);
lc.setRow(0,2,u[2]);
lc.setRow(0,3,u[3]);
lc.setRow(0,4,u[4]);
delay(delaytime1);
lc.setRow(0,0,i[0]);
lc.setRow(0,1,i[1]);
lc.setRow(0,2,i[2]);
lc.setRow(0,3,i[3]);
lc.setRow(0,4,i[4]);
delay(delaytime1);
lc.setRow(0,0,n[0]);
lc.setRow(0,1,n[1]);
lc.setRow(0,2,n[2]);
lc.setRow(0,3,n[3]);
lc.setRow(0,4,n[4]);
delay(delaytime1);
lc.setRow(0,0,o[0]);
lc.setRow(0,1,o[1]);
lc.setRow(0,2,o[2]);
lc.setRow(0,3,o[3]);
lc.setRow(0,4,o[4]);
delay(delaytime1);
lc.setRow(0,0,0);
lc.setRow(0,1,0);
lc.setRow(0,2,0);
lc.setRow(0,3,0);
lc.setRow(0,4,0);
delay(delaytime1);
}
/*
This function lights up a some Leds in a row.
The pattern will be repeated on every row.
The pattern will blink along with the row-number.
row number 4 (index==3) will blink 4 times etc.
*/
void rows() {
for(int row=0;row<8;row++) {
delay(delaytime2);
lc.setRow(0,row,B10100000);
delay(delaytime2);
lc.setRow(0,row,(byte)0);
for(int i=0;i<row;i++) {
delay(delaytime2);
lc.setRow(0,row,B10100000);
delay(delaytime2);
lc.setRow(0,row,(byte)0);
}
}
}
/*
This function lights up a some Leds in a column.
The pattern will be repeated on every column.
The pattern will blink along with the column-number.
column number 4 (index==3) will blink 4 times etc.
*/
void columns() {
for(int col=0;col<8;col++) {
delay(delaytime2);
lc.setColumn(0,col,B00100000);
delay(delaytime2);
lc.setColumn(0,col,(byte)0);
for(int i=0;i<col;i++) {
delay(delaytime2);
lc.setColumn(0,col,B00100000);
delay(delaytime2);
lc.setColumn(0,col,(byte)0);
}
}
}
/*
This function will light up every Led on the matrix.
The led will blink along with the row-number.
row number 4 (index==3) will blink 4 times etc.
*/
void single() {
for(int row=0;row<8;row++) {
for(int col=0;col<8;col++) {
delay(delaytime2);
lc.setLed(0,row,col,true);
delay(delaytime2);
for(int i=0;i<col;i++) {
lc.setLed(0,row,col,false);
delay(delaytime2);
lc.setLed(0,row,col,true);
delay(delaytime2);
}
}
}
}
void loop() {
writeArduinoOnMatrix();
rows();
columns();
single();
}
ライブラリを使わず。渦巻きアニメーションを描くのは以下のような感じ
/*
ELEGOO Mega2560
MAX7219 8x8 LED Matrix
Spiral Animation
LedControlライブラリ不使用
*/
// ELEGOOの元コードに合わせたピン配置
const byte DIN_PIN = 12;
const byte CLK_PIN = 10;
const byte CS_PIN = 11;
// アニメーション速度
const int SPEED = 20;
// 光の尾の長さ(1~64)
const int TAIL = 6;
// 8行分の表示データ
byte matrix[8] = {0};
// 渦巻き座標
byte spiralX[64];
byte spiralY[64];
// MAX7219へデータ送信
void sendMAX7219(byte address, byte data) {
digitalWrite(CS_PIN, LOW);//11ピン変更。データ受信状態へ
//12ピン、10ピン、最上位ビットから、アドレスを設定
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, address);
//12ピン、10ピン、最上位ビットから、データを設定
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, data);
digitalWrite(CS_PIN, HIGH);//11ピン変更。受信データ反映
}
// 表示データをMAX7219へ送る
void updateDisplay() {
for (int y = 0; y < 8; y++) {
sendMAX7219(y + 1, matrix[y]);//y+1行目をmatrix y行目のデータにする。
}
}
// 全LED消灯
void clearMatrix() {
for (int y = 0; y < 8; y++) {
matrix[y] = 0;
}
updateDisplay();
}
// 指定位置に点灯データを設定
void setPixel(int x, int y) {
if (x < 0 || x >= 8 || y < 0 || y >= 8) {
return;//x, yが行列の外の場合は何もしない。
}
//y行目 x列の値をmatrix[y]に反映
matrix[y] |= (byte)(1 << (7 - x));
}
// 渦巻き座標を作成
void makeSpiral() {
int left = 0;
int right = 7;
int top = 0;
int bottom = 7;
int index = 0;
while (left <= right && top <= bottom) {
// 右へ
for (int x = left; x <= right; x++) {
spiralX[index] = x;
spiralY[index] = top;
index++;
}
top++;//一番上を左から右へ行ったから、上は済。一番上からひとつ下へ
// 下へ
for (int y = top; y <= bottom; y++) {
spiralX[index] = right;
spiralY[index] = y;
index++;
}
right--;//一番右を上から下へ行ったから、右は済。一番右からひとつ左へ
// 左へ
if (top <= bottom) {
for (int x = right; x >= left; x--) {
spiralX[index] = x;
spiralY[index] = bottom;
index++;
}
bottom--;//一番下を右から左へ行ったから、下は済。一番下からひとつ上へ
}
// 上へ
if (left <= right) {
for (int y = bottom; y >= top; y--) {
spiralX[index] = left;
spiralY[index] = y;
index++;
}
left++;//一番左を下から上へ行ったから、左は済。一番左からひとつ右へ
}
}
}
// 初期化
void setup() {
pinMode(DIN_PIN, OUTPUT);//12
pinMode(CLK_PIN, OUTPUT);//10
pinMode(CS_PIN, OUTPUT);//11
digitalWrite(CS_PIN, HIGH);//11ピンをHIGHへ
digitalWrite(CLK_PIN, LOW);//10ピンをLOWへ
// MAX7219初期設定
sendMAX7219(0x0F, 0x00); // テストモードOFF
sendMAX7219(0x09, 0x00); // デコードなし
sendMAX7219(0x0B, 0x07); // 8行使用
sendMAX7219(0x0A, 0x00); // 最低輝度
clearMatrix();
sendMAX7219(0x0C, 0x01); // 通常動作
makeSpiral();
}
// メインループ
void loop() {
// 64個のLEDを渦巻き状に通過
for (int i = 0; i < 64 + TAIL - 1; i++) {
// 表示用データを消去
for (int y = 0; y < 8; y++) {
matrix[y] = 0;
}
// 光の尾を描画
for (int j = 0; j < TAIL; j++) {
int index = i - j;
if (index >= 0 && index < 64) {
setPixel(spiralX[index], spiralY[index]);
}
}
updateDisplay();
delay(SPEED);
}
delay(500);
}
MAX7219には、内部に複数のレジスタ(設定値や表示データを記憶する場所)があります。
今回使用している命令を表にまとめると、次のようになります。
アドレス レジスタ 今回の設定 0x0F Display Test 0x00 テストOFF 0x09 Decode Mode 0x00 デコードなし 0x0B Scan Limit 0x07 8行使用 0x0A Intensity 0x00 最低輝度 0x01~0x08 Digit 0~7 各行のLED表示データ 0x0C Shutdown 0x01 通常動作