forked from amiravni/LightFun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightFun.ino
360 lines (313 loc) · 12.8 KB
/
LightFun.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#define FILTERTAPS 30
#define MAXVAL 1023
#define MAXVALREL 256
#define MINVAL 0
#define AVGNUM 30
#define WAKEUPSAMP 2
#define BUFFER 40
#define FADEGAP 1
#define TIMECHANGEMIL 5000
#define FREQ 8000
#define SENDSERIAL 0
template<int filterTaps>
class FIR {
public:
//construct without coefs
FIR() {
k = 0; //initialize so that we start to read at index 0
for (int i=0; i<filterTaps; i++) {
values[i] = 0; // to have a nice start up, fill the array with 0's
}
//TODO calculate default gain?
//TODO calculate default coefs?
}
//construct with coefs
FIR(float newGain, float *newCoefs) {
k = 0; //initialize so that we start to read at index 0
setGain(newGain);
for (int i=0; i<filterTaps; i++) {
values[i] = 0; // to have a nice start up, fill the array with 0's
}
setCoefficients(newCoefs);
}
void setGain(float newGain) {gain = newGain;}
void setCoefficients(float *newCoefs) {
for (int i=0; i<filterTaps; i++) {
coef[i] = newCoefs[i];
}
}
//set coefficient at specified index
void setCoefficient(int idx, float newCoef) { coef[idx] = newCoef; }
float process(float in) {
float out = 0; // out is the return variable. It is set to 0 every time we call the filter!
values[k] = in; // store the input of the routine (contents of the 'in' variable) in the array at the current pointer position
for (int i=0; i<filterTaps; i++) { // we step through the array
out += coef[i] * values[(i + k) % filterTaps]; // ... and add and multiply each value to accumulate the output
// (i + k) % filterTaps creates a cyclic way of getting through the array
}
out /= gain; // We need to scale the output (unless the coefficients provide unity gain in the passband)
k = (k+1) % filterTaps; // k is increased and wraps around the filterTaps, so next time we will overwrite the oldest saved sample in the array
return out; // we send the output value back to whoever called the routine
}
private:
float values[filterTaps];
float coef[filterTaps];
//declare gain coefficient to scale the output back to normal
float gain; // set to 1 and input unity to see what this needs to be
int k; // k stores the index of the current array read to create a circular memory through the array
};
class RGBControl {
public:
RGBControl() {
ledPin=0;
micVal=0;
DCVal=-1;
brightness=0;
LastBrightness=0;
micMax=0;
micMin=255;//1023;
counter=0;
SumDCVal=0;
UpFade = 0;
DownFade = 0;
}
RGBControl operator=( RGBControl C2)
{
ledPin = C2.ledPin;
brightness = C2.brightness;
LastBrightness = C2.LastBrightness;
micVal = C2.micVal;
DCVal = C2.DCVal;
micMax = C2.micMax;
micMin = C2.micMin;
counter = C2.counter;
SumDCVal = C2.SumDCVal;
UpFade = C2.UpFade;
DownFade = C2.DownFade;
}
void SetZero() { brightness=0; }
void SetLedPin(int Num) { ledPin=Num; }
void SetDCVal(int Num) { DCVal=Num; }
void SetFade(int Up, int Down) { UpFade=Up; DownFade=Down;}
void SetMinMax(int Min, int Max) { micMin=Min; micMax=Max;}
void micVal2Brightness() {
micVal = abs(micVal-DCVal);
//micVal = min(micVal+DCALLVal,MAXVAL);
micMin=min(micVal,micMin);
micMax=max(micVal,micMax);
brightness = int((double(micVal-micMin)/double(micMax-micMin))*MAXVALREL)-1;
brightness = min(max(brightness-20,0),MAXVALREL-1);
}
void WriteBright()
{
int counter=0;
while ( abs(LastBrightness - brightness)> FADEGAP/2)
{
counter++;
if (LastBrightness>brightness) {
LastBrightness = LastBrightness - FADEGAP;
if (counter>=DownFade) break;
}
else
{
LastBrightness = LastBrightness + FADEGAP;
if (counter>=UpFade) break;
}
analogWrite(ledPin, LastBrightness);
}
}
int CalcDC() {
if (DCVal==-1)
{
if (counter>=WAKEUPSAMP) SumDCVal=SumDCVal+micVal;
counter++;
if (counter==AVGNUM+WAKEUPSAMP )
{
DCVal=SumDCVal/AVGNUM;
if (SENDSERIAL) {
Serial.println(DCVal);
}
return 1;
}
return 0;
}
else
{
return 1;
}
}
int ledPin;
int brightness;
int LastBrightness;
int micVal;
int micMax;
int micMin;
private:
int DCVal;
int counter;
int SumDCVal;
int UpFade;
int DownFade;
};
long TimeTMP1=0;
long TimeTMP2=0;
long timeTMPDT=0;
long timeChange=0;
long Time1=0;
long Time2=0;
long timeDT=0;
int mic_NoFilt[BUFFER]={0};
int micPin=0;
volatile int AutoPin=12;
volatile bool AutoMode=0;
RGBControl RCtrl,GCtrl,BCtrl;
FIR<FILTERTAPS> firR,firG,firB;
void setup() {
RCtrl.SetFade(50,20);
BCtrl.SetFade(50,7);
GCtrl.SetFade(50,2);
// RCtrl.SetMinMax(0,100);
BCtrl.SetMinMax(0,20);
GCtrl.SetMinMax(0,10);
RCtrl.SetLedPin(5);
GCtrl.SetLedPin(9);
BCtrl.SetLedPin(3);
pinMode(RCtrl.ledPin, OUTPUT);
pinMode(GCtrl.ledPin, OUTPUT);
pinMode(BCtrl.ledPin, OUTPUT);
if (SENDSERIAL) {
Serial.begin(115200);
}
float Rcoef[FILTERTAPS] = {0.021987,0.024035,0.026037,0.027974,0.029828,0.03158,0.033212,0.034709,0.036054,0.037234,0.038238,0.039053,0.039672,0.040089,0.040298,0.040298,0.040089,0.039672,0.039053,0.038238,0.037234,0.036054,0.034709,0.033212,0.03158,0.029828,0.027974,0.026037,0.024035,0.021987};
//{0.030352,0.03572,0.04093,0.045848,0.050345,0.054301,0.057606,0.06017,0.061921,0.062808,0.062808,0.061921,0.06017,0.057606,0.054301,0.050345,0.045848,0.04093,0.03572,0.030352};
//{0.0070018,0.0094086,0.016214,0.026914,0.040488,0.055516,0.070343,0.083292,0.092868,0.097955,0.097955,0.092868,0.083292,0.070343,0.055516,0.040488,0.026914,0.016214,0.0094086,0.0070018};
float Bcoef[FILTERTAPS] ={-0.011762,-0.038701,0.0050889,0.056559,0.015417,-0.034795,-0.0098747,-0.011244,-0.051569,0.030127,0.14934,0.018955,-0.22001,-0.12084,0.20886,0.20886,-0.12084,-0.22001,0.018955,0.14934,0.030127,-0.051569,-0.011244,-0.0098747,-0.034795,0.015417,0.056559,0.0050889,-0.038701,-0.011762};
//{-0.0059583,0.010091,-0.011563,0.0090001,0.080684,0.06634,-0.085756,-0.18041,-0.040071,0.18015,0.18015,-0.040071,-0.18041,-0.085756,0.06634,0.080684,0.0090001,-0.011563,0.010091,-0.0059583};
//{0.0039218,0.0041651,-0.0017636,-0.024773,-0.064743,-0.096112,-0.078892,0.0056742,0.12672,0.2157,0.2157,0.12672,0.0056742,-0.078892,-0.096112,-0.064743,-0.024773,-0.0017636,0.0041651,0.0039218};
float Gcoef[FILTERTAPS] ={0.014581,0.015791,-0.017186,-0.018813,0.020738,0.023056,-0.025904,-0.029495,0.034171,0.040525,-0.049674,-0.064014,0.089775,0.1498,-0.44965,0.44965,-0.1498,-0.089775,0.064014,0.049674,-0.040525,-0.034171,0.029495,0.025904,-0.023056,-0.020738,0.018813,0.017186,-0.015791,-0.014581};
//{-0.0063113,0.030439,0.034878,-0.0095334,-0.057106,-0.039805,0.051455,0.12769,0.042445,-0.54343,0.54343,-0.042445,-0.12769,-0.051455,0.039805,0.057106,0.0095334,-0.034878,-0.030439,0.0063113};
//{0.0012888,-0.0014432,-0.007129,-0.0119,-0.0030922,0.027927,0.065647,0.063596,-0.04974,-0.57121,0.57121,0.04974,-0.063596,-0.065647,-0.027927,0.0030922,0.0119,0.007129,0.0014432,-0.0012888};
firR.setCoefficients(Rcoef);
firR.setGain(1);
firB.setCoefficients(Bcoef);
firB.setGain( 1.9663);
firG.setCoefficients(Gcoef);
firG.setGain(2.0864);
// BCtrl.SetDCVal(0);
//set up continuous sampling of analog pin 0 (you don't need to understand this part, just know how to use it in the loop())
//clear ADCSRA and ADCSRB registers
ADCSRA = 0;
ADCSRB = 0;
ADMUX |= (1 << REFS0); //set reference voltage
ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only
ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
ADCSRA |= (1 << ADATE); //enabble auto trigger
ADCSRA |= (1 << ADEN); //enable ADC
ADCSRA |= (1 << ADSC); //start ADC measurements
//if you want to add other things to setup(), do it here
pinMode(2, INPUT);
pinMode(AutoPin, OUTPUT);
//attachInterrupt(1, ChangeMode, RISING);
attachInterrupt(0, ChangeMode, RISING);
}
void loop() {
// timeChange = millis();
// if ( (timeChange % TIMECHANGEMIL) > (TIMECHANGEMIL - 50) )
// {
// firTMP = firR;
// TMPCtrl = RCtrl;
// firR = firB;
// RCtrl = BCtrl;
// firB = firG;
// BCtrl = GCtrl;
// firG = firTMP;
// GCtrl = TMPCtrl;
// }
if (AutoMode)
//if (digitalRead(2)==HIGH)
{
RCtrl.SetZero();
GCtrl.SetZero();
BCtrl.SetZero();
RCtrl.WriteBright();
BCtrl.WriteBright();
GCtrl.WriteBright();
if (SENDSERIAL) {
Serial.println("Auto!");
}
delay(100);
}
else
{
TimeTMP2=micros();
timeTMPDT=TimeTMP2-TimeTMP1;
TimeTMP1=micros();
int Buff=0;
while (Buff<BUFFER/2) {
Time2= micros();
if ( Time2 - Time1 >= 56) {
Time2= micros();
mic_NoFilt[Buff] = ADCH;//analogRead(micPin);
timeDT = Time2 - Time1;
Time1 = micros();
Buff++;
}
}
for ( Buff=0;Buff<BUFFER/2;Buff++){
RCtrl.micVal = int(firR.process(float(mic_NoFilt[Buff])));
GCtrl.micVal = int(firG.process(float(mic_NoFilt[Buff])));
BCtrl.micVal = int(firB.process(float(mic_NoFilt[Buff])));
}
if ( RCtrl.CalcDC() && GCtrl.CalcDC() && BCtrl.CalcDC()) {
if (SENDSERIAL) {
Serial.print(" TIME: ");
Serial.print(timeDT);
Serial.print(",");
Serial.print(timeTMPDT);
Serial.print(" NoFiltVal: ");
Serial.print(mic_NoFilt[BUFFER/2-1]);
Serial.print(" Val: R = ");
Serial.print(RCtrl.micVal);
Serial.print(" G = ");
Serial.print(GCtrl.micVal);
Serial.print(" B = ");
Serial.print(BCtrl.micVal);
}
RCtrl.micVal2Brightness();
GCtrl.micVal2Brightness();
BCtrl.micVal2Brightness();
if (SENDSERIAL) {
Serial.print(" MinMax: R = ");
Serial.print(RCtrl.micMin); Serial.print(","); Serial.print(RCtrl.micMax);
Serial.print(" G = ");
Serial.print(GCtrl.micMin); Serial.print(","); Serial.print(GCtrl.micMax);
Serial.print(" B = ");
Serial.print(BCtrl.micMin); Serial.print(","); Serial.print(BCtrl.micMax);
Serial.print(" Bright: R = ");
Serial.print(RCtrl.brightness);
Serial.print(" G = ");
Serial.print(GCtrl.brightness);
Serial.print(" B = ");
Serial.print(BCtrl.brightness);
}
RCtrl.WriteBright();
BCtrl.WriteBright();
GCtrl.WriteBright();
if (SENDSERIAL) {
Serial.print(" LastBright: R = ");
Serial.print(RCtrl.LastBrightness);
Serial.print(" G = ");
Serial.print(GCtrl.LastBrightness);
Serial.print(" B = ");
Serial.println(BCtrl.LastBrightness);
}
}
}
}
void ChangeMode()
{
// Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
AutoMode=!AutoMode;
if (AutoMode) digitalWrite(AutoPin,HIGH);
else digitalWrite(AutoPin,LOW);
}