606|27

1776

帖子

0

资源

纯净的硅(中级)

【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏 [复制链接]

 

偶然心血来潮,想要做一个声音可视化的系列专题。这个专题的难度有点高,涉及面也比较广泛,相关的FFT和FHT等算法也相当复杂,不过还是打算从最简单的开始,实际动手做做试验,耐心尝试一下各种方案,逐步积累些有用的音乐频谱可视化的资料,也会争取成型一些实用好玩的音乐可视器项目。

 

 
【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
 

01.jpg

02.jpg


回复

19

帖子

1

资源

一粒金砂(中级)

感谢大佬的分享!!!感谢大佬的分享!!!

点评

早上好,也是初学者呢  详情 回复 发表于 2021-12-14 09:24

回复

1776

帖子

0

资源

纯净的硅(中级)

zgc135 发表于 2021-12-13 15:31 感谢大佬的分享!!!感谢大佬的分享!!!

早上好,也是初学者呢


回复

1776

帖子

0

资源

纯净的硅(中级)

03-.jpg

 

WS2812B主要特点
智能反接保护,电源反接不会损坏IC。
IC控制电路与LED点光源公用一个电源。
控制电路与RGB芯片集成在一个5050封装的元器件中,构成一个完整的外控像素点。
内置信号整形电路,任何一个像素点收到信号后经过波形整形再输出,保证线路波形畸变不会累加。
内置上电复位和掉电复位电路。
每个像素点的三基色颜色可实现256级亮度显示,完成16777216种颜色的全真色彩显示,扫描频率不低于400Hz/s。
串行级联接口,能通过一根信号线完成数据的接收与解码。
任意两点传传输距离在不超过5米时无需增加任何电路。
当刷新速率30帧/秒时,级联数不小于1024点。
数据发送速度可达800Kbps。
光的颜色高度一致,性价比高。

 

主要应用领域
LED全彩发光字灯串,LED全彩模组, LED全彩软灯条硬灯条,LED护栏管。
LED点光源,LED像素屏,LED异形屏,各种电子产品,电器设备跑马灯。


回复

1776

帖子

0

资源

纯净的硅(中级)

WS2812模块电原理图

 

04.jpg


回复

1776

帖子

0

资源

纯净的硅(中级)

MAX9814
是一款低成本高性能麦克风放大器,具有自动增益控制(AGC)和低噪声麦克风偏置。器件具有低噪声前端放大器、可变增益放大(VGA)、输出放大器、麦克风偏置电压发生器和AGC控制电路。
●自动增益控制(AGC)
●3种增益设置(40dB、50dB、60dB)
●可编程动作时间
●可编程动作和释放时间比
●电源电压范围2.7V~5.5V  
●低THD:0.04% (典型值)
●低功耗关断模式
●内置2V低噪声麦克风偏置

 

000.jpg


回复

2万

帖子

71

资源

管理员

期待期待 :)

点评

谢谢老师的鼓励  详情 回复 发表于 2021-12-14 20:36
个人签名

不管是哪年,都要加油!继续为中国电子行业做出小小的贡献吧! 扣扣 1206973913


回复

1776

帖子

0

资源

纯净的硅(中级)

搜索并安装Adafruit_NeoPixel库:

https://github.com/adafruit/Adafruit_NeoPixel

 

05.jpg


回复

1776

帖子

0

资源

纯净的硅(中级)

谢谢老师的鼓励


回复

1776

帖子

0

资源

纯净的硅(中级)

 【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏

  项目之一:使用Adafruit_NeoPixel库的音乐可视化多彩节奏灯

 

  实验开源代码

 

/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之一:使用Adafruit_NeoPixel库的音乐可视化多彩节奏灯
*/

#include <Adafruit_NeoPixel.h>
#define MIC A0 // 麦克风与A0相连接
#define LED_PIN 6 // LED are connected to D6
#define N_PIXELS 16 // Number of LED 
#define N 100 //样本数 
#define fadeDelay 10 // 淡出量
#define noiseLevel 40 // 降噪下限

Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);

int samples[N]; // 存储样本
int periodFactor = 0; // 用于周期计算
int t1 = -1;
int T;
int slope;
byte periodChanged = 0;

void setup() {
  // Serial.begin(9600);
  strip.begin();
  ledsOff();
  delay(500);
  displayColor(Wheel(100));
  strip.show();
  delay(500);
}

void loop() {
  Samples();
}

void Samples() {
  for (int i = 0; i < N; i++) {
    samples[i] = analogRead(0);
    if (i > 0) {
      slope = samples[i] - samples[i - 1];
    }
    else {
      slope = samples[i] - samples[N - 1];
    }

    if (abs(slope) > noiseLevel) {
      if (slope < 0) {
        calculatePeriod(i);
        if (periodChanged == 1) {
          displayColor(getColor(T));
        }
      }
    }
    else {
      ledsOff();
    }
    periodFactor += 1;
    delay(1);
  }
}

void calculatePeriod(int i) {
  if (t1 == -1) {

    t1 = i;
  }
  else {

    int period = periodFactor * (i - t1);
    periodChanged = T == period ? 0 : 1;
    T = period;
    // Serial.println(T);

    t1 = i;
    periodFactor = 0;
  }
}

uint32_t getColor(int period) {
  if (period == -1)
    return Wheel(0);
  else if (period > 400)
    return Wheel(5);
  else
    return Wheel(map(-1 * period, -400, -1, 50, 255));
}

void fadeOut()
{
  for (int i = 0; i < 5; i++) {
    strip.setBrightness(110 - i * 20);
    strip.show(); // Update strip
    delay(fadeDelay);
    periodFactor += fadeDelay;
  }
}

void fadeIn() {
  strip.setBrightness(100);
  strip.show();

  for (int i = 0; i < 5; i++) {
    //strip.setBrightness(20*i + 30);
    //strip.show();
    delay(fadeDelay);
    periodFactor += fadeDelay;
  }
}

void ledsOff() {
  fadeOut();
  for (int i = 0; i < N_PIXELS; i++) {
    strip.setPixelColor(i, 0, 0, 0);
  }
}

void displayColor(uint32_t color) {
  for (int i = 0; i < N_PIXELS; i++) {
    strip.setPixelColor(i, color);
  }
  fadeIn();
}

uint32_t Wheel(byte WheelPos) {
  // Serial.println(WheelPos);
  if (WheelPos < 85) {

    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
  else if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  else {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

 


回复

1776

帖子

0

资源

纯净的硅(中级)

实验场景图

 

06.jpg


回复

1776

帖子

0

资源

纯净的硅(中级)


 【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之一:使用Adafruit_NeoPixel库的多彩节奏灯

   实验视频剪辑

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

 


 


回复

1776

帖子

0

资源

纯净的硅(中级)

本帖最后由 eagler8 于 2021-12-15 15:20 编辑

 【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之二:音乐反应式 LED 灯板(4x4位)

 

  实验开源代码

 

/*
 【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之二:音乐反应式 LED 灯板
*/

#include <Adafruit_NeoPixel.h>
#include <math.h>
#define N_PIXELS  16
#define MIC_PIN   A0
#define LED_PIN    6
#define SAMPLE_WINDOW  4
#define PEAK_HANG 24
#define PEAK_FALL 4
#define INPUT_FLOOR 10
#define INPUT_CEILING 50
byte peak = 16;
unsigned int sample;

byte Count = 0;
byte HangCount = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(9600);
  analogReference(EXTERNAL);
  strip.setBrightness(22);
  strip.show();
  strip.begin();
}

float fscale( float originalMin, float originalMax, float newBegin, float newEnd, float inputValue, float curve) {

  float OriginalRange = 0;
  float NewRange = 0;
  float zeroRefCurVal = 0;
  float normalizedCurVal = 0;
  float rangedValue = 0;
  boolean invFlag = 0;

  if (curve > 10) curve = 10;
  if (curve < -10) curve = -10;

  curve = (curve * -.1) ;
  curve = pow(10, curve);

  if (inputValue < originalMin) {
    inputValue = originalMin;
  }
  if (inputValue > originalMax) {
    inputValue = originalMax;
  }

  OriginalRange = originalMax - originalMin;

  if (newEnd > newBegin) {
    NewRange = newEnd - newBegin;
  }
  else
  {
    NewRange = newBegin - newEnd;
    invFlag = 1;
  }

  zeroRefCurVal = inputValue - originalMin;
  normalizedCurVal  =  zeroRefCurVal / OriginalRange;   // normalize to 0 - 1 float

  Serial.print(OriginalRange, DEC);
  Serial.print("   ");
  Serial.print(NewRange, DEC);
  Serial.print("   ");
  Serial.println(zeroRefCurVal, DEC);
  Serial.println();
  delay(10);

  if (originalMin > originalMax ) {
    return 0;
  }

  if (invFlag == 0) {
    rangedValue =  (pow(normalizedCurVal, curve) * NewRange) + newBegin;
  }
  else
  {
    rangedValue =  newBegin - (pow(normalizedCurVal, curve) * NewRange);
  }
  return rangedValue;
}

void loop() {
  unsigned long startMillis = millis();
  float peakToPeak = 0;

  unsigned int signalMax = 0;
  unsigned int signalMin = 1023;
  unsigned int c, y;

  while (millis() - startMillis < SAMPLE_WINDOW)
  {
    sample = analogRead(MIC_PIN);
    if (sample < 1024)
    {
      if (sample > signalMax)
      {
        signalMax = sample;
      }
      else if (sample < signalMin)
      {
        signalMin = sample;
      }
    }
  }
  peakToPeak = signalMax - signalMin;

  for (int i = 0; i <= strip.numPixels() - 1; i++) {
    strip.setPixelColor(i, Wheel(map(i, 0, strip.numPixels() - 1, 30, 150)));
  }

  c = fscale(INPUT_FLOOR, INPUT_CEILING, strip.numPixels(), 0, peakToPeak, 2);

  if (c < peak) {
    peak = c;
    HangCount = 0;
  }
  if (c <= strip.numPixels()) {
    drawLine(strip.numPixels(), strip.numPixels() - c, strip.Color(0, 0, 0));
  }

  y = strip.numPixels() - peak;
  strip.setPixelColor(y - 1, Wheel(map(y, 0, strip.numPixels() - 1, 30, 150)));
  strip.show();

  if (HangCount > PEAK_HANG) {
    if (++Count >= PEAK_FALL) {
      peak++;
      Count = 0;
    }
  }
  else {
    HangCount++;
  }
}

void drawLine(uint8_t from, uint8_t to, uint32_t c) {
  uint8_t fromTemp;
  if (from > to) {
    fromTemp = from;
    from = to;
    to = fromTemp;
  }
  for (int i = from; i <= to; i++) {
    strip.setPixelColor(i, c);
  }
}

uint32_t Wheel(byte WheelPos) {
  if (WheelPos < 85) {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
  else if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  else {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

 


回复

1776

帖子

0

资源

纯净的硅(中级)

 【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏

  项目之二:音乐反应式 LED 灯板(4x4位)

 

  实验视频剪辑

 

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

 


 


回复

1776

帖子

0

资源

纯净的硅(中级)

  实验场动态图 

 

135.gif


回复

1776

帖子

0

资源

纯净的硅(中级)

 【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏

  项目之三:六十四位闪动音乐频谱灯(8x8WS2812硬屏)

 

  实验开源代码

 

/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之三:六十四位闪动音乐频谱灯(8x8WS2812硬屏)
*/

#include "FastLED.h"
#define OCTAVE 1 //   // Group buckets into octaves  (use the log output function LOG_OUT 1)
#define OCT_NORM 0 // Don't normalise octave intensities by number of bins
#define FHT_N 256 // set to 256 point fht
#include <FHT.h> // include the library
//int noise[] = {204,188,68,73,150,98,88,68}; // noise level determined by playing pink noise and seeing levels [trial and error]{204,188,68,73,150,98,88,68}


// int noise[] = {204,190,108,85,65,65,55,60}; // noise for mega adk
int noise[] = {204, 195, 100, 90, 85, 80, 75, 75}; // noise for NANO
//int noise[] = {204,198,100,85,85,80,80,80};
float noise_fact[] = {15, 7, 1.5, 1, 1.2, 1.4, 1.7, 3}; // noise level determined by playing pink noise and seeing levels [trial and error]{204,188,68,73,150,98,88,68}
float noise_fact_adj[] = {15, 7, 1.5, 1, 1.2, 1.4, 1.7, 3}; // noise level determined by playing pink noise and seeing levels [trial and error]{204,188,68,73,150,98,88,68}

#define LED_PIN     6
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB

// Params for width and height
const uint8_t kMatrixWidth = 8;
const uint8_t kMatrixHeight = 8;//----------was 27
//#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
#define NUM_LEDS    64

CRGB leds[NUM_LEDS];

int counter2 = 0;



void setup() {
  Serial.begin(9600);
  delay(1000);
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

  FastLED.setBrightness (22);
  fill_solid(leds, NUM_LEDS, CRGB::Black);
  FastLED.show();
  // TIMSK0 = 0; // turn off timer0 for lower jitter
  ADCSRA = 0xe5; // set the adc to free running mode
  ADMUX = 0x40; // use adc0
  DIDR0 = 0x01; // turn off the digital input for adc0
}

void loop() {
  int prev_j[8];
  int beat = 0;
  int prev_oct_j;
  int counter = 0;
  int prev_beat = 0;
  int led_index = 0;
  int saturation = 0;
  int saturation_prev = 0;
  int brightness = 0;
  int brightness_prev = 0;

  while (1) { // reduces jitter
    cli();  // UDRE interrupt slows this way down on arduino1.0
    for (int i = 0 ; i < FHT_N ; i++) { // save 256 samples
      while (!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j << 8) | m; // form into an int
      k -= 0x0200; // form into a signed int
      k <<= 6; // form into a 16b signed int
      fht_input[i] = k; // put real data into bins
    }
    fht_window(); // window the data for better frequency response
    fht_reorder(); // reorder the data before doing the fht
    fht_run(); // process the data in the fht
    fht_mag_octave(); // take the output of the fht  fht_mag_log()

    // every 50th loop, adjust the volume accourding to the value on A2 (Pot)
    if (counter >= 50) {
      ADMUX = 0x40 | (1 & 0x07); // set admux to look at Analogpin A1 - Master Volume

      while (!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc
      delay(10);
      while (!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j << 8) | m; // form into an int
      float master_volume = (k + 0.1) / 1000 + .75; // so the valu will be between ~0.5 and 1.---------------------+.75 was .5
      Serial.println (master_volume);

      for (int i = 1; i < 8; i++) {
        noise_fact_adj[i] = noise_fact[i] * master_volume;
      }

      ADMUX = 0x40 | (0 & 0x07); // set admux back to look at A0 analog pin (to read the microphone input
      counter = 0;
    }

    sei();
    counter++;
    // End of Fourier Transform code - output is stored in fht_oct_out[i].
    // i=0-7 frequency (octave) bins (don't use 0 or 1), fht_oct_out[1]= amplitude of frequency for bin 1
    // for loop a) removes background noise average and takes absolute value b) low / high pass filter as still very noisy
    // c) maps amplitude of octave to a colour between blue and red d) sets pixel colour to amplitude of each frequency (octave)

    for (int i = 1; i < 8; i++) {  // goes through each octave. skip the first 1, which is not useful

      int j;
      j = (fht_oct_out[i] - noise[i]); // take the pink noise average level out, take the asbolute value to avoid negative numbers
      if (j < 10) {
        j = 0;
      }
      j = j * noise_fact_adj[i];

      if (j < 10) {
        j = 0;
      }
      else {
        j = j * noise_fact_adj[i];
        if (j > 180) {
          if (i >= 7) {
            beat += 2;
          }
          else {
            beat += 1;
          }
        }
        j = j / 30;
        j = j * 30; // (force it to more discrete values)
      }

      prev_j[i] = j;

      //     Serial.print(j);
      //     Serial.print(" ");


      // this fills in 11 LED's with interpolated values between each of the 8 OCT values
      if (i >= 2) {
        led_index = 2 * i - 3;
        prev_oct_j = (j + prev_j[i - 1]) / 2;

        saturation = constrain(j + 50, 0, 255); //-----------50 was 30
        saturation_prev = constrain(prev_oct_j + 50, 0, 255);
        brightness = constrain(j, 0, 255);
        brightness_prev = constrain(prev_oct_j, 0, 255);
        if (brightness == 255) {
          saturation = 50;
          brightness = 200;
        }
        if (brightness_prev == 255) {
          saturation_prev = 50;
          brightness_prev = 200;
        }


        for (uint8_t y = 0; y < kMatrixHeight; y++) {
          leds[XY(led_index - 1, y)] = CHSV(j + y * 30, saturation, brightness);
          if (i > 2) {
            prev_oct_j = (j + prev_j[i - 1]) / 2;
            leds[ XY(led_index - 2, y)] = CHSV(prev_oct_j + y * 30, saturation_prev, brightness_prev);
          }
        }
      }
    }

    if (beat >= 7) {
      fill_solid(leds, NUM_LEDS, CRGB::Gray);
      FastLED.setBrightness(200);

    }
    else {
      if (prev_beat != beat) {
        FastLED.setBrightness(40 + beat * beat * 5);
        prev_beat = beat;
      }
    }

    FastLED.show();
    if (beat) {
      counter2 += ((beat + 4) / 2 - 2);
      if (counter2 < 0) {
        counter2 = 1000;
      }
      if (beat > 3 && beat < 7) {
        FastLED.delay (20);
      }
      beat = 0;
    }
    // Serial.println();
  }
}

// Param for different pixel layouts
const bool    kMatrixSerpentineLayout = false;
// Set 'kMatrixSerpentineLayout' to false if your pixels are
// laid out all running the same way, like this:
// Set 'kMatrixSerpentineLayout' to true if your pixels are
// laid out back-and-forth, like this:

uint16_t XY( uint8_t x, uint8_t y)
{
  uint16_t i;

  if ( kMatrixSerpentineLayout == false) {
    i = (y * kMatrixWidth) + x;
  }

  if ( kMatrixSerpentineLayout == true) {
    if ( y & 0x01) {
      // Odd rows run backwards
      uint8_t reverseX = (kMatrixWidth - 1) - x;
      i = (y * kMatrixWidth) + reverseX;
    } else {
      // Even rows run forwards
      i = (y * kMatrixWidth) + x;
    }
  }

  i = (i + counter2) % NUM_LEDS;
  return i;
}

 


回复

1776

帖子

0

资源

纯净的硅(中级)

【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏

  项目之三:六十四位闪动音乐频谱灯(8x8位WS2812硬屏)

 

  实验视频剪辑

 

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

 


 


回复

1776

帖子

0

资源

纯净的硅(中级)

  实验场景动态图 

 

 

136.gif


回复

1776

帖子

0

资源

纯净的硅(中级)

 【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏

  项目之四:256位全彩闪动音乐频谱灯(8x32位WS2812硬屏)

 

  实验开源代码

 

/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之三:六十四位闪动音乐频谱灯(8x8WS2812硬屏)
*/

#include "FastLED.h"
#define OCTAVE 1 //   // Group buckets into octaves  (use the log output function LOG_OUT 1)
#define OCT_NORM 0 // Don't normalise octave intensities by number of bins
#define FHT_N 256 // set to 256 point fht
#include <FHT.h> // include the library
//int noise[] = {204,188,68,73,150,98,88,68}; // noise level determined by playing pink noise and seeing levels [trial and error]{204,188,68,73,150,98,88,68}

// int noise[] = {204,190,108,85,65,65,55,60}; // noise for mega adk
int noise[] = {204, 195, 100, 90, 85, 80, 75, 75}; // noise for NANO
//int noise[] = {204,198,100,85,85,80,80,80};
float noise_fact[] = {15, 7, 1.5, 1, 1.2, 1.4, 1.7, 3}; // noise level determined by playing pink noise and seeing levels [trial and error]{204,188,68,73,150,98,88,68}
float noise_fact_adj[] = {15, 7, 1.5, 1, 1.2, 1.4, 1.7, 3}; // noise level determined by playing pink noise and seeing levels [trial and error]{204,188,68,73,150,98,88,68}

#define LED_PIN     6
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB

// Params for width and height
const uint8_t kMatrixWidth = 8;
const uint8_t kMatrixHeight = 32;//----------was 27
//#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
#define NUM_LEDS    256

CRGB leds[NUM_LEDS];

int counter2 = 0;

void setup() {
  Serial.begin(9600);
  delay(1000);
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

  FastLED.setBrightness (6);
  fill_solid(leds, NUM_LEDS, CRGB::Black);
  FastLED.show();
  // TIMSK0 = 0; // turn off timer0 for lower jitter
  ADCSRA = 0xe5; // set the adc to free running mode
  ADMUX = 0x40; // use adc0
  DIDR0 = 0x01; // turn off the digital input for adc0
}

void loop() {
  int prev_j[8];
  int beat = 0;
  int prev_oct_j;
  int counter = 0;
  int prev_beat = 0;
  int led_index = 0;
  int saturation = 0;
  int saturation_prev = 0;
  int brightness = 0;
  int brightness_prev = 0;

  while (1) { // reduces jitter
    cli();  // UDRE interrupt slows this way down on arduino1.0
    for (int i = 0 ; i < FHT_N ; i++) { // save 256 samples
      while (!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j << 8) | m; // form into an int
      k -= 0x0200; // form into a signed int
      k <<= 6; // form into a 16b signed int
      fht_input[i] = k; // put real data into bins
    }
    fht_window(); // window the data for better frequency response
    fht_reorder(); // reorder the data before doing the fht
    fht_run(); // process the data in the fht
    fht_mag_octave(); // take the output of the fht  fht_mag_log()

    // every 50th loop, adjust the volume accourding to the value on A2 (Pot)
    if (counter >= 50) {
      ADMUX = 0x40 | (1 & 0x07); // set admux to look at Analogpin A1 - Master Volume

      while (!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc
      delay(10);
      while (!(ADCSRA & 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j << 8) | m; // form into an int
      float master_volume = (k + 0.1) / 1000 + .75; // so the valu will be between ~0.5 and 1.---------------------+.75 was .5
      Serial.println (master_volume);

      for (int i = 1; i < 8; i++) {
        noise_fact_adj[i] = noise_fact[i] * master_volume;
      }

      ADMUX = 0x40 | (0 & 0x07); // set admux back to look at A0 analog pin (to read the microphone input
      counter = 0;
    }

    sei();
    counter++;
    // End of Fourier Transform code - output is stored in fht_oct_out[i].
    // i=0-7 frequency (octave) bins (don't use 0 or 1), fht_oct_out[1]= amplitude of frequency for bin 1
    // for loop a) removes background noise average and takes absolute value b) low / high pass filter as still very noisy
    // c) maps amplitude of octave to a colour between blue and red d) sets pixel colour to amplitude of each frequency (octave)

    for (int i = 1; i < 8; i++) {  // goes through each octave. skip the first 1, which is not useful

      int j;
      j = (fht_oct_out[i] - noise[i]); // take the pink noise average level out, take the asbolute value to avoid negative numbers
      if (j < 10) {
        j = 0;
      }
      j = j * noise_fact_adj[i];

      if (j < 10) {
        j = 0;
      }
      else {
        j = j * noise_fact_adj[i];
        if (j > 180) {
          if (i >= 7) {
            beat += 2;
          }
          else {
            beat += 1;
          }
        }
        j = j / 30;
        j = j * 30; // (force it to more discrete values)
      }

      prev_j[i] = j;

      //     Serial.print(j);
      //     Serial.print(" ");


      // this fills in 11 LED's with interpolated values between each of the 8 OCT values
      if (i >= 2) {
        led_index = 2 * i - 3;
        prev_oct_j = (j + prev_j[i - 1]) / 2;

        saturation = constrain(j + 50, 0, 255); //-----------50 was 30
        saturation_prev = constrain(prev_oct_j + 50, 0, 255);
        brightness = constrain(j, 0, 255);
        brightness_prev = constrain(prev_oct_j, 0, 255);
        if (brightness == 255) {
          saturation = 50;
          brightness = 200;
        }
        if (brightness_prev == 255) {
          saturation_prev = 50;
          brightness_prev = 200;
        }


        for (uint8_t y = 0; y < kMatrixHeight; y++) {
          leds[XY(led_index - 1, y)] = CHSV(j + y * 30, saturation, brightness);
          if (i > 2) {
            prev_oct_j = (j + prev_j[i - 1]) / 2;
            leds[ XY(led_index - 2, y)] = CHSV(prev_oct_j + y * 30, saturation_prev, brightness_prev);
          }
        }
      }
    }

    if (beat >= 7) {
      fill_solid(leds, NUM_LEDS, CRGB::Gray);
      FastLED.setBrightness(200);

    }
    else {
      if (prev_beat != beat) {
        FastLED.setBrightness(40 + beat * beat * 5);
        prev_beat = beat;
      }
    }

    FastLED.show();
    if (beat) {
      counter2 += ((beat + 4) / 2 - 2);
      if (counter2 < 0) {
        counter2 = 1000;
      }
      if (beat > 3 && beat < 7) {
        FastLED.delay (20);
      }
      beat = 0;
    }
    // Serial.println();
  }
}

// Param for different pixel layouts
const bool    kMatrixSerpentineLayout = false;
// Set 'kMatrixSerpentineLayout' to false if your pixels are
// laid out all running the same way, like this:
// Set 'kMatrixSerpentineLayout' to true if your pixels are
// laid out back-and-forth, like this:

uint16_t XY( uint8_t x, uint8_t y)
{
  uint16_t i;

  if ( kMatrixSerpentineLayout == false) {
    i = (y * kMatrixWidth) + x;
  }

  if ( kMatrixSerpentineLayout == true) {
    if ( y & 0x01) {
      // Odd rows run backwards
      uint8_t reverseX = (kMatrixWidth - 1) - x;
      i = (y * kMatrixWidth) + reverseX;
    } else {
      // Even rows run forwards
      i = (y * kMatrixWidth) + x;
    }
  }

  i = (i + counter2) % NUM_LEDS;
  return i;
}

 


回复

1776

帖子

0

资源

纯净的硅(中级)

  实验场景图

 

07.jpg


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

查找数据手册?

EEWorld Datasheet 技术支持

最新文章 更多>>
    关闭
    站长推荐上一条 1/6 下一条

    About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

    站点相关: 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

    北京市海淀区知春路23号集成电路设计园量子银座1305 电话:(010)82350740 邮编:100191

    电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2022 EEWORLD.com.cn, Inc. All rights reserved
    快速回复 返回顶部 返回列表