Files
NeoPixelBus/examples/NeoPixelFun/NeoPixelFun.pde
T
Makuna 71dd6e53cf provide better samples and more
two samples now are included
improved the readme (changed from txt to md)
NEO_RGB doesn;t require 400khz support, and 400khz support by default
isn't compiled in
2014-12-15 18:51:52 -08:00

140 lines
3.5 KiB
Plaintext

#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);
}