回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页
best-icon

【花雕动手做】有趣好玩的音乐可视化系列小项目(13)---有机棒立柱灯 简单

头像 驴友花雕 2022.07.25 383 9

造物记


【花雕动手做】有趣好玩的音乐可视化项目(01)---LED节奏灯
https://makelog.dfrobot.com.cn/article-311363.html
【花雕动手做】有趣好玩的音乐可视化项目(02)---OLED频谱灯
https://makelog.dfrobot.com.cn/article-311365.html
【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯
https://makelog.dfrobot.com.cn/article-311366.html
【花雕动手做】有趣好玩的音乐可视化项目(04)---WS2812条灯
https://makelog.dfrobot.com.cn/article-311377.html
【花雕动手做】有趣好玩的音乐可视化项目(05)---WS2812柱跳灯
https://makelog.dfrobot.com.cn/article-311378.html
【花雕动手做】有趣好玩的音乐可视化项目(06)---点阵频谱灯
https://makelog.dfrobot.com.cn/article-311379.html
【花雕动手做】有趣好玩的音乐可视化系列小项目(07)---大方格频谱灯
https://makelog.dfrobot.com.cn/article-311429.html
【花雕动手做】有趣好玩的音乐可视化系列小项目(08)---四位32段点阵屏
https://makelog.dfrobot.com.cn/article-311463.html

【花雕动手做】有趣好玩的音乐可视化系列小项目(09)---X Music Spectrum
https://makelog.dfrobot.com.cn/article-311482.html

【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
https://makelog.dfrobot.com.cn/article-311491.html
【花雕动手做】有趣好玩的音乐可视化项目(11)---WS2812幻彩灯带
https://makelog.dfrobot.com.cn/article-311745.html
【花雕动手做】有趣好玩的音乐可视化项目(12)---米管快速节奏灯
https://makelog.dfrobot.com.cn/article-311746.html
【花雕动手做】有趣好玩的音乐可视化系列小项目(13)---有机棒立柱灯
https://makelog.dfrobot.com.cn/article-311759.html

徒步的路上,在草丛里面捡到一些报废的有机棒

project-image

在路边五金店里,买了一米装修用的墙上线槽盒子

project-image

带气泡的有机棒

project-image

线槽盒子有可拆卸的盖子

project-image

备好项目材料

project-image
project-image

有机棒直径约20mm

project-image

测量划线

project-image
project-image

先钻小孔

project-image

再扩大孔

project-image

打孔完工

project-image

声音模块依旧使用MAX9814(几元钱便宜好用)

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

project-image

彩灯使用WS2812B

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

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

project-image

WS2812模块电原理图

project-image

WS2812B灯带选用的是每米60灯黑底裸板

project-image

WS2812B是集控制电路和发光电路于一体的LED光源元件,其控制IC为WS2812B,发光元件是5050RGBLED,电压为5V,每个单位的峰值电流为60ma,灯带为三线制,VCC GND DIN分别为电源+、电源-、信号,当使用外部电源时,外部电源-需要与单片机的GND相连。

project-image
project-image
project-image

灯带固定在线槽盖子上

project-image

开始安装有机棒

project-image

Arduno UNO项目接线示意图

project-image
project-image

使用热干胶固定有机棒

project-image

安装好了

project-image

【花雕动手做】有趣好玩的音乐可视化系列小项目(13)---有机棒立柱灯
实验程序:15种循环灯光模式
模块接线:WS2812B接D6
MAX9814 UNO
VCC 5V
GND GND
OUT A0

代码
/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(13)---有机排比立柱灯
  相关程序之:SoftwareSerial有机排比立柱灯
  模块接线:WS2812B接D6
  MAX9814      UNO
  VCC          5V
  GND         GND
  OUT          A0
*/

#include <Adafruit_NeoPixel.h>
#include <FastLED.h>
#include <math.h>
#include <SoftwareSerial.h>
#define N_PIXELS  70  
#define N_PIXELS_HALF (N_PIXELS/2)
#define MIC_PIN   A0  
#define LED_PIN    6  
#define SAMPLE_WINDOW   10  
#define PEAK_HANG 24 
#define PEAK_FALL 20 
#define PEAK_FALL2 8 
#define INPUT_FLOOR 10 
#define INPUT_CEILING 30 
#define DC_OFFSET  0  
#define NOISE     10  
#define SAMPLES   60  
#define TOP       (N_PIXELS + 2)
#define SPEED .20       
#define TOP2      (N_PIXELS + 1) 
#define LAST_PIXEL_OFFSET N_PIXELS-1
#define PEAK_FALL_MILLIS 10  
#define POT_PIN    4
#define BG 0
#define LAST_PIXEL_OFFSET N_PIXELS-1
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
#define BRIGHTNESS  255
#define LED_TYPE    WS2812B     
#define COLOR_ORDER GRB
#define COLOR_MIN           0
#define COLOR_MAX         255
#define DRAW_MAX          100
#define SEGMENTS            4  
#define COLOR_WAIT_CYCLES  10  
#define qsubd(x, b)  ((x>b)?b:0)
#define qsuba(x, b)  ((x>b)?x-b:0)                                              
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))

struct CRGB leds[N_PIXELS];

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

static uint16_t dist;         
uint16_t scale = 30;          
uint8_t maxChanges = 48;      

CRGBPalette16 currentPalette(OceanColors_p);
CRGBPalette16 targetPalette(CloudColors_p);

uint8_t timeval = 20;                                                           
uint16_t loops = 0;                                                             
bool     samplepeak = 0;                                                        
uint16_t oldsample = 0;                                                         
bool thisdir = 0;

enum
{
} MODE;
bool reverse = true;
int BRIGHTNESS_MAX = 80;
int brightness = 20;


byte
//  peak      = 0,      
//  dotCount  = 0,      
volCount  = 0;      
int
reading,
vol[SAMPLES],       
lvl       = 10,      
minLvlAvg = 0,      
maxLvlAvg = 512;
float
greenOffset = 30,
blueOffset = 150;

int CYCLE_MIN_MILLIS = 2;
int CYCLE_MAX_MILLIS = 1000;
int cycleMillis = 20;
bool paused = false;
long lastTime = 0;
bool boring = true;
bool gReverseDirection = false;
int          myhue =   0;

uint8_t colour;
uint8_t myfade = 255;                                         
#define maxsteps 16                                           
int peakspersec = 0;
int peakcount = 0;
uint8_t bgcol = 0;
int thisdelay = 20;
uint8_t max_bright = 255;

unsigned int sample;

#define NSAMPLES 64
unsigned int samplearray[NSAMPLES];
unsigned long samplesum = 0;
unsigned int sampleavg = 0;
int samplecount = 0;
//unsigned int sample = 0;
unsigned long oldtime = 0;
unsigned long newtime = 0;

int color;
int center = 0;
int step = -1;
int maxSteps = 16;
float fadeRate = 0.80;
int diff;

int
origin = 0,
color_wait_count = 0,
scroll_color = COLOR_MIN,
last_intensity = 0,
intensity_max = 0,
origin_at_flip = 0;
uint32_t
draw[DRAW_MAX];
boolean
growing = false,
fall_from_left = true;

uint32_t currentBg = random(256);
uint32_t nextBg = currentBg;
TBlendType    currentBlending;

const int buttonPin = 0;     

int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;


byte peak = 16;      

byte dotCount = 0;  
byte dotHangCount = 0; 

void setup() {

  analogReference(EXTERNAL);

  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);

  // Serial.begin(9600);
  strip.begin();
  strip.show(); // all pixels to 'off'

  Serial.begin(57600);
  delay(3000);

  LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, N_PIXELS).setCorrection(TypicalLEDStrip);
  LEDS.setBrightness(BRIGHTNESS);
  dist = random16(12345);          
}

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;  

  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() {
  uint8_t  i;
  uint16_t minLvl, maxLvl;
  int      n, height;
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
      if (buttonPushCounter == 16) {
        buttonPushCounter = 1;
      }
    }
    else {
      Serial.println("off");
    }
  }
  lastButtonState = buttonState;

  switch (buttonPushCounter) {

    case 1:
      buttonPushCounter == 1; {
        All2(); 
        break;
      }

    case 2:
      buttonPushCounter == 2; {
        vu(); 
        break;
      }

    case 3:
      buttonPushCounter == 3; {
        vu1(); 
        break;
      }

    case 4:
      buttonPushCounter == 4; {
        vu2(); 
        break;
      }

    case 5:
      buttonPushCounter == 5; {
        Vu3(); 
        break;
      }

    case 6:
      buttonPushCounter == 6; {
        Vu4(); 
        break;
      }

    case 7:
      buttonPushCounter == 7; {
        Vu5(); 
        break;
      }

    case 8:
      buttonPushCounter == 8; {
        Vu6(); 
        break;
      }

    case 9:
      buttonPushCounter == 9; {
        vu7(); 
        break;
      }

    case 10:
      buttonPushCounter == 10; {
        vu8(); 
        break;
      }

    case 11:
      buttonPushCounter == 11; {
        vu9(); 
        break;
      }

    case 12:
      buttonPushCounter == 12; {
        vu10();
        break;
      }
    case 13:
      buttonPushCounter == 13; {
        vu11(); 
        break;
      }

    case 14:
      buttonPushCounter == 14; {
        vu12(); 
        break;
      }

    case 15:
      buttonPushCounter == 15; {
        vu13(); 
        break;
      }


    case 16:
      buttonPushCounter == 16; {
        colorWipe(strip.Color(0, 0, 0), 10);
        break;
      }
  }
}

void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    if (digitalRead(buttonPin) != lastButtonState) 
      return;         
    delay(wait);
  }
}

void vu() {
  uint8_t  i;
  uint16_t minLvl, maxLvl;
  int      n, height;
  n   = analogRead(MIC_PIN);                        
  n   = abs(n - 512 - DC_OFFSET); 
  n   = (n <= NOISE) ? 0 : (n - NOISE);             
  lvl = ((lvl * 7) + n) >> 3;    

  height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);

  if (height < 0L)       height = 0;     
  else if (height > TOP) height = TOP;
  if (height > peak)     peak   = height; 

  for (i = 0; i < N_PIXELS; i++) {
    if (i >= height)               strip.setPixelColor(i,   0,   0, 0);
    else strip.setPixelColor(i, Wheel(map(i, 0, strip.numPixels() - 1, 30, 150)));
  }
 
  if (peak > 0 && peak <= N_PIXELS - 1) strip.setPixelColor(peak, Wheel(map(peak, 0, strip.numPixels() - 1, 30, 150)));

  strip.show(); 
  
  if (++dotCount >= PEAK_FALL) {

    if (peak > 0) peak--;
    dotCount = 0;
  }

  vol[volCount] = n;                      
  if (++volCount >= SAMPLES) volCount = 0; 

  minLvl = maxLvl = vol[0];
  for (i = 1; i < SAMPLES; i++) {
    if (vol[i] < minLvl)      minLvl = vol[i];
    else if (vol[i] > maxLvl) maxLvl = vol[i];
  }
  
  if ((maxLvl - minLvl) < TOP) maxLvl = minLvl + TOP;
  minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6; 
  maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6; 
}

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);
  }
}

void vu1() {
  uint8_t  i;
  uint16_t minLvl, maxLvl;
  int      n, height;

  n   = analogRead(MIC_PIN);                        
  n   = abs(n - 512 - DC_OFFSET); 
  n   = (n <= NOISE) ? 0 : (n - NOISE);            
  lvl = ((lvl * 7) + n) >> 3;    

  height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);

  if (height < 0L)       height = 0;     
  else if (height > TOP) height = TOP;
  if (height > peak)     peak   = height; 

  for (i = 0; i < N_PIXELS_HALF; i++) {
    if (i >= height) {
      strip.setPixelColor(N_PIXELS_HALF - i - 1,   0,   0, 0);
      strip.setPixelColor(N_PIXELS_HALF + i,   0,   0, 0);
    }
    else {
      uint32_t color = Wheel(map(i, 0, N_PIXELS_HALF - 1, 30, 150));
      strip.setPixelColor(N_PIXELS_HALF - i - 1, color);
      strip.setPixelColor(N_PIXELS_HALF + i, color);
    }
  }

  if (peak > 0 && peak <= N_PIXELS_HALF - 1) {
    uint32_t color = Wheel(map(peak, 0, N_PIXELS_HALF - 1, 30, 150));
    strip.setPixelColor(N_PIXELS_HALF - peak - 1, color);
    strip.setPixelColor(N_PIXELS_HALF + peak, color);
  }

  strip.show(); 

  if (++dotCount >= PEAK_FALL) { //fall rate

    if (peak > 0) peak--;
    dotCount = 0;
  }

  vol[volCount] = n;                      
  if (++volCount >= SAMPLES) volCount = 0; 

  minLvl = maxLvl = vol[0];
  for (i = 1; i < SAMPLES; i++) {
    if (vol[i] < minLvl)      minLvl = vol[i];
    else if (vol[i] > maxLvl) maxLvl = vol[i];
  }

  if ((maxLvl - minLvl) < TOP) maxLvl = minLvl + TOP;
  minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6;
  maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6; 
}

void vu2()
{
  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 <= N_PIXELS_HALF - 1; i++) {
    uint32_t color = Wheel(map(i, 0, N_PIXELS_HALF - 1, 30, 150));
    strip.setPixelColor(N_PIXELS - i, color);
    strip.setPixelColor(0 + i, color);
  }

  c = fscale(INPUT_FLOOR, INPUT_CEILING, N_PIXELS_HALF, 0, peakToPeak, 2);

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

  y = N_PIXELS_HALF - peak;
  uint32_t color1 = Wheel(map(y, 0, N_PIXELS_HALF - 1, 30, 150));
  strip.setPixelColor(y - 1, color1);

  y = N_PIXELS_HALF + peak;
  strip.setPixelColor(y, color1);

  strip.show();

  if (dotHangCount > PEAK_HANG) { 
    if (++dotCount >= PEAK_FALL2) { 
      peak++;
      dotCount = 0;
    }
  }
  else {
    dotHangCount++;
  }
}


void Vu3() {
  uint8_t i;
  uint16_t minLvl, maxLvl;
  int n, height;

  n = analogRead(MIC_PIN);             
  n = abs(n - 512 - DC_OFFSET);        
  n = (n <= NOISE) ? 0 : (n - NOISE);  
  lvl = ((lvl * 7) + n) >> 3;    

  height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);

  if (height < 0L)       height = 0;      
  else if (height > TOP) height = TOP;
  if (height > peak)     peak   = height; 

  greenOffset += SPEED;
  blueOffset += SPEED;
  if (greenOffset >= 255) greenOffset = 0;
  if (blueOffset >= 255) blueOffset = 0;

  for (i = 0; i < N_PIXELS; i++) {
    if (i >= height) {
      strip.setPixelColor(i, 0, 0, 0);
    } else {
      strip.setPixelColor(i, Wheel(
                            map(i, 0, strip.numPixels() - 1, (int)greenOffset, (int)blueOffset)
                          ));
    }
  }
  if (peak > 0 && peak <= N_PIXELS - 1) strip.setPixelColor(peak, Wheel(map(peak, 0, strip.numPixels() - 1, 30, 150)));

  strip.show(); 

  if (++dotCount >= PEAK_FALL) { 

    if (peak > 0) peak--;
    dotCount = 0;
  }
  strip.show();  

  vol[volCount] = n;
  if (++volCount >= SAMPLES) {
    volCount = 0;
  }

  minLvl = maxLvl = vol[0];
  for (i = 1; i < SAMPLES; i++) {
    if (vol[i] < minLvl) {
      minLvl = vol[i];
    } else if (vol[i] > maxLvl) {
      maxLvl = vol[i];
    }
  }

  if ((maxLvl - minLvl) < TOP) {
    maxLvl = minLvl + TOP;
  }
  minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6; 
  maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6; 
}


void Vu4() {
  uint8_t  i;
  uint16_t minLvl, maxLvl;
  int      n, height;

  n   = analogRead(MIC_PIN);                        
  n   = abs(n - 512 - DC_OFFSET); 
  n   = (n <= NOISE) ? 0 : (n - NOISE);             
  lvl = ((lvl * 7) + n) >> 3;    

  height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);

  if (height < 0L)       height = 0;     
  else if (height > TOP) height = TOP;
  if (height > peak)     peak   = height; 
  greenOffset += SPEED;
  blueOffset += SPEED;
  if (greenOffset >= 255) greenOffset = 0;
  if (blueOffset >= 255) blueOffset = 0;

  for (i = 0; i < N_PIXELS_HALF; i++) {
    if (i >= height) {
      strip.setPixelColor(N_PIXELS_HALF - i - 1,   0,   0, 0);
      strip.setPixelColor(N_PIXELS_HALF + i,   0,   0, 0);
    }
    else {
      uint32_t color = Wheel(map(i, 0, N_PIXELS_HALF - 1, (int)greenOffset, (int)blueOffset));
      strip.setPixelColor(N_PIXELS_HALF - i - 1, color);
      strip.setPixelColor(N_PIXELS_HALF + i, color);
    }

  }

  // Draw peak dot
  if (peak > 0 && peak <= N_PIXELS_HALF - 1) {
    uint32_t color = Wheel(map(peak, 0, N_PIXELS_HALF - 1, 30, 150));
    strip.setPixelColor(N_PIXELS_HALF - peak - 1, color);
    strip.setPixelColor(N_PIXELS_HALF + peak, color);
  }

  strip.show(); // Update strip

  // Every few frames, make the peak pixel drop by 1:

  if (++dotCount >= PEAK_FALL) { //fall rate

    if (peak > 0) peak--;
    dotCount = 0;
  }


  vol[volCount] = n;                     
  if (++volCount >= SAMPLES) volCount = 0; 


  minLvl = maxLvl = vol[0];
  for (i = 1; i < SAMPLES; i++) {
    if (vol[i] < minLvl)      minLvl = vol[i];
    else if (vol[i] > maxLvl) maxLvl = vol[i];
  }

  if ((maxLvl - minLvl) < TOP) maxLvl = minLvl + TOP;
  minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6; 
  maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6;
}

void Vu5()
{
  uint8_t  i;
  uint16_t minLvl, maxLvl;
  int      n, height;

  n   = analogRead(MIC_PIN);                        
  n   = abs(n - 512 - DC_OFFSET); 
  n   = (n <= NOISE) ? 0 : (n - NOISE);             
  lvl = ((lvl * 7) + n) >> 3;   

  height = TOP2 * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);

  if (height < 0L)       height = 0;     
  else if (height > TOP2) height = TOP2;
  if (height > peak)     peak   = height; 


#ifdef CENTERED
  for (i = 0; i < (N_PIXELS / 2); i++) {
    if (((N_PIXELS / 2) + i) >= height)
    {
      strip.setPixelColor(((N_PIXELS / 2) + i),   0,   0, 0);
      strip.setPixelColor(((N_PIXELS / 2) - i),   0,   0, 0);
    }
    else
    {
      strip.setPixelColor(((N_PIXELS / 2) + i), Wheel(map(((N_PIXELS / 2) + i), 0, strip.numPixels() - 1, 30, 150)));
      strip.setPixelColor(((N_PIXELS / 2) - i), Wheel(map(((N_PIXELS / 2) - i), 0, strip.numPixels() - 1, 30, 150)));
    }
  }

  if (peak > 0 && peak <= LAST_PIXEL_OFFSET)
  {
    strip.setPixelColor(((N_PIXELS / 2) + peak), 255, 255, 255); 
    strip.setPixelColor(((N_PIXELS / 2) - peak), 255, 255, 255); 
  }
#else
  for (i = 0; i < N_PIXELS; i++)
  {
    if (i >= height)
    {
      strip.setPixelColor(i,   0,   0, 0);
    }
    else
    {
      strip.setPixelColor(i, Wheel(map(i, 0, strip.numPixels() - 1, 30, 150)));
    }
  }

  if (peak > 0 && peak <= LAST_PIXEL_OFFSET)
  {
    strip.setPixelColor(peak, 255, 255, 255); 
  }

#endif

  if (millis() - lastTime >= PEAK_FALL_MILLIS)
  {
    lastTime = millis();

    strip.show(); 

    if (peak > 0) peak--;
  }

  vol[volCount] = n;                      
  if (++volCount >= SAMPLES) volCount = 0; 

 
  minLvl = maxLvl = vol[0];
  for (i = 1; i < SAMPLES; i++)
  {
    if (vol[i] < minLvl)      minLvl = vol[i];
    else if (vol[i] > maxLvl) maxLvl = vol[i];
  }

  if ((maxLvl - minLvl) < TOP2) maxLvl = minLvl + TOP2;
  minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6; 
  maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6; 
}

void Vu6()
{
  uint8_t  i;
  uint16_t minLvl, maxLvl;
  int      n, height;

  n   = analogRead(MIC_PIN);                       
  n   = abs(n - 512 - DC_OFFSET); 
  n   = (n <= NOISE) ? 0 : (n - NOISE);            
  lvl = ((lvl * 7) + n) >> 3;   

  height = TOP2 * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);

  if (height < 0L)       height = 0;     
  else if (height > TOP2) height = TOP2;
  if (height > peak)     peak   = height; 


#ifdef CENTERED
  if (peak > 0 && peak <= LAST_PIXEL_OFFSET)
  {
    strip.setPixelColor(((N_PIXELS / 2) + peak), 255, 255, 255); 
    strip.setPixelColor(((N_PIXELS / 2) - peak), 255, 255, 255); 
  }
#else
  // Color pixels based on rainbow gradient
  for (i = 0; i < N_PIXELS; i++)
  {
    if (i >= height)
    {
      strip.setPixelColor(i,   0,   0, 0);
    }
    else
    {
    }
  }

  // Draw peak dot
  if (peak > 0 && peak <= LAST_PIXEL_OFFSET)
  {
    strip.setPixelColor(peak, 0, 0, 255); 
  }

#endif

  // Every few frames, make the peak pixel drop by 1:

  if (millis() - lastTime >= PEAK_FALL_MILLIS)
  {
    lastTime = millis();

    strip.show(); // Update strip

    //fall rate
    if (peak > 0) peak--;
  }

  vol[volCount] = n;                      
  if (++volCount >= SAMPLES) volCount = 0; 

  minLvl = maxLvl = vol[0];
  for (i = 1; i < SAMPLES; i++)
  {
    if (vol[i] < minLvl)      minLvl = vol[i];
    else if (vol[i] > maxLvl) maxLvl = vol[i];
  }
 
  if ((maxLvl - minLvl) < TOP2) maxLvl = minLvl + TOP2;
  minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6; 
  maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6; 
}

void vu7() {

  EVERY_N_MILLISECONDS(1000) {
    peakspersec = peakcount;                                       
    peakcount = 0;                                                 
  }

  soundmems();

  EVERY_N_MILLISECONDS(20) {
    ripple3();
  }
  show_at_max_brightness_for_power();
} 

void soundmems() {                                                  
  newtime = millis();
  int tmp = analogRead(MIC_PIN) - 512;
  sample = abs(tmp);

  int potin = map(analogRead(POT_PIN), 0, 1023, 0, 60);

  samplesum = samplesum + sample - samplearray[samplecount];        
  sampleavg = samplesum / NSAMPLES;                                 
  samplearray[samplecount] = sample;                                
  samplecount = (samplecount + 1) % NSAMPLES;                     

  if (newtime > (oldtime + 200)) digitalWrite(13, LOW);           

  if ((sample > (sampleavg + potin)) && (newtime > (oldtime + 60)) ) { 
    step = -1;
    peakcount++;
    digitalWrite(13, HIGH);
    oldtime = newtime;
  }
}  

void ripple3() {
  for (int i = 0; i < N_PIXELS; i++) leds[i] = CHSV(bgcol, 255, sampleavg * 2); 

  switch (step) {

    case -1:                                                        
      center = random(N_PIXELS);
      colour = (peakspersec * 10) % 255;                                           
      step = 0;
      bgcol = bgcol + 8;
      break;

    case 0:
      leds[center] = CHSV(colour, 255, 255);                          
      step ++;
      break;

    case maxsteps:                                                    
      break;

    default:                                                            
      leds[(center + step + N_PIXELS) % N_PIXELS] += CHSV(colour, 255, myfade / step * 2);  
      leds[(center - step + N_PIXELS) % N_PIXELS] += CHSV(colour, 255, myfade / step * 2);
      step ++;                                                         
      break;
  } 
} 

void vu8() {
  int intensity = calculateIntensity();
  updateOrigin(intensity);
  assignDrawValues(intensity);
  writeSegmented();
  updateGlobals();
}

int calculateIntensity() {
  int      intensity;

  reading   = analogRead(MIC_PIN);                       
  reading   = abs(reading - 512 - DC_OFFSET); 
  reading   = (reading <= NOISE) ? 0 : (reading - NOISE);           
  lvl = ((lvl * 7) + reading) >> 3;    

  intensity = DRAW_MAX * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);

  return constrain(intensity, 0, DRAW_MAX - 1);
}

void updateOrigin(int intensity) {
  if (growing && intensity < last_intensity) {
    growing = false;
    intensity_max = last_intensity;
    fall_from_left = !fall_from_left;
    origin_at_flip = origin;
  } else if (intensity > last_intensity) {
    growing = true;
    origin_at_flip = origin;
  }
  last_intensity = intensity;

  if (!growing) {
    if (fall_from_left) {
      origin = origin_at_flip + ((intensity_max - intensity) / 2);
    } else {
      origin = origin_at_flip - ((intensity_max - intensity) / 2);
    }
    if (origin < 0) {
      origin = DRAW_MAX - abs(origin);
    } else if (origin > DRAW_MAX - 1) {
      origin = origin - DRAW_MAX - 1;
    }
  }
}

void assignDrawValues(int intensity) {
  int min_lit = origin - (intensity / 2);
  int max_lit = origin + (intensity / 2);
  if (min_lit < 0) {
    min_lit = min_lit + DRAW_MAX;
  }
  if (max_lit >= DRAW_MAX) {
    max_lit = max_lit - DRAW_MAX;
  }
  for (int i = 0; i < DRAW_MAX; i++) {

    if (
      (min_lit < max_lit && min_lit < i && i < max_lit) 
      || (min_lit > max_lit && (i > min_lit || i < max_lit)) 
    ) {
      draw[i] = Wheel(scroll_color);
    } else {
      draw[i] = 0;
    }
  }
}

void writeSegmented() {
  int seg_len = N_PIXELS / SEGMENTS;

  for (int s = 0; s < SEGMENTS; s++) {
    for (int i = 0; i < seg_len; i++) {
      strip.setPixelColor(i + (s * seg_len), draw[map(i, 0, seg_len, 0, DRAW_MAX)]);
    }
  }
  strip.show();
}

uint32_t * segmentAndResize(uint32_t* draw) {
  int seg_len = N_PIXELS / SEGMENTS;

  uint32_t segmented[N_PIXELS];
  for (int s = 0; s < SEGMENTS; s++) {
    for (int i = 0; i < seg_len; i++) {
      segmented[i + (s * seg_len) ] = draw[map(i, 0, seg_len, 0, DRAW_MAX)];
    }
  }
  return segmented;
}

void writeToStrip(uint32_t* draw) {
  for (int i = 0; i < N_PIXELS; i++) {
    strip.setPixelColor(i, draw[i]);
  }
  strip.show();
}

void updateGlobals() {
  uint16_t minLvl, maxLvl;

  color_wait_count++;
  if (color_wait_count > COLOR_WAIT_CYCLES) {
    color_wait_count = 0;
    scroll_color++;
    if (scroll_color > COLOR_MAX) {
      scroll_color = COLOR_MIN;
    }
  }

  vol[volCount] = reading;                      
  if (++volCount >= SAMPLES) volCount = 0; 

  minLvl = maxLvl = vol[0];
  for (uint8_t i = 1; i < SAMPLES; i++) {
    if (vol[i] < minLvl)      minLvl = vol[i];
    else if (vol[i] > maxLvl) maxLvl = vol[i];
  }
  
  if ((maxLvl - minLvl) < N_PIXELS) maxLvl = minLvl + N_PIXELS;
  minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6; 
  maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6; 
}

void vu9() {
  currentPalette = OceanColors_p;                            
  currentBlending = LINEARBLEND;
  EVERY_N_SECONDS(5) {                                        
    for (int i = 0; i < 16; i++) {
      targetPalette[i] = CHSV(random8(), 255, 255);
    }
  }

  EVERY_N_MILLISECONDS(100) {                                 
    uint8_t maxChanges = 24;
    nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges);
  }

  EVERY_N_MILLIS_I(thistimer, 20) {                           
    uint8_t timeval = beatsin8(10, 20, 50);                  
    thistimer.setPeriod(timeval);                            
    fadeToBlackBy(leds, N_PIXELS, 16);                       
    sndwave();
    soundble();
  }
  FastLED.setBrightness(max_bright);
  FastLED.show();
} 

void soundble() {                                           
  int tmp = analogRead(MIC_PIN) - 512 - DC_OFFSET;
  sample = abs(tmp);
} 

void sndwave() {
  leds[N_PIXELS / 2] = ColorFromPalette(currentPalette, sample, sample * 2, currentBlending); 
  for (int i = N_PIXELS - 1; i > N_PIXELS / 2; i--) {    
    leds[i] = leds[i - 1];
  }

  for (int i = 0; i < N_PIXELS / 2; i++) {                
    leds[i] = leds[i + 1];
  }
  addGlitter(sampleavg);
}

void vu10() {
  EVERY_N_SECONDS(5) {                                       
    static uint8_t baseC = random8();                         

    for (int i = 0; i < 16; i++) {
      targetPalette[i] = CHSV(random8(), 255, 255);
    }
  }

  EVERY_N_MILLISECONDS(100) {
    uint8_t maxChanges = 24;
    nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges);   
  }

  EVERY_N_MILLISECONDS(thisdelay) {                           
    soundtun();
    FastLED.setBrightness(max_bright);
    FastLED.show();
  }
} 

void soundtun() {
  int n;
  n = analogRead(MIC_PIN);                                    
  n = qsuba(abs(n - 512), 10);                                
  CRGB newcolour = ColorFromPalette(currentPalette, constrain(n, 0, 255), constrain(n, 0, 255), currentBlending);
  nblend(leds[0], newcolour, 128);

  for (int i = N_PIXELS - 1; i > 0; i--) {
    leds[i] = leds[i - 1];
  }

} 

void vu11() {
  EVERY_N_MILLISECONDS(1000) {
    peakspersec = peakcount;                                  
    peakcount = 0;                                            
  }

  soundrip();

  EVERY_N_MILLISECONDS(20) {
    rippled();
  }
  FastLED.show();
} 

void soundrip() {                                            
  newtime = millis();
  int tmp = analogRead(MIC_PIN) - 512;
  sample = abs(tmp);

  int potin = map(analogRead(POT_PIN), 0, 1023, 0, 60);

  samplesum = samplesum + sample - samplearray[samplecount];  
  sampleavg = samplesum / NSAMPLES;                          

  Serial.println(sampleavg);

  samplearray[samplecount] = sample;                          
  samplecount = (samplecount + 1) % NSAMPLES;                

  if (newtime > (oldtime + 200)) digitalWrite(13, LOW);       

  if ((sample > (sampleavg + potin)) && (newtime > (oldtime + 60)) ) { 
    step = -1;
    peakcount++;
    oldtime = newtime;
  }
}  

void rippled() {

  fadeToBlackBy(leds, N_PIXELS, 64);                          

  switch (step) {

    case -1:                                                  
      center = random(N_PIXELS);
      colour = (peakspersec * 10) % 255;                      
      step = 0;
      break;

    case 0:
      leds[center] = CHSV(colour, 255, 255);                  
      step ++;
      break;

    case maxsteps:                                           
      break;

    default:                                                 
      leds[(center + step + N_PIXELS) % N_PIXELS] += CHSV(colour, 255, myfade / step * 2);   
      leds[(center - step + N_PIXELS) % N_PIXELS] += CHSV(colour, 255, myfade / step * 2);
      step ++;                                                
      break;
  } 

} 

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);
  }
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
  strip.setPixelColor(Pixel, strip.Color(red, green, blue));
}

void setAll(byte red, byte green, byte blue) {

  for (int i = 0; i < N_PIXELS; i++ ) {

    setPixel(i, red, green, blue);
  }
  strip.show();
}
void vu12() {

  EVERY_N_MILLISECONDS(1000) {
    peakspersec = peakcount;                                  
    peakcount = 0;                                            
  }

  soundripped();

  EVERY_N_MILLISECONDS(20) {
    rippvu();
  }
  FastLED.show();
}

void soundripped() {                                            
  newtime = millis();
  int tmp = analogRead(MIC_PIN) - 512;
  sample = abs(tmp);

  int potin = map(analogRead(POT_PIN), 0, 1023, 0, 60);

  samplesum = samplesum + sample - samplearray[samplecount];  
  sampleavg = samplesum / NSAMPLES;                           

  Serial.println(sampleavg);

  samplearray[samplecount] = sample;                          
  samplecount = (samplecount + 1) % NSAMPLES;                 

  if (newtime > (oldtime + 200)) digitalWrite(13, LOW);      

  if ((sample > (sampleavg + potin)) && (newtime > (oldtime + 60)) ) { 
    step = -1;
    peakcount++;
    oldtime = newtime;
  }

}  
void rippvu() {                                                                

  fadeToBlackBy(leds, N_PIXELS, 64);                          

  switch (step) {

    case -1:                                                  
      center = random(N_PIXELS);
      colour = (peakspersec * 10) % 255;                      
      step = 0;
      break;

    case 0:
      leds[center] = CHSV(colour, 255, 255);                  
      step ++;
      break;

    case maxsteps:                                            
      break;

    default:                                                 
      leds[(center + step + N_PIXELS) % N_PIXELS] += CHSV(colour, 255, myfade / step * 2);   
      leds[(center - step + N_PIXELS) % N_PIXELS] += CHSV(colour, 255, myfade / step * 2);
      step ++;                                                
      break;
  } 
  addGlitter(sampleavg);
} 

void vu13() {                                                                   
  EVERY_N_MILLISECONDS(1000) {
    peakspersec = peakcount;                                  
    peakcount = 0;                                           
  }

  soundripper();

  EVERY_N_MILLISECONDS(20) {
    jugglep();
  }
  FastLED.show();
} 

void soundripper() {                                            

  newtime = millis();
  int tmp = analogRead(MIC_PIN) - 512;
  sample = abs(tmp);

  int potin = map(analogRead(POT_PIN), 0, 1023, 0, 60);

  samplesum = samplesum + sample - samplearray[samplecount];  
  sampleavg = samplesum / NSAMPLES;                           

  Serial.println(sampleavg);

  samplearray[samplecount] = sample;                         
  samplecount = (samplecount + 1) % NSAMPLES;                 

  if (newtime > (oldtime + 200)) digitalWrite(13, LOW);       

  if ((sample > (sampleavg + potin)) && (newtime > (oldtime + 60)) ) { 
    step = -1;
    peakcount++;
    oldtime = newtime;
    jugglep();
  }
} 

void jugglep() {                                                                

  static uint8_t thishue = 0;
  timeval = 40;                                                                 
  leds[0] = ColorFromPalette(currentPalette, thishue++, sampleavg, LINEARBLEND);
  for (int i = N_PIXELS - 1; i > 0 ; i-- ) leds[i] = leds[i - 1];
  addGlitter(sampleavg / 2);                                                   
} 

uint32_t Wheel(byte WheelPos, float opacity) {

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

void addGlitter( fract8 chanceOfGlitter) {                                     

  if ( random8() < chanceOfGlitter) {
    leds[random16(N_PIXELS)] += CRGB::White;
  }
} 

typedef void (*SimplePatternList[])();
SimplePatternList qPatterns = {vu, vu1, vu2, Vu3, Vu4, Vu5, Vu6, vu7, vu8, vu9, vu10, vu11, vu12, vu13};
uint8_t qCurrentPatternNumber = 0; 

void nextPattern2()
{
  qCurrentPatternNumber = (qCurrentPatternNumber + 1) % ARRAY_SIZE( qPatterns);
}

void All2()
{
  qPatterns[qCurrentPatternNumber]();
  EVERY_N_SECONDS( 30 ) {
    nextPattern2();  
  }
}

实验场景图

project-image

实验场景图 动态图

project-image

实验场景图 动态图

project-image

实验场景图 动态图

project-image

实验的视频记录(3分21秒)

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

实验的视频记录(6分09秒)

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

实验场景图 动态图

project-image

实验场景图 动态图

project-image

实验场景图

project-image

实验的视频记录(4分44秒)

https://v.youku.com/v_show/id_XNTg4OTAyMTM3Ng==.html?spm=a2hcb.playlsit.page.3

实验的视频记录(6分17秒)

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

四块开发板,111根有机玻璃棒,259颗WS2812B灯珠组成的音乐可视化器

project-image

实验场景图 动态图

project-image

实验的视频记录(3分30秒)

https://v.youku.com/v_show/id_XNTg5MDI2MDIyOA==.html?spm=a2hcb.playlsit.page.3

实验的视频记录(4分03秒)

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

评论

user-avatar
  • 三春牛-创客

    三春牛-创客2023.01.28

    0
    • 三春牛-创客

      三春牛-创客2023.01.28

      厉害

      0
      • Nd0TAy8yduIE

        Nd0TAy8yduIE2022.11.13

        漂亮

        1
        • 驴友花雕

          驴友花雕2022.11.14

          谢谢鼓励

      • amy微信

        amy微信2022.08.09

        超厉害

        1
        • 驴友花雕

          驴友花雕2022.10.12

          谢谢老师的鼓励

      • Mary

        Mary2022.07.27

        很酷炫

        1
        • 驴友花雕

          驴友花雕2022.07.27

          谢谢老师的鼓励

      • 匿名

        匿名

        该评论已删除

        1
        • 驴友花雕

          驴友花雕2023.07.01

          谢谢鼓励!

      icon 他的勋章
        展开更多