forked from marmilicious/FastLED_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoReel100_with_button.ino
136 lines (109 loc) · 4.33 KB
/
DemoReel100_with_button.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//***************************************************************
// This is Mark Kriegsman's FastLED DemoReel100 example with
// a modificaiton to use a button for changing patterns. The
// timer used for picking a new pattern has been commented out
// and there is a button check inside the main loop now.
//
// Search for "BUTTON STUFF" to find the various button releated
// code additions.
//
// Marc Miller, March 2017
//***************************************************************
#include "FastLED.h"
#define DATA_PIN 11
#define CLK_PIN 13
#define LED_TYPE LPD8806
#define COLOR_ORDER GRB
#define NUM_LEDS 32
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 128
#define FRAMES_PER_SECOND 120
//---------------------------------------------------------------
//BUTTON STUFF
// This uses JChristensen's Button Library from:
// https://github.com/JChristensen/Button
#include "Button.h" // Include Button library
const int buttonPin = 4; // Set digital pin used with debounced pushbutton
Button myButton(buttonPin, true, true, 50); // Declare the button
//---------------------------------------------------------------
void setup() {
Serial.begin(115200); // Allows serial monitor output (check baud rate)
delay(3000); // 3 second delay for recovery
//FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
}
// List of patterns to cycle through. Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = { rainbow, confetti, sinelon, juggle };
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
//---------------------------------------------------------------
void loop()
{
// Call the current pattern function once, updating the 'leds' array
gPatterns[gCurrentPatternNumber]();
// send the 'leds' array out to the actual LED strip
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(1000/FRAMES_PER_SECOND);
// do some periodic updates
EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
// BUTTON STUFF
// Not using this timer to change patterns any more. Instead check the button.
// EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
//
readbutton(); // check for button press
}//end_main_loop
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
//---------------------------------------------------------------
void nextPattern()
{
// add one to the current pattern number, and wrap around at the end
gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}
void rainbow()
{
// FastLED's built-in rainbow generator
fill_rainbow( leds, NUM_LEDS, gHue, 12);
}
void confetti()
{
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = random16(NUM_LEDS);
leds[pos] += CHSV( gHue + random8(64), random8(128,200), random8(48,255));
}
void sinelon()
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 12);
int pos = beatsin16( 13, 0, NUM_LEDS-1 );
leds[pos] += CHSV( gHue, 255, 192);
}
void juggle() {
// four colored dots, weaving in and out of sync with each other
fadeToBlackBy( leds, NUM_LEDS, 20);
byte dothue = 0;
for( int i = 0; i < 4; i++) {
leds[beatsin16( i+5, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
dothue += 32;
}
}
//BUTTON STUFF
//---------Function to read the button and do something----------
void readbutton() {
myButton.read();
if(myButton.wasPressed()) {
Serial.println("Button pressed! Next pattern... ");
nextPattern(); // Change to the next pattern
//Flash pixel zero white as a visual that button was pressed.
leds[0] = CHSV(0,0,255); //Set first pixel color white
FastLED.show(); //Update display
delay(100); //Short pause so we can see leds[0] flash
leds[0] = CRGB::Black; //Set first pixel off
FastLED.show(); //Update display
delay(100); //Short pause so we can see leds[0] flash
}
}//end_readbutton
//---------------------------------------------------------------