FastLED 3.7.8
Loading...
Searching...
No Matches
DemoReel100.ino
Go to the documentation of this file.
1
4
5#include <FastLED.h>
6
7FASTLED_USING_NAMESPACE
8
9// FastLED "100-lines-of-code" demo reel, showing just a few
10// of the kinds of animation patterns you can quickly and easily
11// compose using FastLED.
12//
13// This example also shows one easy way to define multiple
14// animations patterns and have them automatically rotate.
15//
16// -Mark Kriegsman, December 2014
17
18
19#define DATA_PIN 3
20//#define CLK_PIN 4
21#define LED_TYPE WS2811
22#define COLOR_ORDER GRB
23#define NUM_LEDS 64
24CRGB leds[NUM_LEDS];
25
26#define BRIGHTNESS 96
27#define FRAMES_PER_SECOND 120
28
29void setup() {
30 delay(3000); // 3 second delay for recovery
31
32 // tell FastLED about the LED strip configuration
33 FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
34 //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
35
36 // set master brightness control
37 FastLED.setBrightness(BRIGHTNESS);
38}
39
40
41// List of patterns to cycle through. Each is defined as a separate function below.
42typedef void (*SimplePatternList[])();
43SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };
44
45uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
46uint8_t gHue = 0; // rotating "base color" used by many of the patterns
47
48void loop()
49{
50 // Call the current pattern function once, updating the 'leds' array
51 gPatterns[gCurrentPatternNumber]();
52
53 // send the 'leds' array out to the actual LED strip
54 FastLED.show();
55 // insert a delay to keep the framerate modest
56 FastLED.delay(1000/FRAMES_PER_SECOND);
57
58 // do some periodic updates
59 EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
60 EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
61}
62
63#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
64
65void nextPattern()
66{
67 // add one to the current pattern number, and wrap around at the end
68 gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
69}
70
71void rainbow()
72{
73 // FastLED's built-in rainbow generator
74 fill_rainbow( leds, NUM_LEDS, gHue, 7);
75}
76
77void rainbowWithGlitter()
78{
79 // built-in FastLED rainbow, plus some random sparkly glitter
80 rainbow();
81 addGlitter(80);
82}
83
84void addGlitter( fract8 chanceOfGlitter)
85{
86 if( random8() < chanceOfGlitter) {
87 leds[ random16(NUM_LEDS) ] += CRGB::White;
88 }
89}
90
91void confetti()
92{
93 // random colored speckles that blink in and fade smoothly
94 fadeToBlackBy( leds, NUM_LEDS, 10);
95 int pos = random16(NUM_LEDS);
96 leds[pos] += CHSV( gHue + random8(64), 200, 255);
97}
98
99void sinelon()
100{
101 // a colored dot sweeping back and forth, with fading trails
102 fadeToBlackBy( leds, NUM_LEDS, 20);
103 int pos = beatsin16( 13, 0, NUM_LEDS-1 );
104 leds[pos] += CHSV( gHue, 255, 192);
105}
106
107void bpm()
108{
109 // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
110 uint8_t BeatsPerMinute = 62;
112 uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
113 for( int i = 0; i < NUM_LEDS; i++) { //9948
114 leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
115 }
116}
117
118void juggle() {
119 // eight colored dots, weaving in and out of sync with each other
120 fadeToBlackBy( leds, NUM_LEDS, 20);
121 uint8_t dothue = 0;
122 for( int i = 0; i < 8; i++) {
123 leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
124 dothue += 32;
125 }
126}
127
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:21
central include file for FastLED, defines the CFastLED class/object
void delay(unsigned long ms)
Delay for the given number of milliseconds.
Definition FastLED.cpp:154
void setBrightness(uint8_t scale)
Set the global brightness scaling.
Definition FastLED.h:715
void show(uint8_t scale)
Update all our controllers with the current led colors, using the passed in brightness.
Definition FastLED.cpp:59
static CLEDController & addLeds(CLEDController *pLed, struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0)
Add a CLEDController instance to the world.
Definition FastLED.cpp:47
RGB color palette with 16 discrete values.
Definition colorutils.h:933
LIB8STATIC uint16_t beatsin16(accum88 beats_per_minute, uint16_t lowest=0, uint16_t highest=65535, uint32_t timebase=0, uint16_t phase_offset=0)
Generates a 16-bit sine wave at a given BPM that oscillates within a given range.
Definition lib8tion.h:1014
LIB8STATIC uint8_t beatsin8(accum88 beats_per_minute, uint8_t lowest=0, uint8_t highest=255, uint32_t timebase=0, uint8_t phase_offset=0)
Generates an 8-bit sine wave at a given BPM that oscillates within a given range.
Definition lib8tion.h:1032
@ TypicalLEDStrip
Typical values for SMD5050 LEDs.
Definition color.h:20
void fadeToBlackBy(CRGB *leds, uint16_t num_leds, uint8_t fadeBy)
Reduce the brightness of an array of pixels all at once.
void fill_rainbow(struct CRGB *targetArray, int numToFill, uint8_t initialhue, uint8_t deltahue)
Fill a range of LEDs with a rainbow of colors.
uint8_t fract8
ANSI: unsigned short _Fract.
Definition types.h:30
CRGB ColorFromPalette(const CRGBPalette16 &pal, uint8_t index, uint8_t brightness, TBlendType blendType)
Get a color from a palette.
@ White
Definition crgb.h:604
const TProgmemRGBPalette16 PartyColors_p
HSV color ramp: blue, purple, pink, red, orange, yellow (and back).
LIB8STATIC uint16_t random16()
Generate a 16-bit random number.
Definition random8.h:50
LIB8STATIC uint8_t random8()
Generate an 8-bit random number.
Definition random8.h:40
#define EVERY_N_MILLISECONDS(N)
Alias for EVERY_N_MILLIS.
Definition lib8tion.h:1351
#define EVERY_N_SECONDS(N)
Checks whether to execute a block of code every N seconds.
Definition lib8tion.h:1316
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition chsv.h:11
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:25