forked from kwartzlab/sooncon2013
-
Notifications
You must be signed in to change notification settings - Fork 0
/
muskrat.ino
175 lines (148 loc) · 3.82 KB
/
muskrat.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <Bounce.h>
#include <Adafruit_NeoPixel.h>
// Pin definitions
#define LED_DATA 13
#define BUTTON_1 12
#define BUTTON_2 11
// LED strip
Adafruit_NeoPixel ledStrip = Adafruit_NeoPixel(8, LED_DATA, NEO_GRB + NEO_KHZ800);
uint8_t ledStrip_BrightnessLevels[] = { 1, 5, 10, 25, 75, 150, 255 };
uint8_t ledStrip_BrightnessMax = 5; // Change to 6 to enable maximum brightness
uint8_t *ledStrip_BrightnessLevel = &ledStrip_BrightnessLevels[1];
enum {
BRIGHTNESS_UP = 0,
BRIGHTNESS_DOWN
};
// Button debounce
Bounce button1 = Bounce(BUTTON_1, 20);
Bounce button2 = Bounce(BUTTON_2, 20);
enum {
FN_RAINBOW,
FN_AUDIO,
FN_LARSON,
FN_LAST,
};
int function = FN_RAINBOW; // Startup task
boolean fnChanged = false;
void setup()
{
pinMode(BUTTON_1, INPUT_PULLUP); // Configure BUTTON_1 as input with pullup
pinMode(BUTTON_2, INPUT_PULLUP); // Configure BUTTON_2 as input with pullup
ledStrip.begin();
ledStrip.setBrightness(*ledStrip_BrightnessLevel); // Set default brightness
ledStrip.show(); // Initialize all pixels to 'off'
TaskAudio_setup();
Serial.begin(9600);
}
void loop()
{
switch(function) {
case FN_RAINBOW:
TaskRainbow_loop();
break;
case FN_AUDIO:
TaskAudio_loop();
break;
case FN_LARSON:
TaskLarson_loop();
break;
default:
Serial.println("broken");
break;
}
}
void input()
{
button1.update();
button2.update();
if (!button1.read() && !button2.read()) {
if(!fnChanged) {
Serial.println("change fn");
nextFunction();
fnChanged = true;
}
} else {
fnChanged = false;
}
if (button1.fallingEdge()) {
ledStrip_BrightnessChange(BRIGHTNESS_DOWN);
} else if (button2.fallingEdge()) {
ledStrip_BrightnessChange(BRIGHTNESS_UP);
}
}
void nextFunction()
{
function += 1;
function %= FN_LAST;
}
void yield(uint32_t t)
{
input();
delay(t);
}
void ledStrip_BrightnessChange(uint8_t changeDirection)
{
static uint8_t brightnessIndex = 0;
if ((changeDirection == BRIGHTNESS_UP) &&
(brightnessIndex < ledStrip_BrightnessMax)) {
brightnessIndex++;
} else if ((changeDirection == BRIGHTNESS_DOWN) &&
(brightnessIndex > 0)) {
brightnessIndex--;
}
ledStrip_BrightnessLevel = &ledStrip_BrightnessLevels[brightnessIndex];
ledStrip.setBrightness(*ledStrip_BrightnessLevel);
}
// Slightly different, this makes the rainbow equally distributed throughout
void TaskRainbow_loop()
{
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i< ledStrip.numPixels(); i++) {
ledStrip.setPixelColor(i, Wheel((i * 256 / ledStrip.numPixels() + j) & 255));
}
ledStrip.show();
if(function != FN_RAINBOW) {
return;
}
yield(5);
}
}
void TaskLarson_loop()
{
uint8_t i;
uint8_t wait = 50;
for (i = 0; i < 8; i++) {
ledStrip.setPixelColor(i-3, 0x0);
ledStrip.setPixelColor(i-2, 0x110000);
ledStrip.setPixelColor(i-1, 0x330000);
ledStrip.setPixelColor(i, 0xFF0000);
ledStrip.setPixelColor(i+1, 0x330000);
ledStrip.setPixelColor(i+2, 0x110000);
ledStrip.show();
yield(wait);
}
for (i = 7; i > 0; i--) {
ledStrip.setPixelColor(i-2, 0x110000);
ledStrip.setPixelColor(i-1, 0x330000);
ledStrip.setPixelColor(i, 0xFF0000);
ledStrip.setPixelColor(i+1, 0x330000);
ledStrip.setPixelColor(i+2, 0x110000);
ledStrip.setPixelColor(i+3, 0x0);
ledStrip.show();
yield(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return ledStrip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return ledStrip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return ledStrip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}