NeoPixelFunFadeInOut.ino 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // NeoPixelFunFadeInOut
  2. // This example will randomly pick a color and fade all pixels to that color, then
  3. // it will fade them to black and restart over
  4. //
  5. // This example demonstrates the use of a single animation channel to animate all
  6. // the pixels at once.
  7. //
  8. #include <NeoPixelBus.h>
  9. #include <NeoPixelAnimator.h>
  10. const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip
  11. const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
  12. const uint8_t AnimationChannels = 1; // we only need one as all the pixels are animated at once
  13. NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
  14. // For Esp8266, the Pin is omitted and it uses GPIO3 due to DMA hardware use.
  15. // There are other Esp8266 alternative methods that provide more pin options, but also have
  16. // other side effects.
  17. // for details see wiki linked here https://github.com/Makuna/NeoPixelBus/wiki/ESP8266-NeoMethods
  18. NeoPixelAnimator animations(AnimationChannels); // NeoPixel animation management object
  19. boolean fadeToColor = true; // general purpose variable used to store effect state
  20. // what is stored for state is specific to the need, in this case, the colors.
  21. // basically what ever you need inside the animation update function
  22. struct MyAnimationState
  23. {
  24. RgbColor StartingColor;
  25. RgbColor EndingColor;
  26. };
  27. // one entry per pixel to match the animation timing manager
  28. MyAnimationState animationState[AnimationChannels];
  29. void SetRandomSeed()
  30. {
  31. uint32_t seed;
  32. // random works best with a seed that can use 31 bits
  33. // analogRead on a unconnected pin tends toward less than four bits
  34. seed = analogRead(0);
  35. delay(1);
  36. for (int shifts = 3; shifts < 31; shifts += 3)
  37. {
  38. seed ^= analogRead(0) << shifts;
  39. delay(1);
  40. }
  41. randomSeed(seed);
  42. }
  43. // simple blend function
  44. void BlendAnimUpdate(const AnimationParam& param)
  45. {
  46. // this gets called for each animation on every time step
  47. // progress will start at 0.0 and end at 1.0
  48. // we use the blend function on the RgbColor to mix
  49. // color based on the progress given to us in the animation
  50. RgbColor updatedColor = RgbColor::LinearBlend(
  51. animationState[param.index].StartingColor,
  52. animationState[param.index].EndingColor,
  53. param.progress);
  54. // apply the color to the strip
  55. for (uint16_t pixel = 0; pixel < PixelCount; pixel++)
  56. {
  57. strip.SetPixelColor(pixel, updatedColor);
  58. }
  59. }
  60. void FadeInFadeOutRinseRepeat(float luminance)
  61. {
  62. if (fadeToColor)
  63. {
  64. // Fade upto a random color
  65. // we use HslColor object as it allows us to easily pick a hue
  66. // with the same saturation and luminance so the colors picked
  67. // will have similiar overall brightness
  68. RgbColor target = HslColor(random(360) / 360.0f, 1.0f, luminance);
  69. uint16_t time = random(800, 2000);
  70. animationState[0].StartingColor = strip.GetPixelColor(0);
  71. animationState[0].EndingColor = target;
  72. animations.StartAnimation(0, time, BlendAnimUpdate);
  73. }
  74. else
  75. {
  76. // fade to black
  77. uint16_t time = random(600, 700);
  78. animationState[0].StartingColor = strip.GetPixelColor(0);
  79. animationState[0].EndingColor = RgbColor(0);
  80. animations.StartAnimation(0, time, BlendAnimUpdate);
  81. }
  82. // toggle to the next effect state
  83. fadeToColor = !fadeToColor;
  84. }
  85. void setup()
  86. {
  87. strip.Begin();
  88. strip.Show();
  89. SetRandomSeed();
  90. }
  91. void loop()
  92. {
  93. if (animations.IsAnimating())
  94. {
  95. // the normal loop just needs these two to run the active animations
  96. animations.UpdateAnimations();
  97. strip.Show();
  98. }
  99. else
  100. {
  101. // no animation runnning, start some
  102. //
  103. FadeInFadeOutRinseRepeat(0.2f); // 0.0 = black, 0.25 is normal, 0.5 is bright
  104. }
  105. }