forked from Makuna/NeoPixelBus
V2
This commit is contained in:
1 parent
34deb7f8ad
commit
62f699c120
35 files changed
+4765
-1871
No files matched your search
@@ -0,0 +1,226 @@
|
||||
// NeoPixelAnimation
|
||||
// This example will randomly pick a new color for each pixel and animate
|
||||
// the current color to the new color over a random small amount of time, using
|
||||
// a randomly selected animation curve.
|
||||
// It will repeat this process once all pixels have finished the animation
|
||||
//
|
||||
// This will demonstrate the use of the NeoPixelAnimator extended time feature.
|
||||
// This feature allows for different time scales to be used, allowing slow extended
|
||||
// animations to be created.
|
||||
//
|
||||
// This will demonstrate the use of the NeoEase animation ease methods; that provide
|
||||
// simulated acceleration to the animations.
|
||||
//
|
||||
// It also includes platform specific code for Esp8266 that demonstrates easy
|
||||
// animation state and function definition inline. This is not available on AVR
|
||||
// Arduinos; but the AVR compatible code is also included for comparison.
|
||||
//
|
||||
// The example includes some serial output that you can follow along with as it
|
||||
// does the animation.
|
||||
//
|
||||
|
||||
#include <NeoPixelBus.h>
|
||||
#include <NeoPixelAnimator.h>
|
||||
|
||||
const uint16_t PixelCount = 4; // make sure to set this to the number of pixels in your strip
|
||||
const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
|
||||
|
||||
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
// For Esp8266, the Pin is ignored and it uses GPIO3.
|
||||
// There are other Esp8266 alternative methods that provide more pin options, but also have
|
||||
// other side effects.
|
||||
//NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
// NeoEsp8266Uart800KbpsMethod also ignores the pin parameter and uses GPI02
|
||||
//NeoPixelBus<NeoGrbFeature, NeoEsp8266BitBang800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
// NeoEsp8266Uart800KbpsMethod will work with all but pin 16, but is not stable with WiFi
|
||||
// being active
|
||||
|
||||
// NeoPixel animation time management object
|
||||
NeoPixelAnimator animations(PixelCount, NEO_CENTISECONDS);
|
||||
|
||||
// create with enough animations to have one per pixel, depending on the animation
|
||||
// effect, you may need more or less.
|
||||
//
|
||||
// since the normal animation time range is only about 65 seconds, by passing timescale value
|
||||
// to the NeoPixelAnimator constructor we can increase the time range, but we also increase
|
||||
// the time between the animation updates.
|
||||
// NEO_CENTISECONDS will update the animations every 100th of a second rather than the default
|
||||
// of a 1000th of a second, but the time range will now extend from about 65 seconds to about
|
||||
// 10.9 minutes. But you must remember that the values passed to StartAnimations are now
|
||||
// in centiseconds.
|
||||
//
|
||||
// Possible values from 1 to 32768, and there some helpful constants defined as...
|
||||
// NEO_MILLISECONDS 1 // ~65 seconds max duration, ms updates
|
||||
// NEO_CENTISECONDS 10 // ~10.9 minutes max duration, centisecond updates
|
||||
// NEO_DECISECONDS 100 // ~1.8 hours max duration, decisecond updates
|
||||
// NEO_SECONDS 1000 // ~18.2 hours max duration, second updates
|
||||
// NEO_DECASECONDS 10000 // ~7.5 days, 10 second updates
|
||||
//
|
||||
|
||||
#ifdef ARDUINO_ARCH_AVR
|
||||
// for AVR, you need to manage the state due to lack of STL/compiler support
|
||||
// for Esp8266 you can define the function using a lambda and state is created for you
|
||||
// see below for an example
|
||||
struct MyAnimationState
|
||||
{
|
||||
RgbColor StartingColor; // the color the animation starts at
|
||||
RgbColor EndingColor; // the color the animation will end at
|
||||
AnimEaseFunction Easeing; // the acceleration curve it will use
|
||||
};
|
||||
|
||||
MyAnimationState animationState[PixelCount];
|
||||
// one entry per pixel to match the animation timing manager
|
||||
|
||||
void AnimUpdate(const AnimationParam& param)
|
||||
{
|
||||
// first apply an easing (curve) to the animation
|
||||
// this simulates acceleration to the effect
|
||||
float progress = animationState[param.index].Easeing(param.progress);
|
||||
|
||||
// this gets called for each animation on every time step
|
||||
// progress will start at 0.0 and end at 1.0
|
||||
// we use the blend function on the RgbColor to mix
|
||||
// color based on the progress given to us in the animation
|
||||
RgbColor updatedColor = RgbColor::LinearBlend(
|
||||
animationState[param.index].StartingColor,
|
||||
animationState[param.index].EndingColor,
|
||||
progress);
|
||||
// apply the color to the strip
|
||||
strip.SetPixelColor(param.index, updatedColor);
|
||||
}
|
||||
#endif
|
||||
|
||||
void SetRandomSeed()
|
||||
{
|
||||
uint32_t seed;
|
||||
|
||||
// random works best with a seed that can use 31 bits
|
||||
// analogRead on a unconnected pin tends toward less than four bits
|
||||
seed = analogRead(0);
|
||||
delay(1);
|
||||
|
||||
for (int shifts = 3; shifts < 31; shifts += 3)
|
||||
{
|
||||
seed ^= analogRead(0) << shifts;
|
||||
delay(1);
|
||||
}
|
||||
|
||||
// Serial.println(seed);
|
||||
randomSeed(seed);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial); // wait for serial attach
|
||||
|
||||
strip.Begin();
|
||||
strip.Show();
|
||||
|
||||
SetRandomSeed();
|
||||
|
||||
// just pick some colors
|
||||
for (uint16_t pixel = 0; pixel < PixelCount; pixel++)
|
||||
{
|
||||
RgbColor color = RgbColor(random(255), random(255), random(255));
|
||||
strip.SetPixelColor(pixel, color);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
Serial.println("Running...");
|
||||
}
|
||||
|
||||
|
||||
void SetupAnimationSet()
|
||||
{
|
||||
// setup some animations
|
||||
for (uint16_t pixel = 0; pixel < PixelCount; pixel++)
|
||||
{
|
||||
const uint8_t peak = 128;
|
||||
|
||||
// pick a random duration of the animation for this pixel
|
||||
// since values are centiseconds, the range is 1 - 4 seconds
|
||||
uint16_t time = random(100, 400);
|
||||
|
||||
// each animation starts with the color that was present
|
||||
RgbColor originalColor = strip.GetPixelColor(pixel);
|
||||
// and ends with a random color
|
||||
RgbColor targetColor = RgbColor(random(peak), random(peak), random(peak));
|
||||
// with the random ease function
|
||||
AnimEaseFunction easing;
|
||||
|
||||
switch (random(3))
|
||||
{
|
||||
case 0:
|
||||
easing = NeoEase::CubicIn;
|
||||
break;
|
||||
case 1:
|
||||
easing = NeoEase::CubicOut;
|
||||
break;
|
||||
case 2:
|
||||
easing = NeoEase::QuadraticInOut;
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef ARDUINO_ARCH_AVR
|
||||
// each animation starts with the color that was present
|
||||
animationState[pixel].StartingColor = originalColor;
|
||||
// and ends with a random color
|
||||
animationState[pixel].EndingColor = targetColor;
|
||||
// using the specific curve
|
||||
animationState[pixel].Easeing = easing;
|
||||
|
||||
// now use the animation state we just calculated and start the animation
|
||||
// which will continue to run and call the update function until it completes
|
||||
animations.StartAnimation(pixel, time, AnimUpdate);
|
||||
#else
|
||||
// we must supply a function that will define the animation, in this example
|
||||
// we are using "lambda expression" to define the function inline, which gives
|
||||
// us an easy way to "capture" the originalColor and targetColor for the call back.
|
||||
//
|
||||
// this function will get called back when ever the animation needs to change
|
||||
// the state of the pixel, it will provide a animation progress value
|
||||
// from 0.0 (start of animation) to 1.0 (end of animation)
|
||||
//
|
||||
// we use this progress value to define how we want to animate in this case
|
||||
// we call RgbColor::LinearBlend which will return a color blended between
|
||||
// the values given, by the amount passed, hich is also a float value from 0.0-1.0.
|
||||
// then we set the color.
|
||||
//
|
||||
// There is no need for the MyAnimationState struct as the compiler takes care
|
||||
// of those details for us
|
||||
AnimUpdateCallback animUpdate = [=](const AnimationParam& param)
|
||||
{
|
||||
// progress will start at 0.0 and end at 1.0
|
||||
// we convert to the curve we want
|
||||
float progress = easing(param.progress);
|
||||
|
||||
// use the curve value to apply to the animation
|
||||
RgbColor updatedColor = RgbColor::LinearBlend(originalColor, targetColor, progress);
|
||||
strip.SetPixelColor(pixel, updatedColor);
|
||||
};
|
||||
|
||||
// now use the animation properties we just calculated and start the animation
|
||||
// which will continue to run and call the update function until it completes
|
||||
animations.StartAnimation(pixel, time, animUpdate);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (animations.IsAnimating())
|
||||
{
|
||||
// the normal loop just needs these two to run the active animations
|
||||
animations.UpdateAnimations();
|
||||
strip.Show();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println();
|
||||
Serial.println("Setup Next Set...");
|
||||
// example function that sets up some animations
|
||||
SetupAnimationSet();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
#include <NeoPixelBus.h>
|
||||
|
||||
#define pixelCount 4
|
||||
|
||||
NeoPixelBus strip = NeoPixelBus(pixelCount, 8);
|
||||
uint16_t effectState = 0;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
strip.Begin();
|
||||
strip.Show();
|
||||
SetRandomSeed();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
// There are three fun functions that implement different effects
|
||||
// uncomment one at a time and upload to see the effect
|
||||
|
||||
// LoopAround(192, 200); // very interesting on rings of NeoPixels
|
||||
PickRandom(128);
|
||||
// FadeInFadeOutRinseRepeat(192);
|
||||
|
||||
// start animating
|
||||
strip.StartAnimating();
|
||||
|
||||
// wait until no more animations are running
|
||||
while (strip.IsAnimating())
|
||||
{
|
||||
strip.UpdateAnimations();
|
||||
strip.Show();
|
||||
delay(31); // ~30hz change cycle
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void FadeInFadeOutRinseRepeat(uint8_t peak)
|
||||
{
|
||||
if (effectState == 0)
|
||||
{
|
||||
for (uint8_t pixel = 0; pixel < pixelCount; pixel++)
|
||||
{
|
||||
uint16_t time = random(800,1000);
|
||||
strip.LinearFadePixelColor(time, pixel, RgbColor(random(peak), random(peak), random(peak)));
|
||||
}
|
||||
}
|
||||
else if (effectState == 1)
|
||||
{
|
||||
for (uint8_t pixel = 0; pixel < pixelCount; pixel++)
|
||||
{
|
||||
uint16_t time = random(600,700);
|
||||
strip.LinearFadePixelColor(time, pixel, RgbColor(0, 0, 0));
|
||||
}
|
||||
}
|
||||
effectState = (effectState + 1) % 2; // next effectState and keep within the number of effectStates
|
||||
|
||||
}
|
||||
|
||||
void PickRandom(uint8_t peak)
|
||||
{
|
||||
|
||||
// pick random set of pixels to animate
|
||||
uint8_t count = random(pixelCount);
|
||||
while (count > 0)
|
||||
{
|
||||
uint8_t pixel = random(pixelCount);
|
||||
|
||||
// configure the animations
|
||||
RgbColor color; // = strip.getPixelColor(pixel);
|
||||
|
||||
color = RgbColor(random(peak), random(peak), random(peak));
|
||||
|
||||
|
||||
uint16_t time = random(100,400);
|
||||
strip.LinearFadePixelColor( time, pixel, color);
|
||||
|
||||
count--;
|
||||
}
|
||||
}
|
||||
|
||||
void LoopAround(uint8_t peak, uint16_t speed)
|
||||
{
|
||||
// Looping around the ring sample
|
||||
uint16_t prevPixel;
|
||||
RgbColor prevColor;
|
||||
|
||||
// fade previous one dark
|
||||
prevPixel = (effectState + (pixelCount - 5)) % pixelCount;
|
||||
strip.LinearFadePixelColor(speed, prevPixel, RgbColor(0, 0, 0));
|
||||
|
||||
// fade previous one dark
|
||||
prevPixel = (effectState + (pixelCount - 4)) % pixelCount;
|
||||
prevColor = strip.GetPixelColor( prevPixel );
|
||||
prevColor.Darken(prevColor.CalculateBrightness() / 2);
|
||||
strip.LinearFadePixelColor(speed, prevPixel, prevColor);
|
||||
|
||||
// fade previous one dark
|
||||
prevPixel = (effectState + (pixelCount - 3)) % pixelCount;
|
||||
prevColor = strip.GetPixelColor( prevPixel );
|
||||
prevColor.Darken(prevColor.CalculateBrightness() / 2);
|
||||
strip.LinearFadePixelColor(speed, prevPixel, prevColor);
|
||||
|
||||
// fade previous one dark
|
||||
prevPixel = (effectState + (pixelCount - 2)) % pixelCount;
|
||||
prevColor = strip.GetPixelColor( prevPixel );
|
||||
prevColor.Darken(prevColor.CalculateBrightness() / 2);
|
||||
strip.LinearFadePixelColor(speed, prevPixel, prevColor);
|
||||
|
||||
// fade previous one dark
|
||||
prevPixel = (effectState + (pixelCount - 1)) % pixelCount;
|
||||
prevColor = strip.GetPixelColor( prevPixel );
|
||||
prevColor.Darken(prevColor.CalculateBrightness() / 2);
|
||||
strip.LinearFadePixelColor(speed, prevPixel, prevColor);
|
||||
|
||||
// fade current one light
|
||||
strip.LinearFadePixelColor(speed, effectState, RgbColor(random(peak), random(peak), random(peak)));
|
||||
effectState = (effectState + 1) % pixelCount;
|
||||
}
|
||||
|
||||
void SetRandomSeed()
|
||||
{
|
||||
uint32_t seed;
|
||||
|
||||
// random works best with a seed that can use 31 bits
|
||||
// analogRead on a unconnected pin tends toward less than four bits
|
||||
seed = analogRead(0);
|
||||
delay(1);
|
||||
|
||||
for (int shifts = 3; shifts < 31; shifts += 3)
|
||||
{
|
||||
seed ^= analogRead(0) << shifts;
|
||||
delay(1);
|
||||
}
|
||||
|
||||
// Serial.println(seed);
|
||||
randomSeed(seed);
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
// NeoPixelFunFadeInOut
|
||||
// This example will randomly pick a color and fade all pixels to that color, then
|
||||
// it will fade them to black and restart over
|
||||
//
|
||||
// This example demonstrates the use of a single animation channel to animate all
|
||||
// the pixels at once.
|
||||
//
|
||||
#include <NeoPixelBus.h>
|
||||
#include <NeoPixelAnimator.h>
|
||||
|
||||
const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip
|
||||
const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
|
||||
const uint8_t AnimationChannels = 1; // we only need one as all the pixels are animated at once
|
||||
|
||||
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
// For Esp8266, the Pin is ignored and it uses GPIO3.
|
||||
// There are other Esp8266 alternative methods that provide more pin options, but also have
|
||||
// other side effects.
|
||||
//NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
// NeoEsp8266Uart800KbpsMethod also ignores the pin parameter and uses GPI02
|
||||
//NeoPixelBus<NeoGrbFeature, NeoEsp8266BitBang800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
// NeoEsp8266Uart800KbpsMethod will work with all but pin 16, but is not stable with WiFi
|
||||
// being active
|
||||
|
||||
NeoPixelAnimator animations(AnimationChannels); // NeoPixel animation management object
|
||||
|
||||
uint16_t effectState = 0; // general purpose variable used to store effect state
|
||||
|
||||
|
||||
// what is stored for state is specific to the need, in this case, the colors.
|
||||
// basically what ever you need inside the animation update function
|
||||
struct MyAnimationState
|
||||
{
|
||||
RgbColor StartingColor;
|
||||
RgbColor EndingColor;
|
||||
};
|
||||
|
||||
// one entry per pixel to match the animation timing manager
|
||||
MyAnimationState animationState[AnimationChannels];
|
||||
|
||||
void SetRandomSeed()
|
||||
{
|
||||
uint32_t seed;
|
||||
|
||||
// random works best with a seed that can use 31 bits
|
||||
// analogRead on a unconnected pin tends toward less than four bits
|
||||
seed = analogRead(0);
|
||||
delay(1);
|
||||
|
||||
for (int shifts = 3; shifts < 31; shifts += 3)
|
||||
{
|
||||
seed ^= analogRead(0) << shifts;
|
||||
delay(1);
|
||||
}
|
||||
|
||||
randomSeed(seed);
|
||||
}
|
||||
|
||||
// simple blend function
|
||||
void BlendAnimUpdate(const AnimationParam& param)
|
||||
{
|
||||
// this gets called for each animation on every time step
|
||||
// progress will start at 0.0 and end at 1.0
|
||||
// we use the blend function on the RgbColor to mix
|
||||
// color based on the progress given to us in the animation
|
||||
RgbColor updatedColor = RgbColor::LinearBlend(
|
||||
animationState[param.index].StartingColor,
|
||||
animationState[param.index].EndingColor,
|
||||
param.progress);
|
||||
|
||||
// apply the color to the strip
|
||||
for (uint16_t pixel = 0; pixel < PixelCount; pixel++)
|
||||
{
|
||||
strip.SetPixelColor(pixel, updatedColor);
|
||||
}
|
||||
}
|
||||
|
||||
void FadeInFadeOutRinseRepeat(float luminance)
|
||||
{
|
||||
if (effectState == 0)
|
||||
{
|
||||
// Fade upto a random color
|
||||
// we use HslColor object as it allows us to easily pick a hue
|
||||
// with the same saturation and luminance so the colors picked
|
||||
// will have similiar overall brightness
|
||||
RgbColor target = HslColor(random(360) / 360.0f, 1.0f, luminance);
|
||||
uint16_t time = random(800, 2000);
|
||||
|
||||
animationState[0].StartingColor = strip.GetPixelColor(0);
|
||||
animationState[0].EndingColor = target;
|
||||
|
||||
animations.StartAnimation(0, time, BlendAnimUpdate);
|
||||
}
|
||||
else if (effectState == 1)
|
||||
{
|
||||
// fade to black
|
||||
uint16_t time = random(600, 700);
|
||||
|
||||
animationState[0].StartingColor = strip.GetPixelColor(0);
|
||||
animationState[0].EndingColor = RgbColor(0);
|
||||
|
||||
animations.StartAnimation(0, time, BlendAnimUpdate);
|
||||
}
|
||||
|
||||
// toggle to the next effect state
|
||||
effectState = (effectState + 1) % 2;
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
strip.Begin();
|
||||
strip.Show();
|
||||
|
||||
SetRandomSeed();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (animations.IsAnimating())
|
||||
{
|
||||
// the normal loop just needs these two to run the active animations
|
||||
animations.UpdateAnimations();
|
||||
strip.Show();
|
||||
}
|
||||
else
|
||||
{
|
||||
// no animation runnning, start some
|
||||
//
|
||||
FadeInFadeOutRinseRepeat(0.2f); // 0.0 = black, 0.25 is normal, 0.5 is bright
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
// NeoPixelFunLoop
|
||||
// This example will move a trail of light around a series of pixels.
|
||||
// A ring formation of pixels looks best.
|
||||
// The trail will have a slowly fading tail.
|
||||
//
|
||||
// This will demonstrate the use of the NeoPixelAnimator.
|
||||
// It shows the advanced use an animation to control the modification and
|
||||
// starting of other animations.
|
||||
// It also shows the normal use of animating colors.
|
||||
// It also demonstrates the ability to share an animation channel rather than
|
||||
// hard code them to pixels.
|
||||
//
|
||||
|
||||
#include <NeoPixelBus.h>
|
||||
#include <NeoPixelAnimator.h>
|
||||
|
||||
|
||||
const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip
|
||||
const uint16_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
|
||||
const uint16_t AnimCount = 8; // we only need about 8 animations, one to track movement, and the rest to actually animate
|
||||
|
||||
const uint16_t PixelFadeDuration = 400; // half a second
|
||||
// one second divide by the number of pixels = loop once a second
|
||||
const uint16_t NextPixelMoveDuration = 1000 / PixelCount; // how fast we move through the pixels
|
||||
|
||||
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
// For Esp8266, the Pin is ignored and it uses GPIO3.
|
||||
// There are other Esp8266 alternative methods that provide more pin options, but also have
|
||||
// other side effects.
|
||||
//NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
// NeoEsp8266Uart800KbpsMethod also ignores the pin parameter and uses GPI02
|
||||
//NeoPixelBus<NeoGrbFeature, NeoEsp8266BitBang800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
// NeoEsp8266Uart800KbpsMethod will work with all but pin 16, but is not stable with WiFi
|
||||
// being active
|
||||
|
||||
// what is stored for state is specific to the need, in this case, the colors and
|
||||
// the pixel to animate;
|
||||
// basically what ever you need inside the animation update function
|
||||
struct MyAnimationState
|
||||
{
|
||||
RgbColor StartingColor;
|
||||
RgbColor EndingColor;
|
||||
uint16_t IndexPixel; // which pixel this animation is effecting
|
||||
};
|
||||
|
||||
NeoPixelAnimator animations(AnimCount); // NeoPixel animation management object
|
||||
MyAnimationState animationState[AnimCount];
|
||||
uint16_t frontPixel = 0; // the front of the loop
|
||||
RgbColor frontColor; // the color at the front of the loop
|
||||
|
||||
void SetRandomSeed()
|
||||
{
|
||||
uint32_t seed;
|
||||
|
||||
// random works best with a seed that can use 31 bits
|
||||
// analogRead on a unconnected pin tends toward less than four bits
|
||||
seed = analogRead(0);
|
||||
delay(1);
|
||||
|
||||
for (int shifts = 3; shifts < 31; shifts += 3)
|
||||
{
|
||||
seed ^= analogRead(0) << shifts;
|
||||
delay(1);
|
||||
}
|
||||
|
||||
// Serial.println(seed);
|
||||
randomSeed(seed);
|
||||
}
|
||||
|
||||
void FadeOutAnimUpdate(const AnimationParam& param)
|
||||
{
|
||||
// this gets called for each animation on every time step
|
||||
// progress will start at 0.0 and end at 1.0
|
||||
// we use the blend function on the RgbColor to mix
|
||||
// color based on the progress given to us in the animation
|
||||
RgbColor updatedColor = RgbColor::LinearBlend(
|
||||
animationState[param.index].StartingColor,
|
||||
animationState[param.index].EndingColor,
|
||||
param.progress);
|
||||
// apply the color to the strip
|
||||
strip.SetPixelColor(animationState[param.index].IndexPixel, updatedColor);
|
||||
}
|
||||
|
||||
void LoopAnimUpdate(const AnimationParam& param)
|
||||
{
|
||||
// wait for this animation to complete,
|
||||
// we are using it as a timer of sorts
|
||||
if (param.state == AnimationState_Completed)
|
||||
{
|
||||
// done, time to restart this position tracking animation/timer
|
||||
animations.RestartAnimation(param.index);
|
||||
|
||||
// pick the next pixel inline to start animating
|
||||
//
|
||||
frontPixel = (frontPixel + 1) % PixelCount; // increment and wrap
|
||||
if (frontPixel == 1)
|
||||
{
|
||||
// we looped, lets pick a new front color
|
||||
frontColor = HslColor(random(360) / 360.0f, 1.0f, 0.25f);
|
||||
}
|
||||
|
||||
uint16_t indexAnim;
|
||||
// do we have an animation available to use to animate the next front pixel?
|
||||
// if you see skipping, then either you are going to fast or need to increase
|
||||
// the number of animation channels
|
||||
if (animations.NextAvailableAnimation(&indexAnim, 0))
|
||||
{
|
||||
animationState[indexAnim].StartingColor = frontColor;
|
||||
animationState[indexAnim].EndingColor = RgbColor(0, 0, 0);
|
||||
animationState[indexAnim].IndexPixel = frontPixel;
|
||||
|
||||
animations.StartAnimation(indexAnim, PixelFadeDuration, FadeOutAnimUpdate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
strip.Begin();
|
||||
strip.Show();
|
||||
|
||||
SetRandomSeed();
|
||||
|
||||
// we use the index 0 animation to time how often we move to the next
|
||||
// pixel in the strip
|
||||
animations.StartAnimation(0, NextPixelMoveDuration, LoopAnimUpdate);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
// this is all that is needed to keep it running
|
||||
// and avoiding using delay() is always a good thing for
|
||||
// any timing related routines
|
||||
animations.UpdateAnimations();
|
||||
strip.Show();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
// NeoPixelFunRandomChange
|
||||
// This example will randomly select a number pixels and then
|
||||
// start an animation to blend them from their current color to
|
||||
// randomly selected a color
|
||||
//
|
||||
|
||||
#include <NeoPixelBus.h>
|
||||
#include <NeoPixelAnimator.h>
|
||||
|
||||
const uint16_t PixelCount = 16; // make sure to set this to the number of pixels in your strip
|
||||
const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
|
||||
|
||||
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
// For Esp8266, the Pin is ignored and it uses GPIO3.
|
||||
// There are other Esp8266 alternative methods that provide more pin options, but also have
|
||||
// other side effects.
|
||||
//NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
// NeoEsp8266Uart800KbpsMethod also ignores the pin parameter and uses GPI02
|
||||
//NeoPixelBus<NeoGrbFeature, NeoEsp8266BitBang800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
// NeoEsp8266Uart800KbpsMethod will work with all but pin 16, but is not stable with WiFi
|
||||
// being active
|
||||
|
||||
NeoPixelAnimator animations(PixelCount); // NeoPixel animation management object
|
||||
|
||||
// what is stored for state is specific to the need, in this case, the colors.
|
||||
// Basically what ever you need inside the animation update function
|
||||
struct MyAnimationState
|
||||
{
|
||||
RgbColor StartingColor;
|
||||
RgbColor EndingColor;
|
||||
};
|
||||
|
||||
// one entry per pixel to match the animation timing manager
|
||||
MyAnimationState animationState[PixelCount];
|
||||
|
||||
void SetRandomSeed()
|
||||
{
|
||||
uint32_t seed;
|
||||
|
||||
// random works best with a seed that can use 31 bits
|
||||
// analogRead on a unconnected pin tends toward less than four bits
|
||||
seed = analogRead(0);
|
||||
delay(1);
|
||||
|
||||
for (int shifts = 3; shifts < 31; shifts += 3)
|
||||
{
|
||||
seed ^= analogRead(0) << shifts;
|
||||
delay(1);
|
||||
}
|
||||
|
||||
// Serial.println(seed);
|
||||
randomSeed(seed);
|
||||
}
|
||||
|
||||
// simple blend function
|
||||
void BlendAnimUpdate(const AnimationParam& param)
|
||||
{
|
||||
// this gets called for each animation on every time step
|
||||
// progress will start at 0.0 and end at 1.0
|
||||
// we use the blend function on the RgbColor to mix
|
||||
// color based on the progress given to us in the animation
|
||||
RgbColor updatedColor = RgbColor::LinearBlend(
|
||||
animationState[param.index].StartingColor,
|
||||
animationState[param.index].EndingColor,
|
||||
param.progress);
|
||||
// apply the color to the strip
|
||||
strip.SetPixelColor(param.index, updatedColor);
|
||||
}
|
||||
|
||||
void PickRandom(float luminance)
|
||||
{
|
||||
// pick random count of pixels to animate
|
||||
uint16_t count = random(PixelCount);
|
||||
while (count > 0)
|
||||
{
|
||||
// pick a random pixel
|
||||
uint16_t pixel = random(PixelCount);
|
||||
|
||||
// pick random time and random color
|
||||
// we use HslColor object as it allows us to easily pick a color
|
||||
// with the same saturation and luminance
|
||||
uint16_t time = random(100, 400);
|
||||
animationState[pixel].StartingColor = strip.GetPixelColor(pixel);
|
||||
animationState[pixel].EndingColor = HslColor(random(360) / 360.0f, 1.0f, luminance);
|
||||
|
||||
animations.StartAnimation(pixel, time, BlendAnimUpdate);
|
||||
|
||||
count--;
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
strip.Begin();
|
||||
strip.Show();
|
||||
|
||||
SetRandomSeed();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (animations.IsAnimating())
|
||||
{
|
||||
// the normal loop just needs these two to run the active animations
|
||||
animations.UpdateAnimations();
|
||||
strip.Show();
|
||||
}
|
||||
else
|
||||
{
|
||||
// no animations runnning, start some
|
||||
//
|
||||
PickRandom(0.2f); // 0.0 = black, 0.25 is normal, 0.5 is bright
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
// NeoPixelTest
|
||||
// This example will cycle between showing four pixels as Red, Green, Blue, White
|
||||
// and then showing those pixels as Black.
|
||||
//
|
||||
// Included but commented out are examples of configuring a NeoPixelBus for
|
||||
// different color order including an extra white channel, different data speeds, and
|
||||
// for Esp8266 different methods to send the data.
|
||||
// NOTE: You will need to make sure to pick the one for your platform
|
||||
//
|
||||
//
|
||||
// There is serial output of the current state so you can confirm and follow along
|
||||
//
|
||||
|
||||
#include <NeoPixelBus.h>
|
||||
|
||||
const uint16_t PixelCount = 4; // this example assumes 4 pixels, making it smaller will cause a failure
|
||||
const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for Esp8266
|
||||
|
||||
#define colorSaturation 128
|
||||
|
||||
// three element pixels, in different order and speeds
|
||||
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
//NeoPixelBus<NeoRgbFeature, Neo400KbpsMethod> strip(PixelCount, PixelPin);
|
||||
|
||||
// You can also use one of these for Esp8266,
|
||||
// each having their own restrictions
|
||||
//
|
||||
// These two are the same as above as the DMA method is the default
|
||||
// NOTE: These will ignore the PIN and use GPI03 pin
|
||||
//NeoPixelBus<NeoGrbFeature, NeoEsp8266Dma800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
//NeoPixelBus<NeoRgbFeature, NeoEsp8266Dma400KbpsMethod> strip(PixelCount, PixelPin);
|
||||
|
||||
// Uart method is good for the Esp-01 or other pin restricted modules
|
||||
// NOTE: These will ignore the PIN and use GPI02 pin
|
||||
//NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
//NeoPixelBus<NeoRgbFeature, NeoEsp8266Uart400KbpsMethod> strip(PixelCount, PixelPin);
|
||||
|
||||
// The bitbang method is really only good if you are not using WiFi features of the ESP
|
||||
// It works with all but pin 16
|
||||
//NeoPixelBus<NeoGrbFeature, NeoEsp8266BitBang800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
//NeoPixelBus<NeoRgbFeature, NeoEsp8266BitBang400KbpsMethod> strip(PixelCount, PixelPin);
|
||||
|
||||
// four element pixels, RGBW
|
||||
//NeoPixelBus<NeoRgbwFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
|
||||
|
||||
RgbColor red(colorSaturation, 0, 0);
|
||||
RgbColor green(0, colorSaturation, 0);
|
||||
RgbColor blue(0, 0, colorSaturation);
|
||||
RgbColor white(colorSaturation);
|
||||
RgbColor black(0);
|
||||
|
||||
HslColor hslRed(red);
|
||||
HslColor hslGreen(green);
|
||||
HslColor hslBlue(blue);
|
||||
HslColor hslWhite(white);
|
||||
HslColor hslBlack(black);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial); // wait for serial attach
|
||||
|
||||
Serial.println();
|
||||
Serial.println("Initializing...");
|
||||
Serial.flush();
|
||||
|
||||
// this resets all the neopixels to an off state
|
||||
strip.Begin();
|
||||
strip.Show();
|
||||
|
||||
|
||||
Serial.println();
|
||||
Serial.println("Running...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
delay(5000);
|
||||
|
||||
Serial.println("Colors R, G, B, W...");
|
||||
|
||||
// set the colors,
|
||||
// if they don't match in order, you need to use NeoGrbFeature feature
|
||||
strip.SetPixelColor(0, red);
|
||||
strip.SetPixelColor(1, green);
|
||||
strip.SetPixelColor(2, blue);
|
||||
strip.SetPixelColor(3, white);
|
||||
// the following line demonstrates rgbw color support
|
||||
// if the NeoPixels are rgbw types the following line will compile
|
||||
// if the NeoPixels are anything else, the following line will give an error
|
||||
//strip.SetPixelColor(3, RgbwColor(colorSaturation));
|
||||
strip.Show();
|
||||
|
||||
|
||||
delay(5000);
|
||||
|
||||
Serial.println("Off ...");
|
||||
|
||||
// turn off the pixels
|
||||
strip.SetPixelColor(0, black);
|
||||
strip.SetPixelColor(1, black);
|
||||
strip.SetPixelColor(2, black);
|
||||
strip.SetPixelColor(3, black);
|
||||
strip.Show();
|
||||
|
||||
delay(5000);
|
||||
|
||||
Serial.println("HSL Colors R, G, B, W...");
|
||||
|
||||
// set the colors,
|
||||
// if they don't match in order, you may need to use NeoGrbFeature feature
|
||||
strip.SetPixelColor(0, hslRed);
|
||||
strip.SetPixelColor(1, hslGreen);
|
||||
strip.SetPixelColor(2, hslBlue);
|
||||
strip.SetPixelColor(3, hslWhite);
|
||||
strip.Show();
|
||||
|
||||
|
||||
delay(5000);
|
||||
|
||||
Serial.println("Off again...");
|
||||
|
||||
// turn off the pixels
|
||||
strip.SetPixelColor(0, hslBlack);
|
||||
strip.SetPixelColor(1, hslBlack);
|
||||
strip.SetPixelColor(2, hslBlack);
|
||||
strip.SetPixelColor(3, hslBlack);
|
||||
strip.Show();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
#include <NeoPixelBus.h>
|
||||
|
||||
#define pixelCount 4
|
||||
#define colorSaturation 128
|
||||
|
||||
NeoPixelBus strip = NeoPixelBus(pixelCount, 8);
|
||||
|
||||
RgbColor red = RgbColor(colorSaturation, 0, 0);
|
||||
RgbColor green = RgbColor(0, colorSaturation, 0);
|
||||
RgbColor blue = RgbColor(0, 0, colorSaturation);
|
||||
RgbColor white = RgbColor(colorSaturation);
|
||||
RgbColor black = RgbColor(0);
|
||||
|
||||
void setup()
|
||||
{
|
||||
// this resets all the neopixels to an off state
|
||||
strip.Begin();
|
||||
strip.Show();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
delay(1000);
|
||||
|
||||
// set the colors,
|
||||
// if they don't match in order, you may need to use NEO_GRB flag
|
||||
strip.SetPixelColor(0, red);
|
||||
strip.SetPixelColor(1, green);
|
||||
strip.SetPixelColor(2, blue);
|
||||
strip.SetPixelColor(3, white);
|
||||
strip.Show();
|
||||
|
||||
delay(3000);
|
||||
|
||||
// turn off the pixels
|
||||
strip.SetPixelColor(0, black);
|
||||
strip.SetPixelColor(1, black);
|
||||
strip.SetPixelColor(2, black);
|
||||
strip.SetPixelColor(3, black);
|
||||
strip.Show();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user