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

【花雕动手做】有趣好玩的音乐可视化系列小项目(16)--热干胶棒棒灯 简单

头像 驴友花雕 2022.08.14 132 2

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

project-image

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

project-image

声音模块,使用性价比更高的MAX4466声音传感器

project-image

【花雕动手做】有趣好玩的音乐可视化系列小项目(16)--热干胶棒棒灯
项目程序之一:热干胶棒棒音乐反应灯
模块接线:WS2812B接D6
MAX4466 UNO
VCC 5V
GND GND
OUT A0

代码
/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(16)--热干胶棒棒灯
  项目程序之一:热干胶棒棒音乐反应灯
  模块接线:WS2812B接D6
  MAX4466      UNO
  VCC          5V
  GND         GND
  OUT          A0
*/

#include<FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 18

CRGB leds[NUM_LEDS];
uint8_t hue = 0;
int soundsensor = A0;

void setup() {
  delay(2000);
  FastLED.setBrightness(100);
  pinMode(soundsensor, INPUT);
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
}

void loop() {
  int sensval = digitalRead(soundsensor);

  if (sensval == 1) {
    Serial.println("ON");
    leds[0] = CRGB :: Red;
    fill_solid(leds, NUM_LEDS, CRGB :: Blue);
    rainbow_moving();
    FastLED.show();
    delay(10);
  }
  else {
    leds[0] = CRGB :: Black;
    fill_solid(leds, NUM_LEDS, CRGB :: Black);
    FastLED.show();
    delay(10);
  }
}

void rainbow_moving() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CHSV(hue + (i * 10), 255, 255);
  }
  EVERY_N_MILLISECONDS(10) {
    hue++;
  }
}

实验的视频记录之一(1分05秒)

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

【花雕动手做】有趣好玩的音乐可视化系列小项目(16)--热干胶棒棒灯
项目程序之一:热干胶棒棒音乐节奏灯盘
模块接线:WS2812B接D6
MAX4466 UNO
VCC 5V
GND GND
OUT A0

代码
/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(16)--热干胶棒棒灯
  项目程序之二:热干胶棒棒音乐节奏灯盘
  模块接线:WS2812B接D6
  MAX4466      UNO
  VCC          5V
  GND         GND
  OUT          A0
*/

#include<FastLED.h>
#include<MegunoLink.h>
#include<Filter.h>

#define N_PIXELS  69
#define MIC_PIN   A5
#define LED_PIN   12
#define NOISE 10
#define TOP   (N_PIXELS+2)
#define LED_TYPE  WS2811
#define BRIGHTNESS  22
#define COLOR_ORDER GRB

CRGB leds[N_PIXELS];
int lvl = 0, minLvl = 0, maxLvl = 10;

ExponentialFilter<long> ADCFilter(5, 0);

void setup() {
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, N_PIXELS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(BRIGHTNESS);
}

void loop() {
  int n, height;
  n = analogRead(MIC_PIN);
  n = abs(1023 - n);
  n = (n <= NOISE) ? 0 : abs(n - NOISE);
  ADCFilter.Filter(n);
  lvl = ADCFilter.Current();
  //  Serial.print(n);
  //  Serial.print(" ");
  //  Serial.println(lvl);
  height = TOP * (lvl - minLvl) / (long)(maxLvl - minLvl);
  if (height < 0L) height = 0;
  else if (height > TOP) height = TOP;
  for (uint8_t i = 0; i < N_PIXELS; i++) {
    if (i >= height) leds[i] = CRGB(0, 0, 0);
    else leds[i] = Wheel( map( i, 0, N_PIXELS - 1, 30, 150 ) );
  }
  FastLED.show();
}

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

实验场景图 动态图

project-image

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

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

找到一个装喜糖的小铁盒子

project-image

8分位钻孔

project-image

10mm扩孔

project-image

安装热干胶棒

project-image

扭个麻花

project-image
project-image

【花雕动手做】有趣好玩的音乐可视化系列小项目(16)--热干胶棒棒灯
项目程序之三:测试麻花棒棒灯
模块接线:WS2812B接D7
MAX4466 UNO
VCC 5V
GND GND
OUT A0

代码
/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(16)--热干胶棒棒灯
  项目程序之三:测试麻花棒棒灯
  模块接线:WS2812B接D7
  MAX4466      UNO
  VCC          5V
  GND         GND
  OUT          A0
*/


#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN 7

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  #if defined (__AVR_ATtiny85__)
    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif
  // End of trinket special code


  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Some example procedures showing how to display to the pixels:
  //colorWipe(strip.Color(255, 0, 0), 50); // Red
// colorWipe(strip.Color(0, 255, 0), 50); // Green
  //colorWipe(strip.Color(0, 0, 255), 50); // Blue
//colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
  // Send a theater pixel chase in...
// theaterChase(strip.Color(127, 127, 127), 50); // White
  //theaterChase(strip.Color(127, 0, 0), 50); // Red
  //theaterChase(strip.Color(0, 0, 127), 50); // Blue

  rainbow(20);
  //rainbowCycle(20);
  //theaterChaseRainbow(50);
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} 

实验场景图

project-image

实验的视频记录(1分05秒)

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

【花雕动手做】有趣好玩的音乐可视化系列小项目(16)--热干胶棒棒灯
项目程序之四:音乐反应麻花棒棒灯
模块接线:WS2812B接D6
MAX4466 UNO
VCC 5V
GND GND
OUT D7

代码
/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(16)--热干胶棒棒灯
  项目程序之四:音乐反应麻花棒棒灯
  模块接线:WS2812B接D6
  MAX4466      UNO
  VCC          5V
  GND         GND
  OUT          D7
*/

#include<FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 12

CRGB leds[NUM_LEDS];
uint8_t hue = 0;
int soundsensor = 7;

void setup() {
  delay(2000);
  Serial.begin(9600);
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(155);
  pinMode(soundsensor, INPUT);
}

void loop() {
  int sensval = digitalRead(soundsensor);

  if (sensval == 1) {
    Serial.println("ON");
    leds[0] = CRGB :: Red;
    fill_solid(leds, NUM_LEDS, CRGB :: Blue);
    rainbow_moving();
    FastLED.show();
    delay(10);
  }
  else {
    leds[0] = CRGB :: Black;
    fill_solid(leds, NUM_LEDS, CRGB :: Black);
    FastLED.show();
    delay(10);
  }
}

void rainbow_moving() {
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CHSV(hue + (i * 10), 255, 255);
  }
  EVERY_N_MILLISECONDS(10) {
    hue++;
  }
}

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

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

实验的视频记录(5分10秒)

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

继续尝试棒棒灯

project-image

  【花雕动手做】有趣好玩的音乐可视化系列小项目(16)--热干胶棒棒灯
  项目程序之五:多彩MegunoLink伸缩音乐节拍灯
  模块接线:WS2812B接D6
  MAX4466      UNO
  VCC          5V
  GND         GND
  OUT          A0

代码
/*
  【花雕动手做】有趣好玩的音乐可视化系列小项目(16)--热干胶棒棒灯
  项目程序之五:多彩MegunoLink伸缩音乐节拍灯
  模块接线:WS2812B接D6
  MAX4466      UNO
  VCC          5V
  GND         GND
  OUT          A0
*/

#include<FastLED.h>
#include<MegunoLink.h>
#include<Filter.h>

#define N_PIXELS  13
#define MIC_PIN   A0
#define LED_PIN   6
#define NOISE 10
#define TOP   (N_PIXELS+2)
#define LED_TYPE  WS2811
#define BRIGHTNESS  10
#define COLOR_ORDER GRB

CRGB leds[N_PIXELS];
int lvl = 0, minLvl = 0, maxLvl = 10;

ExponentialFilter<long> ADCFilter(5, 0);

void setup() {
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, N_PIXELS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(BRIGHTNESS);
}

void loop() {
  int n, height;
  n = analogRead(MIC_PIN);
  n = abs(1023 - n);
  n = (n <= NOISE) ? 0 : abs(n - NOISE);
  ADCFilter.Filter(n);
  lvl = ADCFilter.Current();
  //  Serial.print(n);
  //  Serial.print(" ");
  //  Serial.println(lvl);
  height = TOP * (lvl - minLvl) / (long)(maxLvl - minLvl);
  if (height < 0L) height = 0;
  else if (height > TOP) height = TOP;
  for (uint8_t i = 0; i < N_PIXELS; i++) {
    if (i >= height) leds[i] = CRGB(0, 0, 0);
    else leds[i] = Wheel( map( i, 0, N_PIXELS - 1, 30, 150 ) );
  }
  FastLED.show();
}

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

实验的视频记录(1分05秒)

 

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

实验场景图  动态图

project-image

评论

user-avatar
  • 三春牛-创客

    三春牛-创客2023.01.26

    6

    0
    • 三春牛-创客

      三春牛-创客2023.01.26

      厉害了

      0
      icon 他的勋章
        展开更多