回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

【雕爷学编程】Arduino动手做(99)---8X32 LED点阵屏模块4 中等

头像 驴友花雕 2023.07.29 90 1

 【花雕动手做】有趣好玩的音乐可视化系列小项目(08)---四位32段点阵屏
  项目之二:多档arduinoFFT浮点32段频谱仪

  实验开源代码
 

 

代码
/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(08)---四位32段点阵屏
  项目之二:多档arduinoFFT浮点32段频谱仪
  接脚连线:MAX9814 接A0
  MAX7219 UNO
  VCC →→→→→ 5V
  GND →→→→→ GND
  DIN →→→→→ D11(数据,数据接收引脚)
  CS →→→→→ D10(负载,命令接收引脚)
  CLK →→→→→ D13(时钟,时钟引脚)
*/

#include <arduinoFFT.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

#define SAMPLES 64
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES  4
#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10
#define  xres 32
#define  yres 8


int MY_ARRAY[] = {0, 128, 192, 224, 240, 248, 252, 254, 255};
int MY_MODE_1[] = {0, 128, 192, 224, 240, 248, 252, 254, 255};
int MY_MODE_2[] = {0, 128, 64, 32, 16, 8, 4, 2, 1};
int MY_MODE_3[] = {0, 128, 192, 160, 144, 136, 132, 130, 129};
int MY_MODE_4[] = {0, 128, 192, 160, 208, 232, 244, 250, 253};
int MY_MODE_5[] = {0, 1, 3, 7, 15, 31, 63, 127, 255};


double vReal[SAMPLES];
double vImag[SAMPLES];
char data_avgs[xres];

int yvalue;
int displaycolumn , displayvalue;
int peaks[xres];
const int buttonPin = 2;
int state = HIGH;
int previousState = LOW;
int displaymode = 1;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;


MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
arduinoFFT FFT = arduinoFFT();



void setup() {

  ADCSRA = 0b11100101;
  ADMUX = 0b00000000;
  pinMode(buttonPin, INPUT);
  mx.begin();
  delay(50);
}

void loop() {
  // ++ Sampling
  for (int i = 0; i < SAMPLES; i++)
  {
    while (!(ADCSRA & 0x10));
    ADCSRA = 0b11110101 ;
    int value = ADC - 512 ;
    vReal[i] = value / 8;
    vImag[i] = 0;
  }

  FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
  FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
  FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);

  int step = (SAMPLES / 2) / xres;
  int c = 0;
  for (int i = 0; i < (SAMPLES / 2); i += step)
  {
    data_avgs[c] = 0;
    for (int k = 0 ; k < step ; k++) {
      data_avgs[c] = data_avgs[c] + vReal[i + k];
    }
    data_avgs[c] = data_avgs[c] / step;
    c++;
  }

  for (int i = 0; i < xres; i++)
  {
    data_avgs[i] = constrain(data_avgs[i], 0, 80);
    data_avgs[i] = map(data_avgs[i], 0, 80, 0, yres);
    yvalue = data_avgs[i];

    peaks[i] = peaks[i] - 1;
    if (yvalue > peaks[i])
      peaks[i] = yvalue ;
    yvalue = peaks[i];
    displayvalue = MY_ARRAY[yvalue];
    displaycolumn = 15 - i;
    mx.setColumn(displaycolumn, displayvalue);
  }

  displayModeChange ();
}

void displayModeChange() {
  int reading = digitalRead(buttonPin);
  if (reading == HIGH && previousState == LOW && millis() - lastDebounceTime > debounceDelay)

  {

    switch (displaymode) {
      case 1:
        displaymode = 2;
        for (int i = 0 ; i <= 8 ; i++ ) {
          MY_ARRAY[i] = MY_MODE_2[i];
        }
        break;
      case 2:
        displaymode = 3;
        for (int i = 0 ; i <= 8 ; i++ ) {
          MY_ARRAY[i] = MY_MODE_3[i];
        }
        break;
      case 3:
        displaymode = 4;
        for (int i = 0 ; i <= 8 ; i++ ) {
          MY_ARRAY[i] = MY_MODE_4[i];
        }
        break;
      case 4:
        displaymode = 5;
        for (int i = 0 ; i <= 8 ; i++ ) {
          MY_ARRAY[i] = MY_MODE_5[i];
        }
        break;
      case 5:
        displaymode = 1;
        for (int i = 0 ; i <= 8 ; i++ ) {
          MY_ARRAY[i] = MY_MODE_1[i];
        }
        break;
    }

    lastDebounceTime = millis();
  }
  previousState = reading;
}

【花雕动手做】有趣好玩的音乐可视化系列小项目(08)---四位32段点阵屏
  项目之二:多档arduinoFFT浮点32段频谱仪

 

  实验视频剪辑 1
https://v.youku.com/v_show/id_XNTgyMTY3NTQwNA==.html?spm=a2hcb.playlsit.page.1

 

实验场景动态图

 

29.gif

【花雕动手做】有趣好玩的音乐可视化系列小项目(08)---四位32段点阵屏
  项目之二:多档arduinoFFT浮点32段频谱仪

 

  实验视频剪辑 7
 https://v.youku.com/v_show/id_XNTgyMjA0MTI1Mg==.html?spm=a2hcb.playlsit.page.1

 

 

 实验场景动态图7  

 

30.gif

  【花雕动手做】有趣好玩的音乐可视化系列小项目(08)---四位32段点阵屏
  项目之二:多档arduinoFFT浮点32段频谱仪

  实验视频剪辑 8
  https://v.youku.com/v_show/id_XNTgyMjA0MTM2MA==.html?spm=a2hcb.playlsit.page.1

 


实验场景动态图 8  

 

31.gif

【花雕动手做】有趣好玩的音乐可视化系列小项目(08)---四位32段点阵屏
   项目之三:红绿色32段级联频谱点阵屏灯(FFT算法)

   实验开源代码
 

代码
/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(08)---四位32段点阵屏
  项目之三:红绿色32段级联频谱点阵屏灯(FFT算法)
  接脚连线:MAX9814 接A0
  MAX7219 UNO
  VCC →→→→→ 5V
  GND →→→→→ GND
  DIN →→→→→ D11(数据,数据接收引脚)
  CS →→→→→ D10(负载,命令接收引脚)
  CLK →→→→→ D13(时钟,时钟引脚)
*/

#include <arduinoFFT.h>

#include <MD_MAX72xx.h>

#include <SPI.h>

MD_MAX72XX disp = MD_MAX72XX(MD_MAX72XX::FC16_HW, 10, 1);

arduinoFFT FFT = arduinoFFT();

double realComponent[64];

double imagComponent[64];

int spectralHeight[] = {0b00000000, 0b10000000, 0b11000000,

                        0b11100000, 0b11110000, 0b11111000,

                        0b11111100, 0b11111110, 0b11111111
                       };

int index, c, value;

void setup()

{

  disp.begin();

  Serial.begin(9600);

}

void loop()

{

  int sensitivity = map(analogRead(A0), 0, 1023, 50, 100);

  Serial.println (analogRead(A0));

  for (int i = 0; i < 64; i++)

  {

    realComponent[i] = analogRead(A7) / sensitivity;

    imagComponent[i] = 0;

  }

  FFT.Windowing(realComponent, 64, FFT_WIN_TYP_HAMMING, FFT_FORWARD);

  FFT.Compute(realComponent, imagComponent, 64, FFT_FORWARD);

  FFT.ComplexToMagnitude(realComponent, imagComponent, 64);

  for (int i = 0; i < 32; i++)

  {

    realComponent[i] = constrain(realComponent[i], 0, 80);

    realComponent[i] = map(realComponent[i], 0, 80, 0, 8);

    index = realComponent[i];

    value = spectralHeight[index];

    c = 31 - i;

    disp.setColumn(c, value);

  }

}

【花雕动手做】有趣好玩的音乐可视化系列小项目(08)---四位32段点阵屏
  项目之三:红绿色32段级联频谱点阵屏灯(FFT算法)

 

 实验视频剪辑 2

https://v.youku.com/v_show/id_XNTgyMTc1NTk2OA==.html?spm=a2hcb.playlsit.page.1

 

 

实验场景动态图 2
 

32.gif

【花雕动手做】有趣好玩的音乐可视化系列小项目
  项目之四:FFT算法的双点阵屏的动态频谱仪

 

  实验视频剪辑

https://v.youku.com/v_show/id_XNTgyMjEwNjcwMA==.html?spm=a2hcb.playlsit.page.1

 


实验场景图

 

33.gif

评论

user-avatar
  • hacker_

    hacker_2023.07.31

    666

    0
    icon 他的勋章
      展开更多