NeoPixelFunFadeInOut.ino 3.9 KB

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