NeoPixelCylon.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // NeoPixelCylon
  2. // This example will move a Cylon Red Eye back and forth across the
  3. // the full collection of pixels on the strip.
  4. //
  5. // This will demonstrate the use of the NeoEase animation ease methods; that provide
  6. // simulated acceleration to the animations.
  7. //
  8. //
  9. #include <NeoPixelBus.h>
  10. #include <NeoPixelAnimator.h>
  11. const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip
  12. const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
  13. const RgbColor CylonEyeColor(HtmlColor(0x7f0000));
  14. NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
  15. // for esp8266 omit the pin
  16. //NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount);
  17. NeoPixelAnimator animations(2); // only ever need 2 animations
  18. uint16_t lastPixel = 0; // track the eye position
  19. int8_t moveDir = 1; // track the direction of movement
  20. // uncomment one of the lines below to see the effects of
  21. // changing the ease function on the movement animation
  22. AnimEaseFunction moveEase =
  23. // NeoEase::Linear;
  24. // NeoEase::QuadraticInOut;
  25. // NeoEase::CubicInOut;
  26. NeoEase::QuarticInOut;
  27. // NeoEase::QuinticInOut;
  28. // NeoEase::SinusoidalInOut;
  29. // NeoEase::ExponentialInOut;
  30. // NeoEase::CircularInOut;
  31. void FadeAll(uint8_t darkenBy)
  32. {
  33. RgbColor color;
  34. for (uint16_t indexPixel = 0; indexPixel < strip.PixelCount(); indexPixel++)
  35. {
  36. color = strip.GetPixelColor(indexPixel);
  37. color.Darken(darkenBy);
  38. strip.SetPixelColor(indexPixel, color);
  39. }
  40. }
  41. void FadeAnimUpdate(const AnimationParam& param)
  42. {
  43. if (param.state == AnimationState_Completed)
  44. {
  45. FadeAll(10);
  46. animations.RestartAnimation(param.index);
  47. }
  48. }
  49. void MoveAnimUpdate(const AnimationParam& param)
  50. {
  51. // apply the movement animation curve
  52. float progress = moveEase(param.progress);
  53. // use the curved progress to calculate the pixel to effect
  54. uint16_t nextPixel;
  55. if (moveDir > 0)
  56. {
  57. nextPixel = progress * PixelCount;
  58. }
  59. else
  60. {
  61. nextPixel = (1.0f - progress) * PixelCount;
  62. }
  63. // if progress moves fast enough, we may move more than
  64. // one pixel, so we update all between the calculated and
  65. // the last
  66. if (lastPixel != nextPixel)
  67. {
  68. for (uint16_t i = lastPixel + moveDir; i != nextPixel; i += moveDir)
  69. {
  70. strip.SetPixelColor(i, CylonEyeColor);
  71. }
  72. }
  73. strip.SetPixelColor(nextPixel, CylonEyeColor);
  74. lastPixel = nextPixel;
  75. if (param.state == AnimationState_Completed)
  76. {
  77. // reverse direction of movement
  78. moveDir *= -1;
  79. // done, time to restart this position tracking animation/timer
  80. animations.RestartAnimation(param.index);
  81. }
  82. }
  83. void SetupAnimations()
  84. {
  85. // fade all pixels providing a tail that is longer the faster
  86. // the pixel moves.
  87. animations.StartAnimation(0, 5, FadeAnimUpdate);
  88. // take several seconds to move eye fron one side to the other
  89. animations.StartAnimation(1, 2000, MoveAnimUpdate);
  90. }
  91. void setup()
  92. {
  93. strip.Begin();
  94. strip.Show();
  95. SetupAnimations();
  96. }
  97. void loop()
  98. {
  99. // this is all that is needed to keep it running
  100. // and avoiding using delay() is always a good thing for
  101. // any timing related routines
  102. animations.UpdateAnimations();
  103. strip.Show();
  104. }