【雕爷学编程】Arduino动手做(22)——8X8 LED点阵MAX7219屏8

发布时间 2023-07-19 09:19:29作者: 行者花雕

37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手试试做实验,不管成功与否,都会记录下来---小小的进步或是搞不定的问题,希望能够抛砖引玉。

 

【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
实验二十二:MAX7219点阵显示模块(8X8 LED共阴屏幕)

 

 

程序十一:循环显示m,i,r,t,o,e,k还有笑脸、爱心和四个方向的箭头
Arduino参考开源代码

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序十一:循环显示m,i,r,t,o,e,k还有笑脸、爱心和四个方向的箭头
  引脚定义
  DIN = D11
  CLK = D13
  CS  = D10
*/

#include <LedControl.h>//导入驱动库

int DIN = 11;//定义引脚
int CS =  10;
int CLK = 13;

byte m[8] =     {0xE7, 0xFF, 0xFF, 0xDB, 0xDB, 0xDB, 0xC3, 0xC3}; //英文字母m
byte i[8] =     {0x00, 0x10, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10}; //英文字母i
byte r[8] =     {0x00, 0x00, 0x2c, 0x32, 0x20, 0x20, 0x20, 0x20}; //英文字母r
byte o[8] =     {0x00, 0x00, 0x1c, 0x22, 0x22, 0x22, 0x22, 0x1c}; //英文字母o
byte t[8] =     {0x00, 0x00, 0x10, 0x7e, 0x10, 0x10, 0x14, 0x18}; //英文字母t
byte e[8] =     {0x00, 0x3c, 0x42, 0x42, 0x7e, 0x40, 0x40, 0x3e}; //英文字母e
byte k[8] =     {0x00, 0x20, 0x20, 0x24, 0x28, 0x30, 0x28, 0x24}; //英文字母k

byte smile[8] =   {0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C}; //微笑
byte love_1[8] =   {0x00, 0x66, 0x99, 0x81, 0x42, 0x24, 0x18, 0x00}; //爱心-1
byte love_2[8] =   {0x00, 0x66, 0xff, 0xff, 0x7e, 0x3c, 0x18, 0x00}; //爱心-2

//实心箭头
byte Front[8] =   {0x08, 0x1c, 0x3e, 0x7f, 0x1c, 0x1c, 0x1c, 0x1c}; //前
byte back[8] =    {0x1c, 0x1c, 0x1c, 0x1c, 0x7f, 0x3e, 0x1c, 0x08}; //后
byte left[8] =    {0x10, 0x30, 0x7f, 0xff, 0x7f, 0x30, 0x10, 0x00}; //左
byte right[8] =   {0x08, 0x0c, 0xfe, 0xff, 0xfe, 0x0c, 0x08, 0x00}; //右

LedControl lc = LedControl(DIN, CLK, CS, 0);//定义引脚

void setup() {
  lc.shutdown(0, false);      //MAX7219启动时处于省电模式
  lc.setIntensity(0, 15);     // 将亮度设置为最大值
  lc.clearDisplay(0);         // 并清除显示
}

void loop() {

  printByte(smile);
  delay(1000);

  printByte(love_1);
  delay(1000);

  printByte(love_2);
  delay(1000);

  printByte(Front);
  delay(1000);

  printByte(back);
  delay(1000);

  printByte(left);
  delay(1000);

  printByte(right);
  delay(1000);

  printLetter();
  lc.clearDisplay(0);
  delay(1000);
}

void printLetter(){
  printByte(m);
  delay(1000);
  printByte(i);
  delay(1000);
  printByte(r);
  delay(1000);
  printByte(o);
  delay(1000);
  printByte(t);
  delay(1000);
  printByte(e);
  delay(1000);
  printByte(k);
  delay(1000);
}

void printByte(byte character []){
  int i = 0;
  for (i = 0; i < 8; i++)
  {
    lc.setRow(0, i, character[i]);
  }
}

  

 

超简单的LED Matrix图形编辑工具:
在上述程序之后,介绍一下PixeltoMatrix图形编辑工具, PixeltoMatrix编辑的结果可以产生 Arduino 16 进位或 2 进位的动画程式码。
PixeltoMatrix编辑工具:下载
https://www.dropbox.com/sh/9ehblhdvx8yvx6b/AAAo24EZKiQbVtwySxL5h8lja?dl=0
▼执行PixelToMatrix.exe就可开始绘制图型,绘制完成后点击「Generate」,将会自动产生16 进位或 2 进位的代码,您可将此代码加入您的程式中,如上面范例第16行「爱心-1」。

程序十二:随机生成的一个 LED 模拟弹跳点
Arduino参考开源代码

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序十二:随机生成的一个 LED 模拟弹跳点
  引脚定义
  DIN = D11
  CLK = D13
  CS  = D10
*/

#include "LedControl.h"//导入驱动库
LedControl lc = LedControl (11, 13, 10, 1); //定义引脚

int row;
int row_prev;
int col;
int col_prev;
int ball_timer = 100;
long randNumber;
long updown = 1;       // 指示移动:+1 向上一排,-1 向下一排
long leftright = 1;    // 指示移动:+1 是右一列,-1 是左一列


byte smiley[] =       // 填充数组以打开笑脸
{
  B00111110,
  B01000001,
  B10010101,
  B10100001,
  B10100001,
  B10010101,
  B01000001,
  B00111010
};


void setup() {
  Serial.begin (9600);
  // MAX72XX 启动时处于省电模式,
  // 我们必须做一个叫醒服务
  lc.shutdown (0, false);

  // 将亮度设置为中等值
  lc.setIntensity (0, 8);     // 0 = 低; 8 = 高

  // 显示笑脸 - 证明 8x8 数组有效
  smile();
  delay(1000);

  //清除显示
  lc.clearDisplay (0);

  // 初始位置
  randomSeed (analogRead(0));
  col = random (1, 7);    // 选择 1 到 7 之间的列位置
  row = random (1, 7);    // 选择 1 到 7 之间的行位置

  // 检查显示单元是否工作

  // 在串行监视器上显示起始列和行号
  Serial.println ("开始条件" );
  Serial.print ("行: ");
  Serial.println (row);
  Serial.print ("列: ");
  Serial.println (col);
  Serial.println ("=======");
  Serial.println ();
}

void loop() {
  col_prev = col;      //记录球的位置,列
  row_prev = row;      //记录球的位置,行
  row = row + updown;
  col = col + leftright;

  checkVectors ();

  lc.setLed (0, col_prev, row_prev, false); // 清除前一个球的位置
  lc.setLed (0, col, row, true);            // 点亮新球的位置
  delay (ball_timer);
  debug(); // 运行调试模块
  delay(1000);
}
// ============ 子程序 ===========================================
void smile() {
  for (int i = 0; i < 8; i++)
  {
    lc.setRow (0, i, smiley[i]);
  }
}

void checkVectors () {
  if ((row > 7) && (updown = 1)) {
    updown = -1;
    row = 6;
  }

  if ((row < 0) && (updown = -1)) {
    updown = 1;
    row = 1;
  }

  if ((col > 7) && (leftright = 1)) {
    leftright = -1;
    col = 6;
  }

  if ((col < 0) && (leftright = -1)) {
    leftright = 1;
    col = 1;
  }
}

void debug() {
  Serial.print ("列: " );
  Serial.println (col);
  Serial.print ("行: " );
  Serial.println (row);
}

  

实验串口返回情况

 

 

程序十三:漫屏随机闪烁星星
Arduino参考开源代码

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序十三:漫屏随机闪烁星星
  引脚定义
  DIN = D11
  CLK = D13
  CS  = D10
*/

#include "LedControl.h"//导入驱动库
LedControl lc = LedControl (11, 13, 10, 1); //定义引脚

void setup() {
  lc.shutdown(0, false); // 关闭省电模式
  lc.setIntensity(0, 15); // 设定亮度为 15 (介于0~15之间)
  lc.clearDisplay(0);    // 清除屏幕
}

void loop() {
  int randNumber_col = random(8);
  int randNumber_row = random(8);
  lc.setLed(0, randNumber_col, randNumber_row, 1); //将Led的行列点,設定为亮
  delay(5);
  lc.setLed(0, randNumber_col, randNumber_row, 0); //将Led的行列点,設定为暗
}

  

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二十二:MAX7219点阵显示模块(8X8 LED共阴)
项目:Arduino 矩阵显示代码
Arduino实验开源代码

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二十二:MAX7219点阵显示模块(8X8 LED共阴)
  项目之四十一:Arduino 矩阵显示代码
*/

//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 the CLK 
 pin 10 is connected to LOAD 
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,1);

/* we always wait a bit between updates of the display */
unsigned long delaytime=100;

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]={B00111110,B00010000,B00100000,B00100000,B00010000};
  byte d[5]={B00011100,B00100010,B00100010,B00010010,B11111110};
  byte u[5]={B00111100,B00000010,B00000010,B00000100,B00111110};
  byte i[5]={B00000000,B00100010,B10111110,B00000010,B00000000};
  byte n[5]={B00111110,B00010000,B00100000,B00100000,B00011110};
  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(delaytime);
  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(delaytime);
  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(delaytime);
  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(delaytime);
  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(delaytime);
  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(delaytime);
  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(delaytime);
  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(delaytime);
}

/*
  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(delaytime);
    lc.setRow(0,row,B10100000);
    delay(delaytime);
    lc.setRow(0,row,(byte)0);
    for(int i=0;i<row;i++) {
      delay(delaytime);
      lc.setRow(0,row,B10100000);
      delay(delaytime);
      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(delaytime);
    lc.setColumn(0,col,B10100000);
    delay(delaytime);
    lc.setColumn(0,col,(byte)0);
    for(int i=0;i<col;i++) {
      delay(delaytime);
      lc.setColumn(0,col,B10100000);
      delay(delaytime);
      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(delaytime);
      lc.setLed(0,row,col,true);
      delay(delaytime);
      for(int i=0;i<col;i++) {
        lc.setLed(0,row,col,false);
        delay(delaytime);
        lc.setLed(0,row,col,true);
        delay(delaytime);
      }
    }
  }
}

void loop() { 
  writeArduinoOnMatrix();
  rows();
  columns();
  single();
}

  

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二十二:MAX7219点阵显示模块(8X8 LED共阴)
项目:冬天来了,是时候考虑一些温暖的想法和LED矩阵上的壁炉了
Arduino实验开源代码

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二十二:MAX7219点阵显示模块(8X8 LED共阴)
  项目之四十:冬天来了,是时候考虑一些温暖的想法和LED矩阵上的壁炉了
*/

#include "LedControl.h"
LedControl lc = LedControl(12, 11, 10, 1);

int theHeightTarget[8];
int theHeightCurrent[8];

int fireHeight = 0;
int sparkX = 0;

void setup() {
  lc.shutdown(0, false);
  lc.setIntensity(0, 8);
  lc.clearDisplay(0);
  Serial.begin(115200);
}

void loop() {  
  //lc.clearDisplay(0);
  // set Heights
  if (int(random(1)) == 0) {
    for (int x = 0; x < 8; x++) {
      theHeightTarget[x] = int(random(5)) + 1;
    }
  }

  // update
  for (int x = 0; x < 8; x++) {
    if (theHeightTarget[x] > theHeightCurrent[x])theHeightCurrent[x]++;
    if (theHeightTarget[x] < theHeightCurrent[x])theHeightCurrent[x]--;
  }

  // show display 
  for (int x = 0; x < 8; x++) {
    for (int y = 0; y < theHeightCurrent[x]; y++) {
      lc.setLed(0, x, y, 1);
    }
    for (int y = theHeightCurrent[x]; y < 8; y++) {
      lc.setLed(0, x, y, 0);
    }
  }

  // spark – Funkenflug
  sparkX = int(random(8));
  if (int(random(40)) == 0) {
    for (int y = 4; y < 8; y++) {
      if (y == 4) {
        lc.setLed(0, sparkX, y, 1);
      } else {
        lc.setLed(0, sparkX, y, 1);
        lc.setLed(0, sparkX, y - 1, 0);
      }
      delay(25);
    }
  } else {
    delay(100);
  }
}

  

确定各行的照明高度。


  1. if (int(random(1)) == 0) {
  2.   for (int x = 0; x < 8; x++) {
  3.     theHeightTarget[x] = int(random(5)) + 1;
  4.   }
  5. }
复制代码



尝试调整当前的光高。

  1. // update
  2. for (int x = 0; x < 8; x++) {
  3.   if (theHeightTarget[x] > theHeightCurrent[x])theHeightCurrent[x]++;
  4.   if (theHeightTarget[x] < theHeightCurrent[x])theHeightCurrent[x]--;
  5. }
复制代码



然后显示在矩阵上:

  1. // show display
  2. for (int x = 0; x < 8; x++) {
  3.   for (int y = 0; y < theHeightCurrent[x]; y++) {
  4.     lc.setLed(0, x, y, 1);
  5.   }
  6.   for (int y = theHeightCurrent[x]; y < 8; y++) {
  7.     lc.setLed(0, x, y, 0);
  8.   }
  9. }
复制代码



时不时地,火花溶解并向上飞扬。

  1. // spark – Funkenflug
  2. sparkX = int(random(8));
  3. if (int(random(40)) == 0) {
  4.   for (int y = 4; y < 8; y++) {
  5.     if (y == 4) {
  6.       lc.setLed(0, sparkX, y, 1);
  7.     } else {
  8.       lc.setLed(0, sparkX, y, 1);
  9.       lc.setLed(0, sparkX, y - 1, 0);
  10.     }
  11.   delay(25);
  12.   }
  13. } else {
  14.   delay(100);
  15. }
复制代码