好奇吖斌 发表于 2016-8-19 01:35:13

max7219LED假唱机器人程序

最近在一个创客群里发现有人做了个假唱机器人,用的是DFrobot的8*8点阵LED,我又在instructables上发现有这个项目,估计群里的那个也是看这个做出来的,instructables上的是直接焊,我有一块Max7219的点阵LED,想把它程序改成Max7219的,可是instructables上的程序有点看不懂,请教一下怎么改,http://www.instructables.com/id/Lip-Synching-Robot/

/*

   @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 = { -1, 9, 8, 7, 6, 5, 4, 3, 2, 17, 16, 15, 14, 13, 12, 11, 10};
const static byte cols = {pins, pins[ 3], pins[ 4], pins, pins[ 6], pins, pins, pins};
const static byte rows = {pins[ 9], pins, pins[ 8], pins, 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;

byte face = {
{
    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, OUTPUT );
    digitalWrite( rows, HIGH );
}
for ( ano = 0; ano < 8; ano++ ) {
    pinMode( cols, OUTPUT );
    digitalWrite( cols, 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 + 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 );
}




以下是我根据上面改出来的,可是吐舌头那段程序看不懂for ( i = 0; i < 8; i++) {
      *(layer + i) = *(face + 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;
    }

#include <LedControl.h>

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

const byte IMAGES[] = {
{
    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);
}

pumpitup 发表于 2016-8-19 11:23:42

感谢分享,学习学习

好奇吖斌 发表于 2016-11-10 19:43:13

/*

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

   8 x 8 Led Matrix Max7219 ... DIN D2
                              CSD3
                              SLK D4

   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 50

int Max7219_pinDIN = 3;
int Max7219_pinCS = 4;
int Max7219_pinCLK = 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;

byte face = {
{
    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()
{
// Init MAX7219
pinMode(Max7219_pinDIN, OUTPUT);
pinMode(Max7219_pinCS, OUTPUT);
pinMode(Max7219_pinCLK, OUTPUT);
delay(50);
Write_Max7219(0x09, 0x00);
Write_Max7219(0x0a, 0x03);
Write_Max7219(0x0b, 0x07);
Write_Max7219(0x0c, 0x01);
Write_Max7219(0x0f, 0x00);

// Timer
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 + 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;
Write_Max7219(cat + 1 , layer);
}

void Write_Max7219(unsigned char address, unsigned char dat)
{
digitalWrite(Max7219_pinCS, LOW);
Write_Max7219_byte(address);
Write_Max7219_byte(dat);
digitalWrite(Max7219_pinCS, HIGH);
}

void Write_Max7219_byte(unsigned char DATA)
{
digitalWrite(Max7219_pinCS, LOW);
for (unsigned char j = 0; j < 8; j++)
{
    digitalWrite(Max7219_pinCLK, LOW);
    digitalWrite(Max7219_pinDIN, (DATA << j) & 0x80);
    digitalWrite(Max7219_pinCLK, HIGH);
}
}


已解决啦

死磕单片机 发表于 2016-11-12 22:16:55

好奇吖斌 发表于 2016-11-10 19:43
已解决啦

楼主库文件可不可分享下,

单片机菜鸟 发表于 2016-12-13 22:30:57

谢谢楼主分享
页: [1]
查看完整版本: max7219LED假唱机器人程序