NeoPixelRotateLoop.ino 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // NeoPixelFunLoop
  2. // This example will move a trail of light around a series of pixels.
  3. // A ring formation of pixels looks best.
  4. // The trail will have a slowly fading tail.
  5. //
  6. // This will demonstrate the use of the RotateRight method.
  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 uint16_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
  12. const uint16_t AnimCount = 1; // we only need one
  13. const uint16_t TailLength = 6; // length of the tail, must be shorter than PixelCount
  14. const float MaxLightness = 0.4f; // max lightness at the head of the tail (0.5f is full bright)
  15. NeoGamma<NeoGammaTableMethod> colorGamma; // for any fade animations, best to correct gamma
  16. NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
  17. // for esp8266 omit the pin
  18. //NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount);
  19. NeoPixelAnimator animations(AnimCount); // NeoPixel animation management object
  20. void SetRandomSeed()
  21. {
  22. uint32_t seed;
  23. // random works best with a seed that can use 31 bits
  24. // analogRead on a unconnected pin tends toward less than four bits
  25. seed = analogRead(0);
  26. delay(1);
  27. for (int shifts = 3; shifts < 31; shifts += 3)
  28. {
  29. seed ^= analogRead(0) << shifts;
  30. delay(1);
  31. }
  32. // Serial.println(seed);
  33. randomSeed(seed);
  34. }
  35. void LoopAnimUpdate(const AnimationParam& param)
  36. {
  37. // wait for this animation to complete,
  38. // we are using it as a timer of sorts
  39. if (param.state == AnimationState_Completed)
  40. {
  41. // done, time to restart this position tracking animation/timer
  42. animations.RestartAnimation(param.index);
  43. // rotate the complete strip one pixel to the right on every update
  44. strip.RotateRight(1);
  45. }
  46. }
  47. void DrawTailPixels()
  48. {
  49. // using Hsl as it makes it easy to pick from similiar saturated colors
  50. float hue = random(360) / 360.0f;
  51. for (uint16_t index = 0; index < strip.PixelCount() && index <= TailLength; index++)
  52. {
  53. float lightness = index * MaxLightness / TailLength;
  54. RgbColor color = HslColor(hue, 1.0f, lightness);
  55. strip.SetPixelColor(index, colorGamma.Correct(color));
  56. }
  57. }
  58. void setup()
  59. {
  60. strip.Begin();
  61. strip.Show();
  62. SetRandomSeed();
  63. // Draw the tail that will be rotated through all the rest of the pixels
  64. DrawTailPixels();
  65. // we use the index 0 animation to time how often we rotate all the pixels
  66. animations.StartAnimation(0, 66, LoopAnimUpdate);
  67. }
  68. void loop()
  69. {
  70. // this is all that is needed to keep it running
  71. // and avoiding using delay() is always a good thing for
  72. // any timing related routines
  73. animations.UpdateAnimations();
  74. strip.Show();
  75. }