亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Manufacturing Technology >> 制造工艺

8×8 LED矩阵MAX7219教程,通过蓝牙滚动文本和Android控制

在本 Arduino 教程中,我们将学习如何使用 MAX7219 驱动器和 Arduino 板控制 8×8 LED 矩阵。您可以观看以下视频或阅读下面的书面教程了解更多详情。

概览

我们将做三个例子,第一个我们将解释 MAX7219 的基本工作原理,在第二个例子中我们将看到 8×8 LED 矩阵上的滚动文本是如何工作的,在第三个例子中我们将控制它们通过蓝牙和自定义构建的 Android 应用程序。

MAX7219

现在让我们仔细看看 MAX7219 驱动器。该 IC 能够驱动 64 个单独的 LED,同时仅使用 3 根线与 Arduino 通信,而且我们可以菊花链多个驱动器和矩阵,仍然使用相同的 3 根线。

64 个 LED 由 IC 的 16 个输出引脚驱动。现在的问题是这怎么可能。好吧,同时点亮的 LED 的最大数量实际上是 8 个。 LED 排列为 8×8 组的行和列。因此,MAX7219 在很短的时间内激活每一列,同时它也驱动每一行。因此,通过快速切换列和行,人眼只会注意到连续的光。

请注意常见的 8×8 LED 矩阵的引脚是如何在内部排列的,因此如果您是自己构建矩阵,则应该考虑它。

另请注意,MAX7219 的通用分线板在 5V 和 IC 引脚 18 之间带有一个电阻器。该电阻器用于设置亮度或流向 LED 的电流。

IC数据表中的下表显示了我们应该根据LED的正向压降使用的电阻值。

电路原理图


现在让我们将 8×8 LED 矩阵模块连接到 Arduino 板。电路原理图如下:

模块的 VCC 和 GND 连接到 Arduino 的 5V 和 GND 引脚,其他三个引脚 DIN、CLK 和 CS 连接到 Arduino 板的任何数字引脚。如果我们想连接多个模块,我们只需将前一个分线板的输出引脚连接到新模块的输入引脚。其实这些管脚都是一样的,只是之前板子的DOUT管脚接新板子的DIN管脚。

您可以从以下链接获取本 Arduino 教程所需的组件:

  • 8×8 LED MAX7219 点阵模块...... ..
  • HC-05蓝牙模块…………………………
  • Arduino 开发板…………………………………………..
  • 面包板和跳线……………………

基本 MAX7219 Arduino 代码

连接模块后,我们就可以查看第一个示例的 Arduino 代码了。我们将使用可以从 GitHub 下载的 MaxMatrix 库。

/*
      8x8 LED Matrix MAX7219 Example 01

   by Dejan Nedelkovski, www.HowToMechatronics.com

   Based on the following library:
   GitHub | riyas-org/max7219  https://github.com/riyas-org/max7219
*/

#include <MaxMatrix.h>

int DIN = 7;   // DIN pin of MAX7219 module
int CLK = 6;   // CLK pin of MAX7219 module
int CS = 5;    // CS pin of MAX7219 module
int maxInUse = 1;


MaxMatrix m(DIN, CS, CLK, maxInUse); 

char A[] = {4, 8,
            B01111110,
            B00010001,
            B00010001,
            B01111110,
           };

char B[] = {4, 8,
            B01111111,
            B01001001,
            B01001001,
            B00110110,
           };

char smile01[] = {8, 8,
                  B00111100,
                  B01000010,
                  B10010101,
                  B10100001,
                  B10100001,
                  B10010101,
                  B01000010,
                  B00111100
                 };
char smile02[] = {8, 8,
                  B00111100,
                  B01000010,
                  B10010101,
                  B10010001,
                  B10010001,
                  B10010101,
                  B01000010,
                  B00111100
                 };
char smile03[] = {8, 8,
                  B00111100,
                  B01000010,
                  B10100101,
                  B10010001,
                  B10010001,
                  B10100101,
                  B01000010,
                  B00111100
                 };

void setup() {
  m.init(); // MAX7219 initialization
  m.setIntensity(8); // initial led matrix intensity, 0-15
}

void loop() {
  // Seting the LEDs On or Off at x,y or row,column position
  m.setDot(6,2,true); 
  delay(1000);
  m.setDot(6,3,true);
  delay(1000);
  m.clear(); // Clears the display
  for (int i=0; i<8; i++){
    m.setDot(i,i,true);
    delay(300);
  }
  m.clear();
  // Displaying the character at x,y (upper left corner of the character)  
  m.writeSprite(2, 0, A);
  delay(1000);

  m.writeSprite(2, 0, B);
  delay(1000);

  m.writeSprite(0, 0, smile01);
  delay(1000);
  
  m.writeSprite(0, 0, smile02);
  delay(1000);
  
  m.writeSprite(0, 0, smile03);
  delay(1000);
  
  for (int i=0; i<8; i++){
    m.shiftLeft(false,false);
    delay(300);
  }
  m.clear();

}
Code language: Arduino (arduino)

说明: 所以首先我们需要包含 MaxMatrix.h 库,定义模块连接的引脚,设置我们使用的模块数量并定义 MaxMatrix 对象。

为了显示字符,我们需要在字符或字节数组中定义它们,这里我有几个例子。我们可以注意到这些位是如何形成实际上是零和一的字符的。在这种情况下,它们会旋转 90 度,但库示例建议以这样的方式使用它们,以便以后更容易实现用于滚动文本的 shiftLeft 自定义函数。

在设置部分,我们只需要初始化模块并设置 LED 的亮度。在使用 setDot() 函数的循环部分,我们可以将任何单个 LED 设置为在 X、Y 或 Row/Column 位置点亮,并使用 clear() 函数可以清除显示。

为了显示预定义的字符,我们使用 writeSprite() 函数,前两个参数是字符左上角的 X 和 Y 位置。最后使用 shiftLeft() 函数向左移动或滚动字符。

8×8 LED 矩阵滚动Arduino代码


接下来让我们看一下滚动文本示例,看看有什么不同。您将在代码下方找到其说明。

/*
      8x8 LED Matrix MAX7219 Scrolling Text Example

   Based on the following library:
   GitHub | riyas-org/max7219  https://github.com/riyas-org/max7219
*/


#include <MaxMatrix.h>
#include <avr/pgmspace.h>

PROGMEM const unsigned char CH[] = {
  3, 8, B00000000, B00000000, B00000000, B00000000, B00000000, // space
  1, 8, B01011111, B00000000, B00000000, B00000000, B00000000, // !
  3, 8, B00000011, B00000000, B00000011, B00000000, B00000000, // "
  5, 8, B00010100, B00111110, B00010100, B00111110, B00010100, // #
  4, 8, B00100100, B01101010, B00101011, B00010010, B00000000, // $
  5, 8, B01100011, B00010011, B00001000, B01100100, B01100011, // %
  5, 8, B00110110, B01001001, B01010110, B00100000, B01010000, // &
  1, 8, B00000011, B00000000, B00000000, B00000000, B00000000, // '
  3, 8, B00011100, B00100010, B01000001, B00000000, B00000000, // (
  3, 8, B01000001, B00100010, B00011100, B00000000, B00000000, // )
  5, 8, B00101000, B00011000, B00001110, B00011000, B00101000, // *
  5, 8, B00001000, B00001000, B00111110, B00001000, B00001000, // +
  2, 8, B10110000, B01110000, B00000000, B00000000, B00000000, // ,
  4, 8, B00001000, B00001000, B00001000, B00001000, B00000000, // -
  2, 8, B01100000, B01100000, B00000000, B00000000, B00000000, // .
  4, 8, B01100000, B00011000, B00000110, B00000001, B00000000, // /
  4, 8, B00111110, B01000001, B01000001, B00111110, B00000000, // 0
  3, 8, B01000010, B01111111, B01000000, B00000000, B00000000, // 1
  4, 8, B01100010, B01010001, B01001001, B01000110, B00000000, // 2
  4, 8, B00100010, B01000001, B01001001, B00110110, B00000000, // 3
  4, 8, B00011000, B00010100, B00010010, B01111111, B00000000, // 4
  4, 8, B00100111, B01000101, B01000101, B00111001, B00000000, // 5
  4, 8, B00111110, B01001001, B01001001, B00110000, B00000000, // 6
  4, 8, B01100001, B00010001, B00001001, B00000111, B00000000, // 7
  4, 8, B00110110, B01001001, B01001001, B00110110, B00000000, // 8
  4, 8, B00000110, B01001001, B01001001, B00111110, B00000000, // 9
  2, 8, B01010000, B00000000, B00000000, B00000000, B00000000, // :
  2, 8, B10000000, B01010000, B00000000, B00000000, B00000000, // ;
  3, 8, B00010000, B00101000, B01000100, B00000000, B00000000, // <
  3, 8, B00010100, B00010100, B00010100, B00000000, B00000000, // =
  3, 8, B01000100, B00101000, B00010000, B00000000, B00000000, // >
  4, 8, B00000010, B01011001, B00001001, B00000110, B00000000, // ?
  5, 8, B00111110, B01001001, B01010101, B01011101, B00001110, // @
  4, 8, B01111110, B00010001, B00010001, B01111110, B00000000, // A
  4, 8, B01111111, B01001001, B01001001, B00110110, B00000000, // B
  4, 8, B00111110, B01000001, B01000001, B00100010, B00000000, // C
  4, 8, B01111111, B01000001, B01000001, B00111110, B00000000, // D
  4, 8, B01111111, B01001001, B01001001, B01000001, B00000000, // E
  4, 8, B01111111, B00001001, B00001001, B00000001, B00000000, // F
  4, 8, B00111110, B01000001, B01001001, B01111010, B00000000, // G
  4, 8, B01111111, B00001000, B00001000, B01111111, B00000000, // H
  3, 8, B01000001, B01111111, B01000001, B00000000, B00000000, // I
  4, 8, B00110000, B01000000, B01000001, B00111111, B00000000, // J
  4, 8, B01111111, B00001000, B00010100, B01100011, B00000000, // K
  4, 8, B01111111, B01000000, B01000000, B01000000, B00000000, // L
  5, 8, B01111111, B00000010, B00001100, B00000010, B01111111, // M
  5, 8, B01111111, B00000100, B00001000, B00010000, B01111111, // N
  4, 8, B00111110, B01000001, B01000001, B00111110, B00000000, // O
  4, 8, B01111111, B00001001, B00001001, B00000110, B00000000, // P
  4, 8, B00111110, B01000001, B01000001, B10111110, B00000000, // Q
  4, 8, B01111111, B00001001, B00001001, B01110110, B00000000, // R
  4, 8, B01000110, B01001001, B01001001, B00110010, B00000000, // S
  5, 8, B00000001, B00000001, B01111111, B00000001, B00000001, // T
  4, 8, B00111111, B01000000, B01000000, B00111111, B00000000, // U
  5, 8, B00001111, B00110000, B01000000, B00110000, B00001111, // V
  5, 8, B00111111, B01000000, B00111000, B01000000, B00111111, // W
  5, 8, B01100011, B00010100, B00001000, B00010100, B01100011, // X
  5, 8, B00000111, B00001000, B01110000, B00001000, B00000111, // Y
  4, 8, B01100001, B01010001, B01001001, B01000111, B00000000, // Z
  2, 8, B01111111, B01000001, B00000000, B00000000, B00000000, // [
  4, 8, B00000001, B00000110, B00011000, B01100000, B00000000, // \ backslash
  2, 8, B01000001, B01111111, B00000000, B00000000, B00000000, // ]
  3, 8, B00000010, B00000001, B00000010, B00000000, B00000000, // hat
  4, 8, B01000000, B01000000, B01000000, B01000000, B00000000, // _
  2, 8, B00000001, B00000010, B00000000, B00000000, B00000000, // `
  4, 8, B00100000, B01010100, B01010100, B01111000, B00000000, // a
  4, 8, B01111111, B01000100, B01000100, B00111000, B00000000, // b
  4, 8, B00111000, B01000100, B01000100, B00101000, B00000000, // c
  4, 8, B00111000, B01000100, B01000100, B01111111, B00000000, // d
  4, 8, B00111000, B01010100, B01010100, B00011000, B00000000, // e
  3, 8, B00000100, B01111110, B00000101, B00000000, B00000000, // f
  4, 8, B10011000, B10100100, B10100100, B01111000, B00000000, // g
  4, 8, B01111111, B00000100, B00000100, B01111000, B00000000, // h
  3, 8, B01000100, B01111101, B01000000, B00000000, B00000000, // i
  4, 8, B01000000, B10000000, B10000100, B01111101, B00000000, // j
  4, 8, B01111111, B00010000, B00101000, B01000100, B00000000, // k
  3, 8, B01000001, B01111111, B01000000, B00000000, B00000000, // l
  5, 8, B01111100, B00000100, B01111100, B00000100, B01111000, // m
  4, 8, B01111100, B00000100, B00000100, B01111000, B00000000, // n
  4, 8, B00111000, B01000100, B01000100, B00111000, B00000000, // o
  4, 8, B11111100, B00100100, B00100100, B00011000, B00000000, // p
  4, 8, B00011000, B00100100, B00100100, B11111100, B00000000, // q
  4, 8, B01111100, B00001000, B00000100, B00000100, B00000000, // r
  4, 8, B01001000, B01010100, B01010100, B00100100, B00000000, // s
  3, 8, B00000100, B00111111, B01000100, B00000000, B00000000, // t
  4, 8, B00111100, B01000000, B01000000, B01111100, B00000000, // u
  5, 8, B00011100, B00100000, B01000000, B00100000, B00011100, // v
  5, 8, B00111100, B01000000, B00111100, B01000000, B00111100, // w
  5, 8, B01000100, B00101000, B00010000, B00101000, B01000100, // x
  4, 8, B10011100, B10100000, B10100000, B01111100, B00000000, // y
  3, 8, B01100100, B01010100, B01001100, B00000000, B00000000, // z
  3, 8, B00001000, B00110110, B01000001, B00000000, B00000000, // {
  1, 8, B01111111, B00000000, B00000000, B00000000, B00000000, // |
  3, 8, B01000001, B00110110, B00001000, B00000000, B00000000, // }
  4, 8, B00001000, B00000100, B00001000, B00000100, B00000000, // ~
};

int DIN = 7;   // DIN pin of MAX7219 module
int CLK = 6;   // CLK pin of MAX7219 module
int CS = 5;    // CS pin of MAX7219 module
int maxInUse = 2;

MaxMatrix m(DIN, CS, CLK, maxInUse);

byte buffer[10];

char text[]= "HowToMechatronics.com  "; // Scrolling text

void setup() {
  m.init(); // module initialize
  m.setIntensity(15); // dot matix intensity 0-15
}

void loop() {

  printStringWithShift(text, 100); // (text, scrolling speed)

}
// Display=the extracted characters with scrolling
void printCharWithShift(char c, int shift_speed) {
  if (c < 32) return;
  c -= 32;
  memcpy_P(buffer, CH + 7 * c, 7);
  m.writeSprite(32, 0, buffer);
  m.setColumn(32 + buffer[0], 0);

  for (int i = 0; i < buffer[0] + 1; i++)
  {
    delay(shift_speed);
    m.shiftLeft(false, false);
  }
}
// Extract the characters from the text string
void printStringWithShift(char* s, int shift_speed) {
  while (*s != 0) {
    printCharWithShift(*s, shift_speed);
    s++;
  }
}Code language: Arduino (arduino)

说明: 在这里,我们必须为 PROGMEN 添加一个附加库,它是变量修饰符,用于将数据存储在闪存而不是 SRAM 中。当我们有一个较大的静态变量数据库时,例如在这种情况下定义字母和字符,最好将它们存储在闪存中,因为它比 SRAM 的 2K 字节大得多,32K 字节。

接下来使用字符数组定义滚动文本,在循环部分中,自定义函数 printStringWithShift 以第二个参数定义的滚动速度(以毫秒为单位)在 LED 矩阵上打印滚动文本。这个自定义函数做的第一件事就是从文本字符串中提取字符,然后在led矩阵上显示这些滚动字符。

通过蓝牙控制 8×8 LED 矩阵的安卓应用


一旦我们了解了 MAX7219 的工作原理,现在我们可以制作第三个示例,这是一个实用的 Arduino 项目,我们将构建一个定制的 Android 应用程序来通过蓝牙通信控制 LED 矩阵。在继续之前,我建议您查看我的详细教程,了解如何使用 HC-05 蓝牙模块以及如何使用 MIT App Inventor 在线应用程序构建自定义 Android 应用程序。

这是 Arduino 代码,现在让我们看看与上一个示例相比的修改。

/*
      8x8 LED Matrix MAX7219 Scrolling Text
          Android Control via Bluetooth

   by Dejan Nedelkovski, www.HowToMechatronics.com

   Based on the following library:
   GitHub | riyas-org/max7219  https://github.com/riyas-org/max7219
*/

#include <MaxMatrix.h>
#include <SoftwareSerial.h>
#include <avr/pgmspace.h>

PROGMEM const unsigned char CH[] = {
  3, 8, B00000000, B00000000, B00000000, B00000000, B00000000, // space
  1, 8, B01011111, B00000000, B00000000, B00000000, B00000000, // !
  3, 8, B00000011, B00000000, B00000011, B00000000, B00000000, // "
  5, 8, B00010100, B00111110, B00010100, B00111110, B00010100, // #
  4, 8, B00100100, B01101010, B00101011, B00010010, B00000000, // $
  5, 8, B01100011, B00010011, B00001000, B01100100, B01100011, // %
  5, 8, B00110110, B01001001, B01010110, B00100000, B01010000, // &
  1, 8, B00000011, B00000000, B00000000, B00000000, B00000000, // '
  3, 8, B00011100, B00100010, B01000001, B00000000, B00000000, // (
  3, 8, B01000001, B00100010, B00011100, B00000000, B00000000, // )
  5, 8, B00101000, B00011000, B00001110, B00011000, B00101000, // *
  5, 8, B00001000, B00001000, B00111110, B00001000, B00001000, // +
  2, 8, B10110000, B01110000, B00000000, B00000000, B00000000, // ,
  4, 8, B00001000, B00001000, B00001000, B00001000, B00000000, // -
  2, 8, B01100000, B01100000, B00000000, B00000000, B00000000, // .
  4, 8, B01100000, B00011000, B00000110, B00000001, B00000000, // /
  4, 8, B00111110, B01000001, B01000001, B00111110, B00000000, // 0
  3, 8, B01000010, B01111111, B01000000, B00000000, B00000000, // 1
  4, 8, B01100010, B01010001, B01001001, B01000110, B00000000, // 2
  4, 8, B00100010, B01000001, B01001001, B00110110, B00000000, // 3
  4, 8, B00011000, B00010100, B00010010, B01111111, B00000000, // 4
  4, 8, B00100111, B01000101, B01000101, B00111001, B00000000, // 5
  4, 8, B00111110, B01001001, B01001001, B00110000, B00000000, // 6
  4, 8, B01100001, B00010001, B00001001, B00000111, B00000000, // 7
  4, 8, B00110110, B01001001, B01001001, B00110110, B00000000, // 8
  4, 8, B00000110, B01001001, B01001001, B00111110, B00000000, // 9
  2, 8, B01010000, B00000000, B00000000, B00000000, B00000000, // :
  2, 8, B10000000, B01010000, B00000000, B00000000, B00000000, // ;
  3, 8, B00010000, B00101000, B01000100, B00000000, B00000000, // <
  3, 8, B00010100, B00010100, B00010100, B00000000, B00000000, // =
  3, 8, B01000100, B00101000, B00010000, B00000000, B00000000, // >
  4, 8, B00000010, B01011001, B00001001, B00000110, B00000000, // ?
  5, 8, B00111110, B01001001, B01010101, B01011101, B00001110, // @
  4, 8, B01111110, B00010001, B00010001, B01111110, B00000000, // A
  4, 8, B01111111, B01001001, B01001001, B00110110, B00000000, // B
  4, 8, B00111110, B01000001, B01000001, B00100010, B00000000, // C
  4, 8, B01111111, B01000001, B01000001, B00111110, B00000000, // D
  4, 8, B01111111, B01001001, B01001001, B01000001, B00000000, // E
  4, 8, B01111111, B00001001, B00001001, B00000001, B00000000, // F
  4, 8, B00111110, B01000001, B01001001, B01111010, B00000000, // G
  4, 8, B01111111, B00001000, B00001000, B01111111, B00000000, // H
  3, 8, B01000001, B01111111, B01000001, B00000000, B00000000, // I
  4, 8, B00110000, B01000000, B01000001, B00111111, B00000000, // J
  4, 8, B01111111, B00001000, B00010100, B01100011, B00000000, // K
  4, 8, B01111111, B01000000, B01000000, B01000000, B00000000, // L
  5, 8, B01111111, B00000010, B00001100, B00000010, B01111111, // M
  5, 8, B01111111, B00000100, B00001000, B00010000, B01111111, // N
  4, 8, B00111110, B01000001, B01000001, B00111110, B00000000, // O
  4, 8, B01111111, B00001001, B00001001, B00000110, B00000000, // P
  4, 8, B00111110, B01000001, B01000001, B10111110, B00000000, // Q
  4, 8, B01111111, B00001001, B00001001, B01110110, B00000000, // R
  4, 8, B01000110, B01001001, B01001001, B00110010, B00000000, // S
  5, 8, B00000001, B00000001, B01111111, B00000001, B00000001, // T
  4, 8, B00111111, B01000000, B01000000, B00111111, B00000000, // U
  5, 8, B00001111, B00110000, B01000000, B00110000, B00001111, // V
  5, 8, B00111111, B01000000, B00111000, B01000000, B00111111, // W
  5, 8, B01100011, B00010100, B00001000, B00010100, B01100011, // X
  5, 8, B00000111, B00001000, B01110000, B00001000, B00000111, // Y
  4, 8, B01100001, B01010001, B01001001, B01000111, B00000000, // Z
  2, 8, B01111111, B01000001, B00000000, B00000000, B00000000, // [
  4, 8, B00000001, B00000110, B00011000, B01100000, B00000000, // \ backslash
  2, 8, B01000001, B01111111, B00000000, B00000000, B00000000, // ]
  3, 8, B00000010, B00000001, B00000010, B00000000, B00000000, // hat
  4, 8, B01000000, B01000000, B01000000, B01000000, B00000000, // _
  2, 8, B00000001, B00000010, B00000000, B00000000, B00000000, // `
  4, 8, B00100000, B01010100, B01010100, B01111000, B00000000, // a
  4, 8, B01111111, B01000100, B01000100, B00111000, B00000000, // b
  4, 8, B00111000, B01000100, B01000100, B00101000, B00000000, // c
  4, 8, B00111000, B01000100, B01000100, B01111111, B00000000, // d
  4, 8, B00111000, B01010100, B01010100, B00011000, B00000000, // e
  3, 8, B00000100, B01111110, B00000101, B00000000, B00000000, // f
  4, 8, B10011000, B10100100, B10100100, B01111000, B00000000, // g
  4, 8, B01111111, B00000100, B00000100, B01111000, B00000000, // h
  3, 8, B01000100, B01111101, B01000000, B00000000, B00000000, // i
  4, 8, B01000000, B10000000, B10000100, B01111101, B00000000, // j
  4, 8, B01111111, B00010000, B00101000, B01000100, B00000000, // k
  3, 8, B01000001, B01111111, B01000000, B00000000, B00000000, // l
  5, 8, B01111100, B00000100, B01111100, B00000100, B01111000, // m
  4, 8, B01111100, B00000100, B00000100, B01111000, B00000000, // n
  4, 8, B00111000, B01000100, B01000100, B00111000, B00000000, // o
  4, 8, B11111100, B00100100, B00100100, B00011000, B00000000, // p
  4, 8, B00011000, B00100100, B00100100, B11111100, B00000000, // q
  4, 8, B01111100, B00001000, B00000100, B00000100, B00000000, // r
  4, 8, B01001000, B01010100, B01010100, B00100100, B00000000, // s
  3, 8, B00000100, B00111111, B01000100, B00000000, B00000000, // t
  4, 8, B00111100, B01000000, B01000000, B01111100, B00000000, // u
  5, 8, B00011100, B00100000, B01000000, B00100000, B00011100, // v
  5, 8, B00111100, B01000000, B00111100, B01000000, B00111100, // w
  5, 8, B01000100, B00101000, B00010000, B00101000, B01000100, // x
  4, 8, B10011100, B10100000, B10100000, B01111100, B00000000, // y
  3, 8, B01100100, B01010100, B01001100, B00000000, B00000000, // z
  3, 8, B00001000, B00110110, B01000001, B00000000, B00000000, // {
  1, 8, B01111111, B00000000, B00000000, B00000000, B00000000, // |
  3, 8, B01000001, B00110110, B00001000, B00000000, B00000000, // }
  4, 8, B00001000, B00000100, B00001000, B00000100, B00000000, // ~
};

int dIn = 7;   // DIN pin of MAX7219 module
int clk = 6;   // CLK pin of MAX7219 module
int cs = 5;    // CS pin of MAX7219 module

int maxInUse = 2;    // Number of MAX7219's connected

MaxMatrix m(dIn, cs, clk, maxInUse);
SoftwareSerial Bluetooth(8, 7); // Bluetooth

byte buffer[10];
char incomebyte;
int scrollSpeed = 100;
char text[100] = "HowToMechatronics.com  "; // Initial text message
int brightness = 15;
int count = 0;
char indicator;

void setup() {
  m.init(); // MAX7219 initialization
  m.setIntensity(brightness); // initial led matrix intensity, 0-15
  Bluetooth.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
  // Printing the text
  printStringWithShift(text, scrollSpeed);
  
  if (Bluetooth.available()) {   // Checks whether data is comming from the serial port
    indicator = Bluetooth.read();   // Starts reading the serial port, the first byte from the incoming data
    // If we have pressed the "Send" button from the Android App, clear the previous text
    if (indicator == '1') {
      for (int i = 0; i < 100; i++) {
        text[i] = 0;
        m.clear();
      }
      // Read the whole data/string comming from the phone and put it into text[] array.
      while (Bluetooth.available()) {
        incomebyte = Bluetooth.read();
        text[count] = incomebyte;
        count++;
      }
      count = 0;
    }
    // Adjusting the Scrolling Speed
    else if (indicator == '2') {
      String sS = Bluetooth.readString();
      scrollSpeed = 150 - sS.toInt(); // Milliseconds, subtraction because lower value means higher scrolling speed
    }
    // Adjusting the brightness
    else if (indicator == '3') {
      String sB = Bluetooth.readString();
      brightness = sB.toInt();
      m.setIntensity(brightness);
    }
  }

}

void printCharWithShift(char c, int shift_speed) {
  if (c < 32) return;
  c -= 32;
  memcpy_P(buffer, CH + 7 * c, 7);
  m.writeSprite(32, 0, buffer);
  m.setColumn(32 + buffer[0], 0);

  for (int i = 0; i < buffer[0] + 1; i++)
  {
    delay(shift_speed);
    m.shiftLeft(false, false);
  }
}

void printStringWithShift(char* s, int shift_speed) {
  while (*s != 0) {
    printCharWithShift(*s, shift_speed);
    s++;
  }
}

void printString(char* s)
{
  int col = 0;
  while (*s != 0)
  {
    if (*s < 32) continue;
    char c = *s - 32;
    memcpy_P(buffer, CH + 7 * c, 7);
    m.writeSprite(col, 0, buffer);
    m.setColumn(col + buffer[0], 0);
    col += buffer[0] + 1;
    s++;
  }
}Code language: Arduino (arduino)

说明: 首先,我们需要包含 SoftwareSerial.h 库,它将启用蓝牙通信并定义程序所需的一些变量。在设置部分,我们需要将蓝牙初始化为其默认波特率 38400 比特每秒。我将初始短信设置为“HowToMechatronics.com”,滚动速度延迟 100 毫秒。

接下来,在循环部分,我们使用 Bluetooth.available() 函数检查是否有来自串行端口的传入数据,如果是,则使用 Bluetooth.read 函数开始读取串行端口,每次迭代一个字节。因此,第一个传入的字节将始终存储在“indicator”变量中,并根据它选择是否更改文本消息、滚动速度或 LED 矩阵的亮度。

如果我们查看 Android 应用程序代码块,我们会注意到,当单击“发送”按钮时,首先我们发送指示字节,在本例中为“1”,这意味着我们想要更改文本消息。为此,在 Arduino 端,我们将清除整个字符数组并清除 LED 矩阵显示。然后在“while”循环中,我们将读取串口中的其余数据,也就是在 Android 应用的文本框中输入的消息。

如果指示变量是“2”,这意味着我们已经改变了滚动速度滑块的位置,所以我们将使用 Bluetooth.readString() 函数读取它的新值并调整滚动速度。以同样的方式调整 LED 的亮度。

在这里你可以下载安卓应用:

适用于 Arduino 8x8 LED 矩阵控制的 AndrodApp

1 个文件 1.48 MB 下载

适用于 Arduino 8×8 LED 矩阵控制的 AndrodApp .aia 文件

1 个文件 34.06 KB 下载

这就是本教程的几乎所有内容,如果您有任何问题,可以使用下面的评论部分。


制造工艺

  1. 使用 Arduino 控制硬币接收器
  2. Arduino Pong Game on 24x16 Matrix with MAX7219
  3. Arduino 带蓝牙控制 LED!
  4. 使用 Cortana 控制 LED
  5. 使用 Arduino Uno 控制 LED 矩阵
  6. 使用 Arduino Uno 和蓝牙进行汽车控制
  7. 使用 Android 应用程序控制 Arduino 机械臂
  8. 带有滚动文本的 Arduino 蓝牙控制矩阵
  9. MAX72XX LED 矩阵显示小行星游戏
  10. Arduino/Android - BLUETOOTH 多伺服电机控制
  11. 蛇形 LED 矩阵游戏
  12. Arduino - 通过网络控制灯泡