-
Notifications
You must be signed in to change notification settings - Fork 2
/
muskrat.ino
167 lines (141 loc) · 3.65 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
#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;
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_LAST,
};
int function = FN_RAINBOW;
boolean fnChanged = false;
void setup()
{
pinMode(BUTTON_1, INPUT_PULLUP); // set pin to input
pinMode(BUTTON_2, INPUT_PULLUP); // set pin to input
// digitalWrite(BUTTON_1, HIGH); // turn on pullup resistors
// digitalWrite(BUTTON_2, HIGH); // turn on pullup resistors
ledStrip.begin();
ledStrip.setBrightness(*ledStrip_BrightnessLevel);
ledStrip.show(); // Initialize all pixels to 'off'
TaskAudio_setup();
Serial.begin(9600);
}
void loop()
{
switch(function) {
case FN_RAINBOW:
Serial.println("rainbow");
rainbowCycle(5);
break;
case FN_AUDIO:
Serial.println("audio");
TaskAudio_loop();
break;
default:
Serial.println("broken");
break;
}
}
void input()
{
button1.update();
button2.update();
//Serial.println(button1.read());
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);
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<ledStrip.numPixels(); i++) {
ledStrip.setPixelColor(i, c);
ledStrip.show();
delay(wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<ledStrip.numPixels(); i++) {
ledStrip.setPixelColor(i, Wheel((i+j) & 255));
}
ledStrip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
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(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);
}
}