-
Notifications
You must be signed in to change notification settings - Fork 0
/
nollebricka.ino
216 lines (166 loc) · 5.05 KB
/
nollebricka.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Based on code from https://github.com/GeorgiZhelezov/arduino-msgeq7
#include <FastLED.h>
#include <Button.h>
#define NUM_LEDS 67
#define LED_PIN 2
CRGB leds[NUM_LEDS];
Button modeButton(6);
unsigned short MSGEQ[7]; //arrays for storing MSGEQ7 data
unsigned short strobePin = 4;
unsigned short resetPin = 9;
unsigned short noiseFilterPin = A2;
unsigned short MSGEQPin = 1;
unsigned short readDataDelay = 3; //delay after reading MSGEQ7 data
uint8_t hue = 0;
uint8_t mode = 0;
boolean pressed = false;
int ledSections[7][2][2] = {
{
{1, 4},
{64, 66}
},
{
{5, 8},
{60, 63}
},
{
{9, 12},
{56, 59}
},
{
{13, 16},
{52, 55}
},
{
{17, 20},
{48, 51}
},
{
{21, 24},
{44, 47}
},
{
{25, 34},
{35, 43}
}
};
void setup() {
//Serial.begin(9600);
modeButton.begin();
// put your setup code here, to run once:
FastLED.addLeds<WS2812, LED_PIN>(leds, NUM_LEDS);
pinMode(strobePin, OUTPUT);
pinMode(resetPin, OUTPUT);
pinMode(MSGEQPin, INPUT);
pinMode(noiseFilterPin, INPUT);
digitalWrite(resetPin, HIGH);
digitalWrite(resetPin, LOW);
}
void loop() {
if (modeButton.pressed() && !pressed) {
pressed = true;
mode++;
if (mode >= 4) {
mode = 0;
}
}
if (modeButton.released()) {
pressed = false;
}
if (mode == 0) {
readData();
rainbowEq();
} else if (mode == 1) {
readData();
rainbowEqMinBrightness();
} else if (mode == 2) {
readData();
whiteEq();
} else if (mode == 3) {
rainbowFade();
}
}
void readData() {
unsigned short noiseFilter = analogRead(noiseFilterPin);
delay(readDataDelay);
noiseFilter = constrain(noiseFilter, 20, 1023);
for (short i = 0; i < 7; i++) {
digitalWrite(strobePin, LOW);
MSGEQ[i] = analogRead(MSGEQPin);
digitalWrite(strobePin, HIGH);
MSGEQ[i] = constrain(MSGEQ[i], noiseFilter, 1023);
MSGEQ[i] = map(MSGEQ[i], noiseFilter-1, 1023, 0, 255);
MSGEQ[i] = constrain(MSGEQ[i]*2, 0, 255);
delay(readDataDelay);
}
}
void rainbowEq() {
for (int sectionIndex = 0; sectionIndex < 7; sectionIndex++) {
unsigned short brightness = MSGEQ[sectionIndex];
for (int side = 0; side < 2; side++) {
for (int dot = ledSections[sectionIndex][side][0]; dot <= ledSections[sectionIndex][side][1]; dot++) {
int thisSection = 32 * sectionIndex;
int nextSection = 32 * (sectionIndex + 1);
// To make each section fade to next section
int extra = map(dot, ledSections[sectionIndex][side][0], ledSections[sectionIndex][side][1], 0, nextSection - thisSection - 1);
// Invert on other side
if (side == 1) {
extra = (extra * -1) + 32;
}
leds[dot] = CHSV(thisSection + extra, 255, brightness);
}
}
}
FastLED.show();
}
void rainbowEqMinBrightness() {
for (int sectionIndex = 0; sectionIndex < 7; sectionIndex++) {
unsigned short brightness = MSGEQ[sectionIndex];
brightness = constrain(brightness + 30, 30, 255);
for (int side = 0; side < 2; side++) {
for (int dot = ledSections[sectionIndex][side][0]; dot <= ledSections[sectionIndex][side][1]; dot++) {
int thisSection = 32 * sectionIndex;
int nextSection = 32 * (sectionIndex + 1);
// To make each section fade to next section
int extra = map(dot, ledSections[sectionIndex][side][0], ledSections[sectionIndex][side][1], 0, nextSection - thisSection - 1);
// Invert on other side
if (side == 1) {
extra = (extra * -1) + 32;
}
leds[dot] = CHSV(thisSection + extra, 255, brightness);
}
}
}
FastLED.show();
}
void whiteEq() {
for (int sectionIndex = 0; sectionIndex < 7; sectionIndex++) {
unsigned short brightness = MSGEQ[sectionIndex];
brightness = constrain(brightness, 10, 150);
for (int side = 0; side < 2; side++) {
for (int dot = ledSections[sectionIndex][side][0]; dot <= ledSections[sectionIndex][side][1]; dot++) {
leds[dot] = CHSV(0, 0, brightness);
}
}
}
FastLED.show();
}
void rainbowFade() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue++ + i, 255, 100);
FastLED.show();
delay(30);
if (modeButton.pressed() && !pressed) {
pressed = true;
mode++;
if (mode >= 4) {
mode = 0;
}
}
if (modeButton.released()) {
pressed = false;
leds[0] = CHSV(255,255,0);
break;
}
}
}