NeoPixelFunRandomChange.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. //NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount);
  15. //
  16. // NeoEsp8266Uart800KbpsMethod uses GPI02 instead
  17. NeoPixelAnimator animations(PixelCount); // NeoPixel animation management object
  18. // what is stored for state is specific to the need, in this case, the colors.
  19. // Basically what ever you need inside the animation update function
  20. struct MyAnimationState
  21. {
  22. RgbColor StartingColor;
  23. RgbColor EndingColor;
  24. };
  25. // one entry per pixel to match the animation timing manager
  26. MyAnimationState animationState[PixelCount];
  27. void SetRandomSeed()
  28. {
  29. uint32_t seed;
  30. // random works best with a seed that can use 31 bits
  31. // analogRead on a unconnected pin tends toward less than four bits
  32. seed = analogRead(0);
  33. delay(1);
  34. for (int shifts = 3; shifts < 31; shifts += 3)
  35. {
  36. seed ^= analogRead(0) << shifts;
  37. delay(1);
  38. }
  39. // Serial.println(seed);
  40. randomSeed(seed);
  41. }
  42. // simple blend function
  43. void BlendAnimUpdate(const AnimationParam& param)
  44. {
  45. // this gets called for each animation on every time step
  46. // progress will start at 0.0 and end at 1.0
  47. // we use the blend function on the RgbColor to mix
  48. // color based on the progress given to us in the animation
  49. RgbColor updatedColor = RgbColor::LinearBlend(
  50. animationState[param.index].StartingColor,
  51. animationState[param.index].EndingColor,
  52. param.progress);
  53. // apply the color to the strip
  54. strip.SetPixelColor(param.index, updatedColor);
  55. }
  56. void PickRandom(float luminance)
  57. {
  58. // pick random count of pixels to animate
  59. uint16_t count = random(PixelCount);
  60. while (count > 0)
  61. {
  62. // pick a random pixel
  63. uint16_t pixel = random(PixelCount);
  64. // pick random time and random color
  65. // we use HslColor object as it allows us to easily pick a color
  66. // with the same saturation and luminance
  67. uint16_t time = random(100, 400);
  68. animationState[pixel].StartingColor = strip.GetPixelColor(pixel);
  69. animationState[pixel].EndingColor = HslColor(random(360) / 360.0f, 1.0f, luminance);
  70. animations.StartAnimation(pixel, time, BlendAnimUpdate);
  71. count--;
  72. }
  73. }
  74. void setup()
  75. {
  76. strip.Begin();
  77. strip.Show();
  78. SetRandomSeed();
  79. }
  80. void loop()
  81. {
  82. if (animations.IsAnimating())
  83. {
  84. // the normal loop just needs these two to run the active animations
  85. animations.UpdateAnimations();
  86. strip.Show();
  87. }
  88. else
  89. {
  90. // no animations runnning, start some
  91. //
  92. PickRandom(0.2f); // 0.0 = black, 0.25 is normal, 0.5 is bright
  93. }
  94. }