Arduino爱好者

 找回密码
 立即注册

QQ登录

只需一步,快速开始

楼主: eagler8

[经验] 【Arduino】108种传感器系列实验(22)---MAX7219点阵模块

[复制链接]
 楼主| 发表于 2021-9-30 12:52 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二十二:MAX7219点阵显示模块(8X8 LED共阴)
  项目三十:根据来自加速度计的相应输出,来确定 LED 的移动

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二十二:MAX7219点阵显示模块(8X8 LED共阴)
  4.   项目三十:根据来自加速度计的相应输出,来确定 LED 的移动
  5.   接脚连线:
  6.   MAX7219       UNO
  7.   VCC  →→→→→ 5V
  8.   GND  →→→→→ GND
  9.   DIN  →→→→→ D12(数据,数据接收引脚)
  10.   CS   →→→→→ D11(负载,命令接收引脚)
  11.   CLK  →→→→→ D10(时钟,时钟引脚)
  12. */

  13. #include <LedControl.h>
  14. #include <Wire.h>

  15. #define DEVICE (0x53)   //ADXL345 device address
  16. #define TO_READ (6)     //num of bytes we are going to read (two bytes for each axis)

  17. byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device
  18. char str[512];          //string buffer to transform data before sending it

  19. int MATRIX_WIDTH = 8;
  20. LedControl lc = LedControl(12, 10, 11, 1); // DIN, CLK, CS, NRDEV
  21. unsigned long delaytime = 50;
  22. int x_key = A1;
  23. int y_key = A0;
  24. int x_pos;
  25. int y_pos;

  26. // object that represents a single light location
  27. // future update with gravity
  28. class Grain
  29. {
  30.   public:
  31.     int x = 0;
  32.     int y = 0;
  33.     int mass = 1;
  34. };
  35. Grain *g;

  36. void setup()
  37. {
  38.   // set up a grain object
  39.   g = new Grain();

  40.   ClearDisplay();

  41.   Wire.begin();        // join i2c bus (address optional for master)
  42.   Serial.begin(9600);  // start serial for output

  43.   //Turning on the ADXL345
  44.   writeTo(DEVICE, 0x2D, 0);
  45.   writeTo(DEVICE, 0x2D, 16);
  46.   writeTo(DEVICE, 0x2D, 8);
  47. }

  48. void loop()
  49. {
  50.   // The first axis-acceleration-data register
  51.   int regAddress = 0x32;
  52.   int x, y, z;

  53.   readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from ADXL345

  54.   // Combine the two bytes of each direction
  55.   // Least significant bit first
  56.   x = (((int)buff[1]) << 8) | buff[0];
  57.   y = (((int)buff[3]) << 8) | buff[2];
  58.   z = (((int)buff[5]) << 8) | buff[4];

  59.   // Convert the values into values that can be represented on the matrix
  60.   x = map(x, -300, 300, 0, 8);
  61.   y = map(y, -300, 300, 0, 8);
  62.   z = map(z, -300, 300, 0, 8);

  63.   //we send the x y z values as a string to the serial port
  64.   Serial.print("X: ");
  65.   Serial.print(x);
  66.   Serial.print("   Y: ");
  67.   Serial.print(y);
  68.   Serial.print("   Z: ");
  69.   Serial.print(z);
  70.   Serial.print("\n");

  71.   ClearDisplay();
  72.   // assign the grain to this location
  73.   g->x = x;
  74.   g->y = y;
  75.   lc.setLed(0, g->x, g->y, true);

  76.   //add some delay between each update
  77.   delay(10);
  78. }

  79. void ClearDisplay()
  80. {
  81.   // sets up the lcd display
  82.   int devices = lc.getDeviceCount();

  83.   for (int address = 0; address < devices; address++)
  84.   {
  85.     lc.shutdown(address, false);
  86.     lc.setIntensity(address, 1);
  87.     lc.clearDisplay(address);
  88.   }
  89. }

  90. //Writes val to address register on device
  91. void writeTo(int device, byte address, byte val)
  92. {
  93.   Wire.beginTransmission(device); //start transmission to device
  94.   Wire.write(address);        // send register address
  95.   Wire.write(val);        // send value to write
  96.   Wire.endTransmission(); //end transmission
  97. }

  98. //reads num bytes starting from address register on device in to buff array
  99. void readFrom(int device, byte address, int num, byte buff[])
  100. {
  101.   Wire.beginTransmission(device); //start transmission to device
  102.   Wire.write(address);        //sends address to read from
  103.   Wire.endTransmission(); //end transmission

  104.   Wire.beginTransmission(device); //start transmission to device
  105.   Wire.requestFrom(device, num);    // request 6 bytes from device

  106.   int i = 0;
  107.   while (Wire.available())   //device may send less than requested (abnormal)
  108.   {
  109.     buff[i] = Wire.read(); // receive a byte
  110.     i++;
  111.   }
  112.   Wire.endTransmission(); //end transmission
  113. }
复制代码


 楼主| 发表于 2021-9-30 12:57 | 显示全部楼层
模块实验接线示意图

28.jpg
 楼主| 发表于 2021-9-30 13:59 | 显示全部楼层
实验串口返回情况

29.jpg
 楼主| 发表于 2021-9-30 14:04 | 显示全部楼层
  实验场景图

30.jpg
 楼主| 发表于 2021-9-30 15:14 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验四十八:三轴ADXL345模块GY-291数字重力加速度倾斜度(IIC/SPI传输)
  项目之九:点阵屏陀螺仪

  实验视频剪辑

https://v.youku.com/v_show/id_XNTgxMDI1NTc3Ng==.html?firsttime=0





 楼主| 发表于 2021-10-1 13:21 | 显示全部楼层

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二十二:MAX7219点阵显示模块(8X8 LED共阴)
  项目三十一:动态音乐频谱仪

  实验开源代码


  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二十二:MAX7219点阵显示模块(8X8 LED共阴)
  4.   项目三十一:动态音乐频谱仪
  5.   接脚连线:
  6.   MAX7219       UNO
  7.   VCC  →→→→→ 5V
  8.   GND  →→→→→ GND
  9.   DIN  →→→→→ D12(数据,数据接收引脚)
  10.   CS   →→→→→ D11(负载,命令接收引脚)
  11.   CLK  →→→→→ D10(时钟,时钟引脚)
  12. */

  13. #include "LedControl.h"

  14. /* Led matrix - Max7219 Declared */
  15. LedControl lc = LedControl(12, 11, 10, 1);

  16. const int maxScale = 11;

  17. /*  Sensor - Max9812 Declared */
  18. const int sensorPin = A4;
  19. const int sampleWindow = 50;  // 50ms = 20Hz
  20. unsigned int sample;

  21. unsigned long startMillis;
  22. unsigned long timeCycle;

  23. unsigned int signalMax = 0;
  24. unsigned int signalMin = 1024;
  25. unsigned char index = 0;

  26. unsigned int peakToPeak[8];
  27. unsigned int displayPeak[8];
  28. unsigned int temp[8]={0,0,0,0,0,0,0,0};
  29. unsigned int signalMaxBuff[8];
  30. unsigned int signalMinBuff[8];


  31. void setup() {
  32.   // Led matrix
  33.   lc.shutdown(0, false); // bật hiện thị
  34.   lc.setIntensity(0, 1); // chỉnh độ sáng
  35.   lc.clearDisplay(0); // tắt tất cả led

  36.   Serial.begin(9600);
  37. }

  38. void loop() {
  39.   startMillis = millis();
  40.   //peakToPeak = 0;

  41.   signalMax = 0;
  42.   signalMin = 1024;
  43.   
  44.   // Get data in 50ms
  45.   while (millis() - startMillis < sampleWindow) {
  46.     sample = analogRead(sensorPin);
  47.    
  48.     if (sample < 1024) {
  49.       if (sample > signalMax) {
  50.         signalMax = sample;
  51.       }
  52.       if (sample < signalMin) {
  53.         signalMin = sample;
  54.       }
  55.     }

  56.     // 20Hz - 64Hz - 125Hz - 250Hz - 500Hz - 1kHz (timeCycle = 1/F)(ms)
  57.     timeCycle = millis() - startMillis;
  58.     if (timeCycle == 1 || timeCycle == 2 || timeCycle == 4 || timeCycle == 8
  59.         || timeCycle == 16 || timeCycle == 32 || timeCycle == 40 || timeCycle == 50) {
  60.               signalMaxBuff[index] = signalMax;
  61.               signalMinBuff[index] = signalMin;
  62.               index = (index + 1) % 8;
  63.               delay(1);
  64.               //Serial.println(timeCycle);
  65.     }
  66.   }

  67.   // Delete pointer to array
  68.   index = 0;

  69.   // Calculation after get samples
  70.   for (int i = 0; i < 8; i++) {  // i = row (led matrix)
  71.     // sound level
  72.     peakToPeak[i] = signalMaxBuff[i] - signalMinBuff[i];
  73.    
  74.     // Map 1v p-p level to the max scale of the display
  75.     displayPeak[i] = map(peakToPeak[i], 0, 1023, 0, maxScale);

  76.     // Show to led matrix
  77.     displayLed(displayPeak[i], i);
  78.    
  79.     // Led drop down
  80.     if (displayPeak[i] >= temp[i]) {
  81.       temp[i] = displayPeak[i];
  82.     }
  83.     else {
  84.       temp[i]--;
  85.     }
  86.    
  87.     lc.setLed(0, i, temp[i], true);
  88.     delayMicroseconds(250);
  89.   }
  90.    
  91. }

  92. void displayLed(int displayPeak, int row) {
  93.   switch (displayPeak) {
  94.     case 0 : lc.setRow(0, row, 0x80); break;
  95.     case 1 : lc.setRow(0, row, 0xC0); break;
  96.     case 2 : lc.setRow(0, row, 0xE0); break;
  97.     case 3 : lc.setRow(0, row, 0xF0); break;
  98.     case 4 : lc.setRow(0, row, 0xF8); break;
  99.     case 5 : lc.setRow(0, row, 0xFC); break;
  100.     case 6 : lc.setRow(0, row, 0xFE); break;
  101.     case 7 : lc.setRow(0, row, 0xFF); break;
  102.   }
  103. }
复制代码


 楼主| 发表于 2021-10-1 13:29 | 显示全部楼层
  实验场景图
21.jpg
 楼主| 发表于 2021-10-1 13:49 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二十二:MAX7219点阵显示模块(8X8 LED共阴)
  项目三十一:动态音乐频谱仪

  实验视频剪辑

https://v.youku.com/v_show/id_XN ... hcb.playlsit.page.1



 楼主| 发表于 2021-10-1 16:27 | 显示全部楼层
  实验场景图 动态图

07.gif
 楼主| 发表于 2021-10-12 15:45 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二十二:MAX7219点阵显示模块(8X8 LED共阴)
  项目三十二:使用 8x8 LED 矩阵和 MAX 模块实现条形图功能

   实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二十二:MAX7219点阵显示模块(8X8 LED共阴)
  4.   项目三十二:使用 8x8 LED 矩阵和 MAX 模块实现条形图功能
  5.   接脚连线:
  6.   MAX7219       UNO
  7.   VCC  →→→→→ 5V
  8.   GND  →→→→→ GND
  9.   DIN  →→→→→ D12(数据,数据接收引脚)
  10.   CS   →→→→→ D11(负载,命令接收引脚)
  11.   CLK  →→→→→ D10(时钟,时钟引脚)
  12. */

  13. #include "LedControl.h"

  14. LedControl lc = LedControl(12, 10, 11, 1);

  15. void setup() {
  16.   /*
  17.     MAX72XX 在启动时处于省电模式,
  18.     我们必须叫醒
  19.   */
  20.   lc.shutdown(0, false);
  21.   lc.setIntensity(0, 0); //将亮度设置为0(变暗), 8 是中等
  22.   lc.clearDisplay(0);    //清除显示
  23. }

  24. void loop() {
  25.   int val =  random(0, 9);
  26.   BarScroll(val); //将此值插入条形图中
  27.   delay(100);
  28. }
  29. int bar[8];

  30. void BarScroll(int NewVal) {
  31.   for (int k = 0; k < 7; k++) { //为新的val腾出空间
  32.     bar[k] = bar[k + 1];
  33.   }
  34.   bar[7] = NewVal; //安装新的val
  35.   BarGraph(bar);
  36. }

  37. void BarGraph(int barvals[8]) {
  38.   byte a[9] = {B00000000, B10000000, B11000000, B11100000, B11110000, B11111000, B11111100, B11111110, B11111111};
  39.   for (int k = 0; k < 8; k++) {  //做第k列
  40.     lc.setRow(0, k, a[barvals[k]]); //立即创建列
  41.   }
  42. }
复制代码


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

本版积分规则

小黑屋|Archiver|手机版|好玩手机游戏盒子|196体育|Arduino爱好者

GMT+8, 2023-6-4 09:02 , Processed in 0.075969 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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