NeoPixelBrightness.ino 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // NeoPixelBrightness
  2. // This example will cycle brightness from high to low of
  3. // three pixels colored Red, Green, Blue.
  4. // This demonstrates the use of the NeoPixelBrightnessBus
  5. // with integrated brightness support
  6. //
  7. // There is serial output of the current state so you can
  8. // confirm and follow along
  9. //
  10. #include <NeoPixelBrightnessBus.h> // instead of NeoPixelBus.h
  11. const uint16_t PixelCount = 3; // this example assumes 3 pixels, making it smaller will cause a failure
  12. const uint8_t PixelPin = 14; // make sure to set this to the correct pin, ignored for Esp8266
  13. #define colorSaturation 255 // saturation of color constants
  14. RgbColor red(colorSaturation, 0, 0);
  15. RgbColor green(0, colorSaturation, 0);
  16. RgbColor blue(0, 0, colorSaturation);
  17. // Make sure to provide the correct color order feature
  18. // for your NeoPixels
  19. NeoPixelBrightnessBus<NeoRgbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
  20. // you loose the original color the lower the dim value used
  21. // here due to quantization
  22. const uint8_t c_MinBrightness = 8;
  23. const uint8_t c_MaxBrightness = 255;
  24. int8_t direction; // current direction of dimming
  25. void setup()
  26. {
  27. Serial.begin(115200);
  28. while (!Serial); // wait for serial attach
  29. Serial.println();
  30. Serial.println("Initializing...");
  31. Serial.flush();
  32. // this resets all the neopixels to an off state
  33. strip.Begin();
  34. strip.Show();
  35. direction = -1; // default to dim first
  36. Serial.println();
  37. Serial.println("Running...");
  38. // set our three original colors
  39. strip.SetPixelColor(0, red);
  40. strip.SetPixelColor(1, green);
  41. strip.SetPixelColor(2, blue);
  42. strip.Show();
  43. }
  44. void loop()
  45. {
  46. uint8_t brightness = strip.GetBrightness();
  47. Serial.println(brightness);
  48. delay(100);
  49. // swap diection of dim when limits are reached
  50. //
  51. if (direction < 0 && brightness <= c_MinBrightness)
  52. {
  53. direction = 1;
  54. }
  55. else if (direction > 0 && brightness >= c_MaxBrightness)
  56. {
  57. direction = -1;
  58. }
  59. // apply dimming
  60. brightness += direction;
  61. strip.SetBrightness(brightness);
  62. // show the results
  63. strip.Show();
  64. }