Created
July 19, 2017 14:48
-
-
Save cjdell/65bbd2b085f13d12bc5eccba053ba16a to your computer and use it in GitHub Desktop.
Arduino LED Strip Samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <NeoPixelBus.h> | |
#include <NeoPixelAnimator.h> | |
const uint16_t PixelCount = 8; // 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 RgbColor CylonEyeColor(HtmlColor(0x7f0000)); | |
NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart800KbpsMethod> strip(PixelCount, PixelPin); | |
NeoPixelAnimator animations(2); // only ever need 2 animations | |
uint16_t lastPixel = 0; // track the eye position | |
int8_t moveDir = 1; // track the direction of movement | |
// uncomment one of the lines below to see the effects of | |
// changing the ease function on the movement animation | |
AnimEaseFunction moveEase = | |
// NeoEase::Linear; | |
// NeoEase::QuadraticInOut; | |
// NeoEase::CubicInOut; | |
NeoEase::QuarticInOut; | |
// NeoEase::QuinticInOut; | |
// NeoEase::SinusoidalInOut; | |
// NeoEase::ExponentialInOut; | |
// NeoEase::CircularInOut; | |
void FadeAll(uint8_t darkenBy) | |
{ | |
RgbColor color; | |
for (uint16_t indexPixel = 0; indexPixel < strip.PixelCount(); indexPixel++) | |
{ | |
color = strip.GetPixelColor(indexPixel); | |
color.Darken(darkenBy); | |
strip.SetPixelColor(indexPixel, color); | |
} | |
} | |
void FadeAnimUpdate(const AnimationParam& param) | |
{ | |
if (param.state == AnimationState_Completed) | |
{ | |
FadeAll(10); | |
animations.RestartAnimation(param.index); | |
} | |
} | |
void MoveAnimUpdate(const AnimationParam& param) | |
{ | |
// apply the movement animation curve | |
float progress = moveEase(param.progress); | |
// use the curved progress to calculate the pixel to effect | |
uint16_t nextPixel; | |
if (moveDir > 0) | |
{ | |
nextPixel = progress * PixelCount; | |
} | |
else | |
{ | |
nextPixel = (1.0f - progress) * PixelCount; | |
} | |
// if progress moves fast enough, we may move more than | |
// one pixel, so we update all between the calculated and | |
// the last | |
if (lastPixel != nextPixel) | |
{ | |
for (uint16_t i = lastPixel + moveDir; i != nextPixel; i += moveDir) | |
{ | |
strip.SetPixelColor(i, CylonEyeColor); | |
} | |
} | |
strip.SetPixelColor(nextPixel, CylonEyeColor); | |
lastPixel = nextPixel; | |
if (param.state == AnimationState_Completed) | |
{ | |
// reverse direction of movement | |
moveDir *= -1; | |
// done, time to restart this position tracking animation/timer | |
animations.RestartAnimation(param.index); | |
} | |
} | |
void SetupAnimations() | |
{ | |
// fade all pixels providing a tail that is longer the faster | |
// the pixel moves. | |
animations.StartAnimation(0, 5, FadeAnimUpdate); | |
// take several seconds to move eye fron one side to the other | |
animations.StartAnimation(1, 2000, MoveAnimUpdate); | |
} | |
void setup() | |
{ | |
strip.Begin(); | |
strip.Show(); | |
SetupAnimations(); | |
} | |
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <NeoPixelBus.h> | |
const uint16_t PixelCount = 8; // 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 | |
// Uart method is good for the Esp-01 or other pin restricted modules | |
// NOTE: These will ignore the PIN and use GPI02 pin | |
NeoPixelBus<NeoRgbFeature, NeoEsp8266Uart800KbpsMethod> 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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment