极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11926|回复: 4

max7219LED假唱机器人程序

[复制链接]
发表于 2016-8-19 01:35:13 | 显示全部楼层 |阅读模式
最近在一个创客群里发现有人做了个假唱机器人,用的是DFrobot的8*8点阵LED,我又在instructables上发现有这个项目,估计群里的那个也是看这个做出来的,instructables上的是直接焊,我有一块Max7219的点阵LED,想把它程序改成Max7219的,可是instructables上的程序有点看不懂,请教一下怎么改,http://www.instructables.com/id/Lip-Synching-Robot/

[pre lang="arduino" line="1" file="Lip_Synching_Robot.ino"]/*

   @file Lip_Synching_Robot.ino
   @brief 8 x 8 LED Matrix animation using Arduino.
   @author Kei Takagi
   @date 2016.7.7

   8 x 8 Led Matrix ... 2-17 pin
   MIC Input        ... A4 pin

   Copyright (c) 2016 Kei dTakagi
   Revfleased under the MIT license
   http://opensource.org/licenses/mit-license.php

*/

#include <MsTimer2.h>
#define FallVelocity 40

// 8 x 8 matrix LED
// ROW side cathode , COLOMN side of the anode
byte pins[17] = { -1, 9, 8, 7, 6, 5, 4, 3, 2, 17, 16, 15, 14, 13, 12, 11, 10};
const static byte cols[8] = {pins[13], pins[ 3], pins[ 4], pins[10], pins[ 6], pins[11], pins[15], pins[16]};
const static byte rows[8] = {pins[ 9], pins[14], pins[ 8], pins[12], pins[ 1], pins[ 7], pins[ 2], pins[ 5]};


int anime_frame = 0;
int cat = 0;
int ano = 0;
int val = 0;
int max = 0;
int min = 1024;
int i = 0;

unsigned long time;
byte layer[8];

byte face[12][8] = {
  {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00000000,
    0b00000000,
    0b01111110,
    0b00000000,
    0b00000000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00000000,
    0b00011000,
    0b00100100,
    0b00011000,
    0b00000000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00000000,
    0b00000000,
    0b00011000,
    0b00000000,
    0b00000000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00000000,
    0b01111110,
    0b10000001,
    0b01111110,
    0b00000000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00000000,
    0b00111100,
    0b01000010,
    0b00111100,
    0b00000000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00111100,
    0b01000010,
    0b01000010,
    0b01000010,
    0b00111100,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00011000,
    0b00100100,
    0b00100100,
    0b00100100,
    0b00011000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b01111110,
    0b10000001,
    0b10000001,
    0b01111110,
    0b00000000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00111100,
    0b01000010,
    0b01000010,
    0b01000010,
    0b00111100,
  }, {
    0b01100110,
    0b01100110,
    0b01111110,
    0b10000001,
    0b10000001,
    0b10000001,
    0b10000001,
    0b01111110,
  }, {
    0b01100110,
    0b01100110,
    0b01111110,
    0b10000001,
    0b10000001,
    0b10000001,
    0b10000001,
    0b01111110,
  }, {
    0b01100110,
    0b00000000,
    0b01111110,
    0b10000001,
    0b10000001,
    0b10000001,
    0b10000001,
    0b01111110,
  }
};

void setup()
{
  for ( cat = 0; cat < 8; cat++ ) {
    pinMode( rows[cat], OUTPUT );
    digitalWrite( rows[cat], HIGH );
  }
  for ( ano = 0; ano < 8; ano++ ) {
    pinMode( cols[ano], OUTPUT );
    digitalWrite( cols[ano], LOW );
  }

  time = millis() +  FallVelocity;
  MsTimer2::set(1, display);
  MsTimer2::start();

  randomSeed(analogRead(4));
}

void loop()
{
  val = 0.7 * val + 0.3 * analogRead(A4) ;
  if (max < val)max = val;
  if (min > val)min = val;
  if (time < millis()) {
    time = millis() +  FallVelocity;

    anime_frame = map(max, min, 1023, 0, 12);
    for ( i = 0; i < 8; i++) {
      *(layer + i) = *(face[anime_frame] + i);
    }
    switch (random(0, 50) ) {
      case 1:
        *(layer + 0) = 0b01000100;
        *(layer + 1) = 0b01000100;
        break;
      case 2:
        *(layer + 0) = 0b00100010;
        *(layer + 1) = 0b00100010;
        break;
      case 3:
        *(layer + 0) = 0b00000000;
        break;
      case 4:
        *(layer + 1) = 0b00000000;
        break;
    }
    min = max;
    max = 0;
  }
}

void display()
{
  cat < 7 ? cat++ :  cat = 0;
  digitalWrite( *(rows + cat), LOW );
  for ( ano = 0; ano < 8; ano++ ) {
    digitalWrite( *(cols + ano) , (*(layer + ano) >> cat ) & 0x01 );
    delayMicroseconds(50);
    digitalWrite( *(cols + ano), LOW );
  }
  digitalWrite( *(rows + cat), HIGH );
}
[/code]



以下是我根据上面改出来的,可是吐舌头那段程序看不懂
  1. for ( i = 0; i < 8; i++) {
  2.       *(layer + i) = *(face[anime_frame] + i);
  3.     }
  4.     switch (random(0, 50) ) {
  5.       case 1:
  6.         *(layer + 0) = 0b01000100;
  7.         *(layer + 1) = 0b01000100;
  8.         break;
  9.       case 2:
  10.         *(layer + 0) = 0b00100010;
  11.         *(layer + 1) = 0b00100010;
  12.         break;
  13.       case 3:
  14.         *(layer + 0) = 0b00000000;
  15.         break;
  16.       case 4:
  17.         *(layer + 1) = 0b00000000;
  18.         break;
  19.     }
复制代码


[pre lang="arduino" line="1" file="max7219"]#include <LedControl.h>

const int DIN_PIN = 5;
const int CS_PIN = 6;
const int CLK_PIN = 7;

const byte IMAGES[][8] = {
{
    0b01100110,
    0b01100110,
    0b00000000,
    0b00000000,
    0b00000000,
    0b01111110,
    0b00000000,
    0b00000000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00000000,
    0b00011000,
    0b00100100,
    0b00011000,
    0b00000000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00000000,
    0b00000000,
    0b00011000,
    0b00000000,
    0b00000000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00000000,
    0b01111110,
    0b10000001,
    0b01111110,
    0b00000000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00000000,
    0b00111100,
    0b01000010,
    0b00111100,
    0b00000000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00111100,
    0b01000010,
    0b01000010,
    0b01000010,
    0b00111100,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00011000,
    0b00100100,
    0b00100100,
    0b00100100,
    0b00011000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b01111110,
    0b10000001,
    0b10000001,
    0b01111110,
    0b00000000,
  }, {
    0b01100110,
    0b01100110,
    0b00000000,
    0b00111100,
    0b01000010,
    0b01000010,
    0b01000010,
    0b00111100,
  }, {
    0b01100110,
    0b01100110,
    0b01111110,
    0b10000001,
    0b10000001,
    0b10000001,
    0b10000001,
    0b01111110,
  }, {
    0b01100110,
    0b01100110,
    0b01111110,
    0b10000001,
    0b10000001,
    0b10000001,
    0b10000001,
    0b01111110,
  }, {
    0b01100110,
    0b00000000,
    0b01111110,
    0b10000001,
    0b10000001,
    0b10000001,
    0b10000001,
    0b01111110,
  }
};
const int IMAGES_LEN = sizeof(IMAGES)/8;

LedControl matrix_led = LedControl(DIN_PIN, CLK_PIN, CS_PIN);

void setup() {
  matrix_led.clearDisplay(0);
  matrix_led.shutdown(0, false);
  matrix_led.setIntensity(0, 1);//设置亮度
}

void displayImage(byte *image) {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      matrix_led.setLed(0, i, j, bitRead(image, 7-j));
    }
  }
}

int i = 0;

void loop() {
  displayImage(IMAGES);
  if (++i >= IMAGES_LEN ) {
    i = 0;
  }
  delay(50);
}[/code]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2016-8-19 11:23:42 | 显示全部楼层
感谢分享,学习学习
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-11-10 19:43:13 | 显示全部楼层
  1. /*

  2.    @file Lip_Synching_Robot_MAX7219.ino
  3.    @brief 8 x 8 LED Matrix animation using Arduino.
  4.    @author Kei Takagi
  5.    @date 2016.10.2

  6.    8 x 8 Led Matrix Max7219 ... DIN D2
  7.                                 CS  D3
  8.                                 SLK D4

  9.    MIC Input                ... A4 pin

  10.    Copyright (c) 2016 Kei dTakagi
  11.    Revfleased under the MIT license
  12.    http://opensource.org/licenses/mit-license.php

  13. */

  14. #include <MsTimer2.h>
  15. #define FallVelocity 50

  16. int Max7219_pinDIN = 3;
  17. int Max7219_pinCS = 4;
  18. int Max7219_pinCLK = 5;

  19. int anime_frame = 0;
  20. int cat = 0;
  21. int ano = 0;
  22. int val = 0;
  23. int max = 0;
  24. int min = 1024;
  25. int i = 0;

  26. unsigned long time;
  27. byte layer[8];

  28. byte face[12][8] = {
  29.   {
  30.     0b01100110,
  31.     0b01100110,
  32.     0b00000000,
  33.     0b00000000,
  34.     0b00000000,
  35.     0b01111110,
  36.     0b00000000,
  37.     0b00000000,
  38.   }, {
  39.     0b01100110,
  40.     0b01100110,
  41.     0b00000000,
  42.     0b00000000,
  43.     0b00011000,
  44.     0b00100100,
  45.     0b00011000,
  46.     0b00000000,
  47.   }, {
  48.     0b01100110,
  49.     0b01100110,
  50.     0b00000000,
  51.     0b00000000,
  52.     0b00000000,
  53.     0b00011000,
  54.     0b00000000,
  55.     0b00000000,
  56.   }, {
  57.     0b01100110,
  58.     0b01100110,
  59.     0b00000000,
  60.     0b00000000,
  61.     0b01111110,
  62.     0b10000001,
  63.     0b01111110,
  64.     0b00000000,
  65.   }, {
  66.     0b01100110,
  67.     0b01100110,
  68.     0b00000000,
  69.     0b00000000,
  70.     0b00111100,
  71.     0b01000010,
  72.     0b00111100,
  73.     0b00000000,
  74.   }, {
  75.     0b01100110,
  76.     0b01100110,
  77.     0b00000000,
  78.     0b00111100,
  79.     0b01000010,
  80.     0b01000010,
  81.     0b01000010,
  82.     0b00111100,
  83.   }, {
  84.     0b01100110,
  85.     0b01100110,
  86.     0b00000000,
  87.     0b00011000,
  88.     0b00100100,
  89.     0b00100100,
  90.     0b00100100,
  91.     0b00011000,
  92.   }, {
  93.     0b01100110,
  94.     0b01100110,
  95.     0b00000000,
  96.     0b01111110,
  97.     0b10000001,
  98.     0b10000001,
  99.     0b01111110,
  100.     0b00000000,
  101.   }, {
  102.     0b01100110,
  103.     0b01100110,
  104.     0b00000000,
  105.     0b00111100,
  106.     0b01000010,
  107.     0b01000010,
  108.     0b01000010,
  109.     0b00111100,
  110.   }, {
  111.     0b01100110,
  112.     0b01100110,
  113.     0b01111110,
  114.     0b10000001,
  115.     0b10000001,
  116.     0b10000001,
  117.     0b10000001,
  118.     0b01111110,
  119.   }, {
  120.     0b01100110,
  121.     0b01100110,
  122.     0b01111110,
  123.     0b10000001,
  124.     0b10000001,
  125.     0b10000001,
  126.     0b10000001,
  127.     0b01111110,
  128.   }, {
  129.     0b01100110,
  130.     0b00000000,
  131.     0b01111110,
  132.     0b10000001,
  133.     0b10000001,
  134.     0b10000001,
  135.     0b10000001,
  136.     0b01111110,
  137.   }
  138. };

  139. void setup()
  140. {
  141.   // Init MAX7219
  142.   pinMode(Max7219_pinDIN, OUTPUT);
  143.   pinMode(Max7219_pinCS, OUTPUT);
  144.   pinMode(Max7219_pinCLK, OUTPUT);
  145.   delay(50);
  146.   Write_Max7219(0x09, 0x00);
  147.   Write_Max7219(0x0a, 0x03);
  148.   Write_Max7219(0x0b, 0x07);
  149.   Write_Max7219(0x0c, 0x01);
  150.   Write_Max7219(0x0f, 0x00);

  151.   // Timer
  152.   time = millis() +  FallVelocity;
  153.   MsTimer2::set(1, display);
  154.   MsTimer2::start();

  155.   randomSeed(analogRead(4));
  156. }

  157. void loop()
  158. {
  159.   val = 0.7 * val + 0.3 * analogRead(A4);
  160.   if (max < val)max = val;
  161.   if (min > val)min = val;
  162.   if (time < millis()) {
  163.     time = millis() +  FallVelocity;

  164.     anime_frame = map(max, min, 1023, 0, 12);
  165.     for ( i = 0; i < 8; i++) {
  166.       *(layer + i) = *(face[anime_frame] + i);
  167.     }
  168.     switch (random(0, 50) ) {
  169.       case 1:
  170.         *(layer + 0) = 0b01000100;
  171.         *(layer + 1) = 0b01000100;
  172.         break;
  173.       case 2:
  174.         *(layer + 0) = 0b00100010;
  175.         *(layer + 1) = 0b00100010;
  176.         break;
  177.       case 3:
  178.         *(layer + 0) = 0b00000000;
  179.         break;
  180.       case 4:
  181.         *(layer + 1) = 0b00000000;
  182.         break;
  183.     }
  184.     min = max;
  185.     max = 0;
  186.   }
  187. }

  188. void display()
  189. {
  190.   cat < 7 ? cat++ :  cat = 0;
  191.   Write_Max7219(cat + 1 , layer[cat]);
  192. }

  193. void Write_Max7219(unsigned char address, unsigned char dat)
  194. {
  195.   digitalWrite(Max7219_pinCS, LOW);
  196.   Write_Max7219_byte(address);
  197.   Write_Max7219_byte(dat);
  198.   digitalWrite(Max7219_pinCS, HIGH);
  199. }

  200. void Write_Max7219_byte(unsigned char DATA)
  201. {
  202.   digitalWrite(Max7219_pinCS, LOW);
  203.   for (unsigned char j = 0; j < 8; j++)
  204.   {
  205.     digitalWrite(Max7219_pinCLK, LOW);
  206.     digitalWrite(Max7219_pinDIN, (DATA << j) & 0x80);
  207.     digitalWrite(Max7219_pinCLK, HIGH);
  208.   }
  209. }

复制代码

已解决啦
回复 支持 反对

使用道具 举报

发表于 2016-11-12 22:16:55 | 显示全部楼层

楼主库文件可不可分享下,
回复 支持 反对

使用道具 举报

发表于 2016-12-13 22:30:57 | 显示全部楼层
谢谢楼主分享
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 18:34 , Processed in 0.047551 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表