8884浏览
楼主: 驴友花雕

[项目] 【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏

[复制链接]

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 21:22:10

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图2

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 21:32:27

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序十八:应用Adafruit_NeoPixel库的入门极简程序

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序十八:应用Adafruit_NeoPixel库的入门极简程序
  5. */
  6. #include <Adafruit_NeoPixel.h>
  7. #define PIN        6 //接脚
  8. #define NUMPIXELS 256 //数量
  9. Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  10. #define DELAYVAL 100 //延时
  11. void setup() {
  12.   pixels.setBrightness(22);//亮度
  13.   pixels.begin();//启动
  14. }
  15. void loop() {
  16.   pixels.clear();
  17.   for (int i = 0; i < NUMPIXELS; i++) {
  18.     pixels.setPixelColor(i, pixels.Color(50, 250, 0));
  19.     pixels.show();
  20.     delay(2);
  21.   }
  22. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-24 07:10:02

实验场景图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-24 08:03:37

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序十九:FHT音乐反应256位动态频谱灯

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序十九:FHT音乐反应256位动态频谱灯
  5. */
  6. #include "FastLED.h"
  7. #define OCTAVE 1 //   // Group buckets into octaves  (use the log output function LOG_OUT 1)
  8. #define OCT_NORM 0 // Don't normalise octave intensities by number of bins
  9. #define FHT_N 256 // set to 256 point fht
  10. #include <FHT.h> // include the library
  11. //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}
  12. // int noise[] = {204,190,108,85,65,65,55,60}; // noise for mega adk
  13. int noise[] = {204, 195, 100, 90, 85, 80, 75, 75}; // noise for NANO
  14. //int noise[] = {204,198,100,85,85,80,80,80};
  15. 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}
  16. 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}
  17. #define LED_PIN     6
  18. #define LED_TYPE    WS2812
  19. #define COLOR_ORDER GRB
  20. // Params for width and height
  21. const uint8_t kMatrixWidth = 8;
  22. const uint8_t kMatrixHeight = 32;//----------was 27
  23. //#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  24. #define NUM_LEDS    256
  25. CRGB leds[NUM_LEDS];
  26. int counter2 = 0;
  27. void setup() {
  28.   Serial.begin(9600);
  29.   delay(1000);
  30.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  31.   FastLED.setBrightness (133);
  32.   fill_solid(leds, NUM_LEDS, CRGB::Black);
  33.   FastLED.show();
  34.   // TIMSK0 = 0; // turn off timer0 for lower jitter
  35.   ADCSRA = 0xe5; // set the adc to free running mode
  36.   ADMUX = 0x40; // use adc0
  37.   DIDR0 = 0x01; // turn off the digital input for adc0
  38. }
  39. void loop() {
  40.   int prev_j[8];
  41.   int beat = 0;
  42.   int prev_oct_j;
  43.   int counter = 0;
  44.   int prev_beat = 0;
  45.   int led_index = 0;
  46.   int saturation = 0;
  47.   int saturation_prev = 0;
  48.   int brightness = 0;
  49.   int brightness_prev = 0;
  50.   while (1) { // reduces jitter
  51.     cli();  // UDRE interrupt slows this way down on arduino1.0
  52.     for (int i = 0 ; i < FHT_N ; i++) { // save 256 samples
  53.       while (!(ADCSRA & 0x10)); // wait for adc to be ready
  54.       ADCSRA = 0xf5; // restart adc
  55.       byte m = ADCL; // fetch adc data
  56.       byte j = ADCH;
  57.       int k = (j << 8) | m; // form into an int
  58.       k -= 0x0200; // form into a signed int
  59.       k <<= 6; // form into a 16b signed int
  60.       fht_input[i] = k; // put real data into bins
  61.     }
  62.     fht_window(); // window the data for better frequency response
  63.     fht_reorder(); // reorder the data before doing the fht
  64.     fht_run(); // process the data in the fht
  65.     fht_mag_octave(); // take the output of the fht  fht_mag_log()
  66.     // every 50th loop, adjust the volume accourding to the value on A2 (Pot)
  67.     if (counter >= 50) {
  68.       ADMUX = 0x40 | (1 & 0x07); // set admux to look at Analogpin A1 - Master Volume
  69.       while (!(ADCSRA & 0x10)); // wait for adc to be ready
  70.       ADCSRA = 0xf5; // restart adc
  71.       delay(10);
  72.       while (!(ADCSRA & 0x10)); // wait for adc to be ready
  73.       ADCSRA = 0xf5; // restart adc
  74.       byte m = ADCL; // fetch adc data
  75.       byte j = ADCH;
  76.       int k = (j << 8) | m; // form into an int
  77.       float master_volume = (k + 0.1) / 1000 + .75; // so the valu will be between ~0.5 and 1.---------------------+.75 was .5
  78.       Serial.println (master_volume);
  79.       for (int i = 1; i < 8; i++) {
  80.         noise_fact_adj[i] = noise_fact[i] * master_volume;
  81.       }
  82.       ADMUX = 0x40 | (0 & 0x07); // set admux back to look at A0 analog pin (to read the microphone input
  83.       counter = 0;
  84.     }
  85.     sei();
  86.     counter++;
  87.     // End of Fourier Transform code - output is stored in fht_oct_out[i].
  88.     // i=0-7 frequency (octave) bins (don't use 0 or 1), fht_oct_out[1]= amplitude of frequency for bin 1
  89.     // for loop a) removes background noise average and takes absolute value b) low / high pass filter as still very noisy
  90.     // c) maps amplitude of octave to a colour between blue and red d) sets pixel colour to amplitude of each frequency (octave)
  91.     for (int i = 1; i < 8; i++) {  // goes through each octave. skip the first 1, which is not useful
  92.       int j;
  93.       j = (fht_oct_out[i] - noise[i]); // take the pink noise average level out, take the asbolute value to avoid negative numbers
  94.       if (j < 10) {
  95.         j = 0;
  96.       }
  97.       j = j * noise_fact_adj[i];
  98.       if (j < 10) {
  99.         j = 0;
  100.       }
  101.       else {
  102.         j = j * noise_fact_adj[i];
  103.         if (j > 180) {
  104.           if (i >= 7) {
  105.             beat += 2;
  106.           }
  107.           else {
  108.             beat += 1;
  109.           }
  110.         }
  111.         j = j / 30;
  112.         j = j * 30; // (force it to more discrete values)
  113.       }
  114.       prev_j[i] = j;
  115.       //     Serial.print(j);
  116.       //     Serial.print(" ");
  117.       // this fills in 11 LED's with interpolated values between each of the 8 OCT values
  118.       if (i >= 2) {
  119.         led_index = 2 * i - 3;
  120.         prev_oct_j = (j + prev_j[i - 1]) / 2;
  121.         saturation = constrain(j + 50, 0, 255); //-----------50 was 30
  122.         saturation_prev = constrain(prev_oct_j + 50, 0, 255);
  123.         brightness = constrain(j, 0, 255);
  124.         brightness_prev = constrain(prev_oct_j, 0, 255);
  125.         if (brightness == 255) {
  126.           saturation = 50;
  127.           brightness = 200;
  128.         }
  129.         if (brightness_prev == 255) {
  130.           saturation_prev = 50;
  131.           brightness_prev = 200;
  132.         }
  133.         for (uint8_t y = 0; y < kMatrixHeight; y++) {
  134.           leds[XY(led_index - 1, y)] = CHSV(j + y * 30, saturation, brightness);
  135.           if (i > 2) {
  136.             prev_oct_j = (j + prev_j[i - 1]) / 2;
  137.             leds[ XY(led_index - 2, y)] = CHSV(prev_oct_j + y * 30, saturation_prev, brightness_prev);
  138.           }
  139.         }
  140.       }
  141.     }
  142.     if (beat >= 7) {
  143.       fill_solid(leds, NUM_LEDS, CRGB::Gray);
  144.       FastLED.setBrightness(200);
  145.     }
  146.     else {
  147.       if (prev_beat != beat) {
  148.         FastLED.setBrightness(40 + beat * beat * 5);
  149.         prev_beat = beat;
  150.       }
  151.     }
  152.     FastLED.show();
  153.     if (beat) {
  154.       counter2 += ((beat + 4) / 2 - 2);
  155.       if (counter2 < 0) {
  156.         counter2 = 1000;
  157.       }
  158.       if (beat > 3 && beat < 7) {
  159.         FastLED.delay (20);
  160.       }
  161.       beat = 0;
  162.     }
  163.     // Serial.println();
  164.   }
  165. }
  166. // Param for different pixel layouts
  167. const bool    kMatrixSerpentineLayout = false;
  168. // Set 'kMatrixSerpentineLayout' to false if your pixels are
  169. // laid out all running the same way, like this:
  170. // Set 'kMatrixSerpentineLayout' to true if your pixels are
  171. // laid out back-and-forth, like this:
  172. uint16_t XY( uint8_t x, uint8_t y)
  173. {
  174.   uint16_t i;
  175.   if ( kMatrixSerpentineLayout == false) {
  176.     i = (y * kMatrixWidth) + x;
  177.   }
  178.   if ( kMatrixSerpentineLayout == true) {
  179.     if ( y & 0x01) {
  180.       // Odd rows run backwards
  181.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  182.       i = (y * kMatrixWidth) + reverseX;
  183.     } else {
  184.       // Even rows run forwards
  185.       i = (y * kMatrixWidth) + x;
  186.     }
  187.   }
  188.   i = (i + counter2) % NUM_LEDS;
  189.   return i;
  190. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-24 09:26:36

实验场景图  动态图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-24 09:33:24

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-27 10:21:35

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序二十:Arduino 和 FastLED多彩音乐灯

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序二十:Arduino 和 FastLED多彩音乐灯
  5. */
  6. #include <FastLED.h>
  7. #define SAMPLEPERIODUS 200
  8. #define MIC_PIN A0
  9. #define LED_DT 6
  10. #define COLOR_ORDER GRB
  11. #define LED_TYPE WS2812
  12. #define NUM_LEDS 256
  13. uint8_t max_bright = 33;
  14. struct CRGB leds[NUM_LEDS];
  15. CRGBPalette16 currentPalette = RainbowColors_p;
  16. CRGBPalette16 targetPalette;
  17. void setup() {
  18.   pinMode(LED_BUILTIN, OUTPUT);
  19.   LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  20.   FastLED.setBrightness(max_bright);
  21. }
  22. float bassFilter(float sample) {
  23.   static float xv[3] = {0, 0, 0}, yv[3] = {0, 0, 0};
  24.   xv[0] = xv[1]; xv[1] = xv[2];
  25.   xv[2] = sample / 9.1f;
  26.   yv[0] = yv[1]; yv[1] = yv[2];
  27.   yv[2] = (xv[2] - xv[0]) + (-0.7960060012f * yv[0]) + (1.7903124146f * yv[1]);
  28.   return yv[2];
  29. }
  30. float envelopeFilter(float sample) {
  31.   static float xv[2] = {0, 0}, yv[2] = {0, 0};
  32.   xv[0] = xv[1];
  33.   xv[1] = sample / 160.f;
  34.   yv[0] = yv[1];
  35.   yv[1] = (xv[0] + xv[1]) + (0.9875119299f * yv[0]);
  36.   return yv[1];
  37. }
  38. float beatFilter(float sample) {
  39.   static float xv[3] = {0, 0, 0}, yv[3] = {0, 0, 0};
  40.   xv[0] = xv[1]; xv[1] = xv[2];
  41.   xv[2] = sample / 7.015f;
  42.   yv[0] = yv[1]; yv[1] = yv[2];
  43.   yv[2] = (xv[2] - xv[0]) + (-0.7169861741f * yv[0]) + (1.4453653501f * yv[1]);
  44.   return yv[2];
  45. }
  46. void loop() {
  47.   unsigned long time = micros();
  48.   float sample, value, envelope, beat, thresh, micLev;
  49.   for (uint8_t i = 0; ; ++i) {
  50.     sample = (float)analogRead(MIC_PIN);
  51.     micLev = ((micLev * 67) + sample) / 68;
  52.     sample -= micLev;
  53.     value = bassFilter(sample);
  54.     value = abs(value);
  55.     envelope = envelopeFilter(value);
  56.     if (i == 200) {
  57.       beat = beatFilter(envelope);
  58.       thresh = 0.02f * 75.;
  59.       if (beat > thresh) {
  60.         digitalWrite(LED_BUILTIN, LOW);
  61.         int strt = random8(NUM_LEDS / 2);
  62.         int ende = strt + random8(NUM_LEDS / 2);
  63.         for (int i = strt; i < ende; i++) {
  64.           uint8_t index = inoise8(i * 30, millis() + i * 30);
  65.           leds[i] = ColorFromPalette(currentPalette, index, 255, LINEARBLEND);
  66.         }
  67.       } else {
  68.         digitalWrite(LED_BUILTIN, HIGH);
  69.       }
  70.       i = 0;
  71.     }
  72.     EVERY_N_SECONDS(5) {
  73.       uint8_t baseC = random8();
  74.       targetPalette = CRGBPalette16(CHSV(baseC + random8(32), 255, random8(128, 255)),
  75.                                     CHSV(baseC + random8(64), 255, random8(128, 255)),
  76.                                     CHSV(baseC + random8(64), 192, random8(128, 255)),
  77.                                     CHSV(baseC + random8(),   255, random8(128, 255)));
  78.     }
  79.     EVERY_N_MILLISECONDS(50) {
  80.       uint8_t maxChanges = 24;
  81.       nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges);
  82.     }
  83.     EVERY_N_MILLIS(50) {
  84.       fadeToBlackBy(leds, NUM_LEDS, 64);
  85.       FastLED.show();
  86.     }
  87.     for (unsigned long up = time + SAMPLEPERIODUS; time > 20 && time < up; time = micros()) {  }
  88.   } // for i
  89. } // loop()
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-27 10:24:13

实验的视频记录
优酷:
B站:https://www.bilibili.com/video/B ... 87403d97f8d3cc0b7e5



回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-27 19:51:05

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序二十一:通过快速傅里叶变换在8*32灯板上显示频谱

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序二十一:通过快速傅里叶变换在8*32灯板上显示频谱
  5. */
  6. #include  "arduinoFFT.h"
  7. #include <FastLED.h>   
  8. #define NUM_LEDS 256   
  9. #define LED_TYPE WS2812
  10. #define COLOR_ORDER GRB
  11. arduinoFFT FFT = arduinoFFT();
  12. CRGB leds[NUM_LEDS];           
  13. #define CHANNEL A0
  14. #define DATA_PIN 6
  15. const uint8_t max_bright = 2;         
  16. const uint16_t samples = NUM_LEDS / 4;
  17. const byte halfsamples = samples / 2;  
  18. uint8_t gHue;                          
  19. int value;                             
  20. double vReal[samples];                 
  21. double vImag[samples];                 
  22. char toData[halfsamples];              
  23. int pointJump[halfsamples];
  24. int uJump[halfsamples];     
  25. int dJump[halfsamples];   
  26. int uValue;                 
  27. int dValue;                 
  28. int tValue;                 
  29. int toDown = 0;            
  30. uint8_t toDownSpeed = 3;   
  31. int pointDown = 0;         
  32. uint8_t pointDownSpeed = 9;
  33. void setup(){
  34.   delay(100);              
  35.   Serial.println("Ready");
  36.   FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  37.   FastLED.setBrightness(max_bright);
  38. }
  39. void loop(){
  40.   FastLED.clear();                        
  41.   EVERY_N_MILLISECONDS(10) {
  42.     gHue += 10;  
  43.   }
  44.   for (int i = 0; i < samples; i++)        
  45.   {
  46.     value = analogRead(CHANNEL);
  47.     vReal[i] = value;      
  48.     vImag[i] = 0.0;         
  49.   }
  50.   
  51.   FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
  52.   FFT.Compute(vReal, vImag, samples, FFT_FORWARD);
  53.   FFT.ComplexToMagnitude(vReal, vImag, samples);
  54.   
  55.   for (int i = 0; i < halfsamples; i++)
  56.   {
  57.     toData[i] = vReal[i + halfsamples / 2];   
  58.     toData[i] = constrain(toData[i], 0, 100);
  59.     toData[i] = map(toData[i], 0, 100, 1, 7);
  60.   }
  61.   for (int i = 0; i < halfsamples; i++)
  62.   {
  63.     uValue = toData[i];   
  64.     uJump[i]++;            
  65.     if (uValue > uJump[i])
  66.     {
  67.       uValue = uJump[i];
  68.     }
  69.     else
  70.     {
  71.       uJump[i] = uValue;
  72.     }
  73.     dValue = uValue;
  74.     toDown++;                     
  75.     if (toDown % toDownSpeed == 0)
  76.     {
  77.       dJump[i]--;
  78.       toDown = 0;
  79.     }
  80.     if (dValue > pointJump[i])
  81.     {
  82.       dJump[i] = dValue;
  83.     }
  84.     else
  85.     {
  86.       dValue = dJump[i];
  87.     }
  88.     tValue = uValue;                     
  89.     pointDown++;                        
  90.     if (pointDown % pointDownSpeed == 0)
  91.     {
  92.       pointJump[i]--;
  93.       pointDown = 0;  
  94.     }
  95.     if (tValue > pointJump[i])
  96.     {
  97.       pointJump[i] = tValue;
  98.     }
  99.     else
  100.     {
  101.       tValue = pointJump[i];
  102.     }
  103.     fill_rainbow(leds + 8 * i, uValue, gHue, 30);
  104.     fill_rainbow(leds + 8 * i, dValue, gHue, 30);
  105.     fill_solid(leds + 8 * i + tValue, 1, CRGB::White);
  106.    
  107.   }
  108.   FastLED.show();
  109.   delay(10);      
  110. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-27 19:55:30

实验场景图  动态图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-27 20:03:59

实验的视频记录
优酷:
B站:https://www.bilibili.com/video/B ... 87403d97f8d3cc0b7e5



回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-27 20:05:29

实验场景图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail