-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.ino
149 lines (119 loc) · 2.99 KB
/
led.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <FastLED.h> // https://github.com/FastLED/FastLED
#include <Button.h> // https://github.com/madleech/Button
// Define pins for button and led
#define MODE_BUTTON_PIN 4
#define LED_PIN 5
// The number of LEDs in the serie
#define NUM_LEDS 2
// Holds the LED data
CRGB leds[NUM_LEDS];
// Simple button
Button modeButton(MODE_BUTTON_PIN);
// The current mode
uint8_t mode = 0;
// If the button is currently pressed or not
boolean pressed = false;
// Setup code, will be called once on powerup
void setup() {
// Initiate FastLED library, with APA106 as LED type
FastLED.addLeds<APA106, LED_PIN>(leds, NUM_LEDS);
modeButton.begin();
}
// This method will be called repeatedly
void loop() {
// Check if button has been pressed, but only run once when pressed
// This section cycles through the modes
if (modeButton.pressed() && !pressed) {
pressed = true;
mode++;
if (mode >= 4) {
mode = 0;
}
}
if (modeButton.released()) {
pressed = false;
}
// Depending on mode we will run different effects
if (mode == 0) {
rainbow();
} else if (mode == 1) {
blink();
} else if (mode == 2) {
randomBlink();
} else if (mode == 3) {
randomFade();
}
}
// Simple rainbow fade
void rainbow() {
// Cycle through all HSV hues
for (int hue = 0; hue < 255; hue++) {
// Both LEDs the same hue
leds[0] = CHSV(hue, 255, 255);
leds[1] = CHSV(hue, 255, 255);
FastLED.show();
// The amount of milliseconds to wait until next increment of hue, while also keep tracking of button press
// so that we don't have to wait for whole loop cycle before being detected
delayButton(30);
// Break the loop after we have released the button
if (pressed && modeButton.released()) {
pressed = false;
leds[0] = CHSV(255,255,0);
break;
}
}
}
// Blink red and blue, alternate on the 2 LEDs
void blink() {
leds[0] = CHSV(0, 255, 255);
leds[1] = CHSV(168, 255, 255);
FastLED.show();
delayButton(500);
leds[1] = CHSV(0, 255, 255);
leds[0] = CHSV(168, 255, 255);
FastLED.show();
delayButton(500);
}
// Blink random clolors
void randomBlink() {
leds[0] = CHSV(random(0, 255), 255, 255);
leds[1] = CHSV(random(0, 255), 255, 255);
FastLED.show();
delayButton(200);
}
int oldHue = 0;
// Fade to random hues
void randomFade() {
int newHue = oldHue;
int way = random(0, 1);
for (int hue = 0; hue < random(1, 50); hue++) {
if (way == 0) {
newHue--;
} else {
newHue++;
}
leds[0] = CHSV(newHue, 255, 255);
leds[1] = CHSV(newHue, 255, 255);
FastLED.show();
delayButton(10);
if (pressed && modeButton.released()) {
pressed = false;
leds[0] = CHSV(255,255,0);
break;
}
}
oldHue = newHue;
delayButton(100);
}
void delayButton(int delayTime) {
for (int i = 0; i < delayTime; i++) {
delay(1);
if (modeButton.pressed() && !pressed) {
pressed = true;
mode++;
if (mode >= 4) {
mode = 0;
}
}
}
}