NeoPixelFunRandomChange.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // NeoPixelFunRandomChange
  2. // This example will randomly select a number pixels and then
  3. // start an animation to blend them from their current color to
  4. // randomly selected a color
  5. //
  6. #include <NeoPixelBus.h>
  7. #include <NeoPixelAnimator.h>
  8. const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip
  9. const uint8_t PixelPin = 2; // 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. }