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