楼主: eagler8

[教程] 【Arduino】108种传感器模块系列实验(61)---WS2812直条8位模块

[复制链接]

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 11:21 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目三十五:将围绕一系列像素移动光迹,轨迹将有一个缓慢衰减的尾部

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目三十五:将围绕一系列像素移动光迹,轨迹将有一个缓慢衰减的尾部
  5. */

  6. // NeoPixelFunLoop
  7. // This example will move a trail of light around a series of pixels.  
  8. // A ring formation of pixels looks best.  
  9. // The trail will have a slowly fading tail.
  10. //
  11. // This will demonstrate the use of the NeoPixelAnimator.
  12. // It shows the advanced use an animation to control the modification and
  13. // starting of other animations.
  14. // It also shows the normal use of animating colors.
  15. // It also demonstrates the ability to share an animation channel rather than
  16. // hard code them to pixels.
  17. //

  18. #include <NeoPixelBus.h>
  19. #include <NeoPixelAnimator.h>


  20. const uint16_t PixelCount = 8; // make sure to set this to the number of pixels in your strip
  21. const uint16_t PixelPin = 6;  // make sure to set this to the correct pin, ignored for Esp8266
  22. const uint16_t AnimCount = PixelCount / 5 * 2 + 1; // we only need enough animations for the tail and one extra

  23. const uint16_t PixelFadeDuration = 300; // third of a second
  24. // one second divide by the number of pixels = loop once a second
  25. const uint16_t NextPixelMoveDuration = 1000 / PixelCount; // how fast we move through the pixels

  26. NeoGamma<NeoGammaTableMethod> colorGamma; // for any fade animations, best to correct gamma

  27. NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
  28. // For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use.  
  29. // There are other Esp8266 alternative methods that provide more pin options, but also have
  30. // other side effects.
  31. // for details see wiki linked here https://github.com/Makuna/NeoPixelBus/wiki/ESP8266-NeoMethods

  32. // what is stored for state is specific to the need, in this case, the colors and
  33. // the pixel to animate;
  34. // basically what ever you need inside the animation update function
  35. struct MyAnimationState
  36. {
  37.     RgbColor StartingColor;
  38.     RgbColor EndingColor;
  39.     uint16_t IndexPixel; // which pixel this animation is effecting
  40. };

  41. NeoPixelAnimator animations(AnimCount); // NeoPixel animation management object
  42. MyAnimationState animationState[AnimCount];
  43. uint16_t frontPixel = 0;  // the front of the loop
  44. RgbColor frontColor;  // the color at the front of the loop

  45. void SetRandomSeed()
  46. {
  47.     uint32_t seed;

  48.     // random works best with a seed that can use 31 bits
  49.     // analogRead on a unconnected pin tends toward less than four bits
  50.     seed = analogRead(0);
  51.     delay(1);

  52.     for (int shifts = 3; shifts < 31; shifts += 3)
  53.     {
  54.         seed ^= analogRead(0) << shifts;
  55.         delay(1);
  56.     }

  57.     // Serial.println(seed);
  58.     randomSeed(seed);
  59. }

  60. void FadeOutAnimUpdate(const AnimationParam& param)
  61. {
  62.     // this gets called for each animation on every time step
  63.     // progress will start at 0.0 and end at 1.0
  64.     // we use the blend function on the RgbColor to mix
  65.     // color based on the progress given to us in the animation
  66.     RgbColor updatedColor = RgbColor::LinearBlend(
  67.         animationState[param.index].StartingColor,
  68.         animationState[param.index].EndingColor,
  69.         param.progress);
  70.     // apply the color to the strip
  71.     strip.SetPixelColor(animationState[param.index].IndexPixel,
  72.         colorGamma.Correct(updatedColor));
  73. }

  74. void LoopAnimUpdate(const AnimationParam& param)
  75. {
  76.     // wait for this animation to complete,
  77.     // we are using it as a timer of sorts
  78.     if (param.state == AnimationState_Completed)
  79.     {
  80.         // done, time to restart this position tracking animation/timer
  81.         animations.RestartAnimation(param.index);

  82.         // pick the next pixel inline to start animating
  83.         //
  84.         frontPixel = (frontPixel + 1) % PixelCount; // increment and wrap
  85.         if (frontPixel == 0)
  86.         {
  87.             // we looped, lets pick a new front color
  88.             frontColor = HslColor(random(360) / 360.0f, 1.0f, 0.25f);
  89.         }

  90.         uint16_t indexAnim;
  91.         // do we have an animation available to use to animate the next front pixel?
  92.         // if you see skipping, then either you are going to fast or need to increase
  93.         // the number of animation channels
  94.         if (animations.NextAvailableAnimation(&indexAnim, 1))
  95.         {
  96.             animationState[indexAnim].StartingColor = frontColor;
  97.             animationState[indexAnim].EndingColor = RgbColor(0, 0, 0);
  98.             animationState[indexAnim].IndexPixel = frontPixel;

  99.             animations.StartAnimation(indexAnim, PixelFadeDuration, FadeOutAnimUpdate);
  100.         }
  101.     }
  102. }

  103. void setup()
  104. {
  105.     strip.Begin();
  106.     strip.Show();

  107.     SetRandomSeed();

  108.     // we use the index 0 animation to time how often we move to the next
  109.     // pixel in the strip
  110.     animations.StartAnimation(0, NextPixelMoveDuration, LoopAnimUpdate);
  111. }


  112. void loop()
  113. {
  114.     // this is all that is needed to keep it running
  115.     // and avoiding using delay() is always a good thing for
  116.     // any timing related routines
  117.     animations.UpdateAnimations();
  118.     strip.Show();
  119. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 11:24 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目三十六:随机选择一个像素,然后启动动画将它们从当前颜色混合到

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目三十六:随机选择一个像素,然后启动动画将它们从当前颜色混合到
  5. */

  6. #include <NeoPixelBus.h>
  7. #include <NeoPixelAnimator.h>

  8. const uint16_t PixelCount = 8; // make sure to set this to the number of pixels in your strip
  9. const uint8_t PixelPin = 6;  // make sure to set this to the correct pin, ignored for Esp8266

  10. NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
  11. // For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use.  
  12. // There are other Esp8266 alternative methods that provide more pin options, but also have
  13. // other side effects.
  14. // for details see wiki linked here https://github.com/Makuna/NeoPixelBus/wiki/ESP8266-NeoMethods

  15. NeoPixelAnimator animations(PixelCount); // NeoPixel animation management object

  16. // what is stored for state is specific to the need, in this case, the colors.
  17. // Basically what ever you need inside the animation update function
  18. struct MyAnimationState
  19. {
  20.     RgbColor StartingColor;
  21.     RgbColor EndingColor;
  22. };

  23. // one entry per pixel to match the animation timing manager
  24. MyAnimationState animationState[PixelCount];

  25. void SetRandomSeed()
  26. {
  27.     uint32_t seed;

  28.     // random works best with a seed that can use 31 bits
  29.     // analogRead on a unconnected pin tends toward less than four bits
  30.     seed = analogRead(0);
  31.     delay(1);

  32.     for (int shifts = 3; shifts < 31; shifts += 3)
  33.     {
  34.         seed ^= analogRead(0) << shifts;
  35.         delay(1);
  36.     }

  37.     // Serial.println(seed);
  38.     randomSeed(seed);
  39. }

  40. // simple blend function
  41. void BlendAnimUpdate(const AnimationParam& param)
  42. {
  43.     // this gets called for each animation on every time step
  44.     // progress will start at 0.0 and end at 1.0
  45.     // we use the blend function on the RgbColor to mix
  46.     // color based on the progress given to us in the animation
  47.     RgbColor updatedColor = RgbColor::LinearBlend(
  48.         animationState[param.index].StartingColor,
  49.         animationState[param.index].EndingColor,
  50.         param.progress);
  51.     // apply the color to the strip
  52.     strip.SetPixelColor(param.index, updatedColor);
  53. }

  54. void PickRandom(float luminance)
  55. {
  56.     // pick random count of pixels to animate
  57.     uint16_t count = random(PixelCount);
  58.     while (count > 0)
  59.     {
  60.         // pick a random pixel
  61.         uint16_t pixel = random(PixelCount);

  62.         // pick random time and random color
  63.         // we use HslColor object as it allows us to easily pick a color
  64.         // with the same saturation and luminance
  65.         uint16_t time = random(100, 400);
  66.         animationState[pixel].StartingColor = strip.GetPixelColor(pixel);
  67.         animationState[pixel].EndingColor = HslColor(random(360) / 360.0f, 1.0f, luminance);

  68.         animations.StartAnimation(pixel, time, BlendAnimUpdate);

  69.         count--;
  70.     }
  71. }

  72. void setup()
  73. {
  74.     strip.Begin();
  75.     strip.Show();

  76.     SetRandomSeed();
  77. }


  78. void loop()
  79. {
  80.     if (animations.IsAnimating())
  81.     {
  82.         // the normal loop just needs these two to run the active animations
  83.         animations.UpdateAnimations();
  84.         strip.Show();
  85.     }
  86.     else
  87.     {
  88.         // no animations runnning, start some
  89.         //
  90.         PickRandom(0.2f); // 0.0 = black, 0.25 is normal, 0.5 is bright
  91.     }
  92. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 11:31 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目三十七:随机使用 31 位的种子效果淡入淡出动画

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目三十七:随机使用 31 位的种子效果淡入淡出动画
  5. */

  6. #include <NeoPixelBus.h>
  7. #include <NeoPixelAnimator.h>


  8. const uint16_t PixelCount = 8; // make sure to set this to the number of pixels in your strip
  9. const uint16_t PixelPin = 6;  // make sure to set this to the correct pin, ignored for Esp8266
  10. const uint16_t AnimCount = 1; // we only need one
  11. const uint16_t TailLength = 6; // length of the tail, must be shorter than PixelCount
  12. const float MaxLightness = 0.4f; // max lightness at the head of the tail (0.5f is full bright)

  13. NeoGamma<NeoGammaTableMethod> colorGamma; // for any fade animations, best to correct gamma

  14. NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
  15. // for esp8266 omit the pin
  16. //NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount);

  17. NeoPixelAnimator animations(AnimCount); // NeoPixel animation management object

  18. void SetRandomSeed()
  19. {
  20.     uint32_t seed;

  21.     // random works best with a seed that can use 31 bits
  22.     // analogRead on a unconnected pin tends toward less than four bits
  23.     seed = analogRead(0);
  24.     delay(1);

  25.     for (int shifts = 3; shifts < 31; shifts += 3)
  26.     {
  27.         seed ^= analogRead(0) << shifts;
  28.         delay(1);
  29.     }

  30.     // Serial.println(seed);
  31.     randomSeed(seed);
  32. }

  33. void LoopAnimUpdate(const AnimationParam& param)
  34. {
  35.     // wait for this animation to complete,
  36.     // we are using it as a timer of sorts
  37.     if (param.state == AnimationState_Completed)
  38.     {
  39.         // done, time to restart this position tracking animation/timer
  40.         animations.RestartAnimation(param.index);

  41.         // rotate the complete strip one pixel to the right on every update
  42.         strip.RotateRight(1);
  43.     }
  44. }

  45. void DrawTailPixels()
  46. {
  47.     // using Hsl as it makes it easy to pick from similiar saturated colors
  48.     float hue = random(360) / 360.0f;
  49.     for (uint16_t index = 0; index < strip.PixelCount() && index <= TailLength; index++)
  50.     {
  51.         float lightness = index * MaxLightness / TailLength;
  52.         RgbColor color = HslColor(hue, 1.0f, lightness);

  53.         strip.SetPixelColor(index, colorGamma.Correct(color));
  54.     }
  55. }

  56. void setup()
  57. {
  58.     strip.Begin();
  59.     strip.Show();

  60.     SetRandomSeed();

  61.     // Draw the tail that will be rotated through all the rest of the pixels
  62.     DrawTailPixels();

  63.     // we use the index 0 animation to time how often we rotate all the pixels
  64.     animations.StartAnimation(0, 66, LoopAnimUpdate);
  65. }


  66. void loop()
  67. {
  68.     // this is all that is needed to keep it running
  69.     // and avoiding using delay() is always a good thing for
  70.     // any timing related routines
  71.     animations.UpdateAnimations();
  72.     strip.Show();
  73. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 11:43 | 显示全部楼层
实验场景图

22.jpg

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-23 17:01 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目三十八:音乐反应式 LED 灯条

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目三十八:音乐反应式 LED 灯条
  5. */

  6. #include <Adafruit_NeoPixel.h>
  7. #include <math.h>
  8. #define N_PIXELS  8
  9. #define MIC_PIN   A0
  10. #define LED_PIN    6
  11. #define SAMPLE_WINDOW   5
  12. #define PEAK_HANG 24
  13. #define PEAK_FALL 4
  14. #define INPUT_FLOOR 10
  15. #define INPUT_CEILING 50
  16. byte peak = 16;
  17. unsigned int sample;

  18. byte Count = 0;
  19. byte HangCount = 0;

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

  21. void setup() {
  22.   Serial.begin(9600);
  23.   analogReference(EXTERNAL);
  24.   strip.setBrightness(22);
  25.   strip.show();
  26.   strip.begin();
  27. }

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

  29.   float OriginalRange = 0;
  30.   float NewRange = 0;
  31.   float zeroRefCurVal = 0;
  32.   float normalizedCurVal = 0;
  33.   float rangedValue = 0;
  34.   boolean invFlag = 0;

  35.   if (curve > 10) curve = 10;
  36.   if (curve < -10) curve = -10;

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

  39.   if (inputValue < originalMin) {
  40.     inputValue = originalMin;
  41.   }
  42.   if (inputValue > originalMax) {
  43.     inputValue = originalMax;
  44.   }

  45.   OriginalRange = originalMax - originalMin;

  46.   if (newEnd > newBegin) {
  47.     NewRange = newEnd - newBegin;
  48.   }
  49.   else
  50.   {
  51.     NewRange = newBegin - newEnd;
  52.     invFlag = 1;
  53.   }

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

  56.   Serial.print(OriginalRange, DEC);
  57.   Serial.print("   ");
  58.   Serial.print(NewRange, DEC);
  59.   Serial.print("   ");
  60.   Serial.println(zeroRefCurVal, DEC);
  61.   Serial.println();
  62.   delay(10);

  63.   if (originalMin > originalMax ) {
  64.     return 0;
  65.   }

  66.   if (invFlag == 0) {
  67.     rangedValue =  (pow(normalizedCurVal, curve) * NewRange) + newBegin;
  68.   }
  69.   else
  70.   {
  71.     rangedValue =  newBegin - (pow(normalizedCurVal, curve) * NewRange);
  72.   }
  73.   return rangedValue;
  74. }

  75. void loop() {
  76.   unsigned long startMillis = millis();
  77.   float peakToPeak = 0;

  78.   unsigned int signalMax = 0;
  79.   unsigned int signalMin = 1023;
  80.   unsigned int c, y;

  81.   while (millis() - startMillis < SAMPLE_WINDOW)
  82.   {
  83.     sample = analogRead(MIC_PIN);
  84.     if (sample < 1024)
  85.     {
  86.       if (sample > signalMax)
  87.       {
  88.         signalMax = sample;
  89.       }
  90.       else if (sample < signalMin)
  91.       {
  92.         signalMin = sample;
  93.       }
  94.     }
  95.   }
  96.   peakToPeak = signalMax - signalMin;

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

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

  101.   if (c < peak) {
  102.     peak = c;
  103.     HangCount = 0;
  104.   }
  105.   if (c <= strip.numPixels()) {
  106.     drawLine(strip.numPixels(), strip.numPixels() - c, strip.Color(0, 0, 0));
  107.   }

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

  111.   if (HangCount > PEAK_HANG) {
  112.     if (++Count >= PEAK_FALL) {
  113.       peak++;
  114.       Count = 0;
  115.     }
  116.   }
  117.   else {
  118.     HangCount++;
  119.   }
  120. }

  121. void drawLine(uint8_t from, uint8_t to, uint32_t c) {
  122.   uint8_t fromTemp;
  123.   if (from > to) {
  124.     fromTemp = from;
  125.     from = to;
  126.     to = fromTemp;
  127.   }
  128.   for (int i = from; i <= to; i++) {
  129.     strip.setPixelColor(i, c);
  130.   }
  131. }

  132. uint32_t Wheel(byte WheelPos) {
  133.   if (WheelPos < 85) {
  134.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  135.   }
  136.   else if (WheelPos < 170) {
  137.     WheelPos -= 85;
  138.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  139.   }
  140.   else {
  141.     WheelPos -= 170;
  142.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  143.   }
  144. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-23 17:04 | 显示全部楼层
实验串口返回情况

23.jpg

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-23 17:54 | 显示全部楼层
  实验场景图

24.jpg

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-23 18:44 | 显示全部楼层
本帖最后由 eagler8 于 2021-9-23 19:21 编辑

  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目三十八:音乐反应式 LED 灯条(实验视频)
https://v.youku.com/v_show/id_XN ... hcb.playlsit.page.1






签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-23 18:46 | 显示全部楼层

  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目三十九:16灯音乐反应式 LED 条


  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目三十九:16灯音乐反应式 LED 条
  5. */

  6. #include <Adafruit_NeoPixel.h>
  7. #include <math.h>
  8. #define N_PIXELS  16
  9. #define MIC_PIN   A0
  10. #define LED_PIN    6
  11. #define SAMPLE_WINDOW   5
  12. #define PEAK_HANG 24
  13. #define PEAK_FALL 4
  14. #define INPUT_FLOOR 10
  15. #define INPUT_CEILING 50
  16. byte peak = 16;
  17. unsigned int sample;

  18. byte Count = 0;
  19. byte HangCount = 0;

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

  21. void setup() {
  22.   Serial.begin(9600);
  23.   analogReference(EXTERNAL);
  24.   strip.setBrightness(22);
  25.   strip.show();
  26.   strip.begin();
  27. }

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

  29.   float OriginalRange = 0;
  30.   float NewRange = 0;
  31.   float zeroRefCurVal = 0;
  32.   float normalizedCurVal = 0;
  33.   float rangedValue = 0;
  34.   boolean invFlag = 0;

  35.   if (curve > 10) curve = 10;
  36.   if (curve < -10) curve = -10;

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

  39.   if (inputValue < originalMin) {
  40.     inputValue = originalMin;
  41.   }
  42.   if (inputValue > originalMax) {
  43.     inputValue = originalMax;
  44.   }

  45.   OriginalRange = originalMax - originalMin;

  46.   if (newEnd > newBegin) {
  47.     NewRange = newEnd - newBegin;
  48.   }
  49.   else
  50.   {
  51.     NewRange = newBegin - newEnd;
  52.     invFlag = 1;
  53.   }

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

  56.   Serial.print(OriginalRange, DEC);
  57.   Serial.print("   ");
  58.   Serial.print(NewRange, DEC);
  59.   Serial.print("   ");
  60.   Serial.println(zeroRefCurVal, DEC);
  61.   Serial.println();
  62.   delay(10);

  63.   if (originalMin > originalMax ) {
  64.     return 0;
  65.   }

  66.   if (invFlag == 0) {
  67.     rangedValue =  (pow(normalizedCurVal, curve) * NewRange) + newBegin;
  68.   }
  69.   else
  70.   {
  71.     rangedValue =  newBegin - (pow(normalizedCurVal, curve) * NewRange);
  72.   }
  73.   return rangedValue;
  74. }

  75. void loop() {
  76.   unsigned long startMillis = millis();
  77.   float peakToPeak = 0;

  78.   unsigned int signalMax = 0;
  79.   unsigned int signalMin = 1023;
  80.   unsigned int c, y;

  81.   while (millis() - startMillis < SAMPLE_WINDOW)
  82.   {
  83.     sample = analogRead(MIC_PIN);
  84.     if (sample < 1024)
  85.     {
  86.       if (sample > signalMax)
  87.       {
  88.         signalMax = sample;
  89.       }
  90.       else if (sample < signalMin)
  91.       {
  92.         signalMin = sample;
  93.       }
  94.     }
  95.   }
  96.   peakToPeak = signalMax - signalMin;

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

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

  101.   if (c < peak) {
  102.     peak = c;
  103.     HangCount = 0;
  104.   }
  105.   if (c <= strip.numPixels()) {
  106.     drawLine(strip.numPixels(), strip.numPixels() - c, strip.Color(0, 0, 0));
  107.   }

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

  111.   if (HangCount > PEAK_HANG) {
  112.     if (++Count >= PEAK_FALL) {
  113.       peak++;
  114.       Count = 0;
  115.     }
  116.   }
  117.   else {
  118.     HangCount++;
  119.   }
  120. }

  121. void drawLine(uint8_t from, uint8_t to, uint32_t c) {
  122.   uint8_t fromTemp;
  123.   if (from > to) {
  124.     fromTemp = from;
  125.     from = to;
  126.     to = fromTemp;
  127.   }
  128.   for (int i = from; i <= to; i++) {
  129.     strip.setPixelColor(i, c);
  130.   }
  131. }

  132. uint32_t Wheel(byte WheelPos) {
  133.   if (WheelPos < 85) {
  134.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  135.   }
  136.   else if (WheelPos < 170) {
  137.     WheelPos -= 85;
  138.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  139.   }
  140.   else {
  141.     WheelPos -= 170;
  142.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  143.   }
  144. }
复制代码




签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-23 18:47 | 显示全部楼层
  实验场景图

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

本版积分规则

热门推荐

原价299元【语音开发板套件】限时免费领!
原价299元【语音开发板套
教你让OLED动起来!多重字符串版!
教你让OLED动起来!多重字
大家都知道:arduino单片机是单线程的 而上次教程中的多段字符串的运行速度必须一致
TTGO T8 1.7.1使用TFT_eSPI库驱动2.4寸ILI9341屏幕显示板载SD内图片
TTGO T8 1.7.1使用TFT_eSP
TTGO T8 1.7.1采用Espressif官方ESP32-WROVER模块制作,4MB闪存和8MB PSRAM,支持TF
TTGO TFT屏幕Arduino使用的小结
TTGO TFT屏幕Arduino使用
TTGO TFT屏幕是一款ESP32和1.44寸LCD屏幕组合的产品,屏幕由ST7789驱动。ESP32自带520
分享我的第一个点灯程序——家庭控制中心
分享我的第一个点灯程序—
2020年,在B站上初识ESP8266,被ESP8266的低门槛、高性价比深深吸引,2020年6月20日,
Copyright   ©2015-2016  Arduino中文社区  Powered by©Discuz!   
快速回复 返回顶部 返回列表