|
@@ -0,0 +1,110 @@
|
|
1
|
+#include <SoftPWM.h>
|
|
2
|
+#include <SoftPWM_timer.h>
|
|
3
|
+
|
|
4
|
+// Dimmer potentiometer pin
|
|
5
|
+#define DIMMER A5
|
|
6
|
+
|
|
7
|
+// LED strip V- pins
|
|
8
|
+// The idea was that LED pins would be PWM, but board only has six, including pin 5
|
|
9
|
+// (used as the spectrum shield's RESET pin [arggghhh]), so we use the SoftPWM library
|
|
10
|
+// which allows us to replace pin 5 with 13
|
|
11
|
+int leds[6] = { 9, 10, 11, 3, 13 /* don't you hate desoldering? */, 7};
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+// Spectrum Shield pin connections
|
|
15
|
+#define STROBE 4
|
|
16
|
+#define RESET 5 // See [arggghhh] above
|
|
17
|
+#define AUDIO_LEFT A0
|
|
18
|
+#define AUDIO_RIGHT A1
|
|
19
|
+
|
|
20
|
+//Define spectrum variables
|
|
21
|
+int Frequencies_Left[7] = {0};
|
|
22
|
+int Frequencies_Right[7] = {0};
|
|
23
|
+
|
|
24
|
+#define NOISE_LEVEL 32 // even "silence" has frequencies ;)
|
|
25
|
+
|
|
26
|
+int loud = 0; // dynamically determined from sound
|
|
27
|
+int dimmer; // DIMMER pot value scaled to 8 bit
|
|
28
|
+int quiet; // level below which LED is off (computed from dimmer and loud)
|
|
29
|
+#define LOUDNESS_DECAY 0.01
|
|
30
|
+#define NORMALIZE(level) level>NOISE_LEVEL ? constrain(map(level, quiet, max(loud, (quiet * 5 / 4)), 0, dimmer), 0, 255) : 0
|
|
31
|
+
|
|
32
|
+/********************Setup*************************/
|
|
33
|
+void setup() {
|
|
34
|
+ Serial.begin(9600);
|
|
35
|
+ // init led pins, and flash them (to help check wiring)
|
|
36
|
+ SoftPWMBegin();
|
|
37
|
+ for (int i = 0; i < 6; i++) {
|
|
38
|
+ pinMode(leds[i], OUTPUT);
|
|
39
|
+ SoftPWMSetFadeTime(leds[i], 23, 42);
|
|
40
|
+ SoftPWMSet(leds[i], 63);
|
|
41
|
+ delay(250);
|
|
42
|
+ SoftPWMSet(leds[i], 0);
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ //Set spectrum Shield pin configurations
|
|
46
|
+ pinMode(STROBE, OUTPUT);
|
|
47
|
+ pinMode(RESET, OUTPUT);
|
|
48
|
+ pinMode(AUDIO_LEFT, INPUT);
|
|
49
|
+ pinMode(AUDIO_RIGHT, INPUT);
|
|
50
|
+ digitalWrite(STROBE, HIGH);
|
|
51
|
+ digitalWrite(RESET, HIGH);
|
|
52
|
+
|
|
53
|
+ //Initialize Spectrum Analyzers
|
|
54
|
+ digitalWrite(STROBE, LOW);
|
|
55
|
+ delay(1);
|
|
56
|
+ digitalWrite(RESET, HIGH);
|
|
57
|
+ delay(1);
|
|
58
|
+ digitalWrite(STROBE, HIGH);
|
|
59
|
+ delay(1);
|
|
60
|
+ digitalWrite(STROBE, LOW);
|
|
61
|
+ delay(1);
|
|
62
|
+ digitalWrite(RESET, LOW);
|
|
63
|
+}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+/**************************Main Loop*****************************/
|
|
67
|
+void loop() {
|
|
68
|
+ Read_Frequencies();
|
|
69
|
+ dimmer = 255 - (analogRead(DIMMER) >> 2);
|
|
70
|
+ quiet = map(dimmer, 255, 0, NOISE_LEVEL, max((loud * 9) / 10, (NOISE_LEVEL * 10) / 9));
|
|
71
|
+ Graph_Frequencies();
|
|
72
|
+ Serial.print(loud);
|
|
73
|
+ Serial.print(' ');
|
|
74
|
+ Serial.println(quiet);
|
|
75
|
+}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+/*******************Pull frquencies from Spectrum Shield********************/
|
|
79
|
+void Read_Frequencies() {
|
|
80
|
+ int level = 0;
|
|
81
|
+ //Read frequencies for each band
|
|
82
|
+ for (int freq_amp = 0; freq_amp < 7; freq_amp++)
|
|
83
|
+ {
|
|
84
|
+ // a crude low-pass to smooth out noises
|
|
85
|
+ Frequencies_Left[freq_amp] = (Frequencies_Left[freq_amp] + (Frequencies_Left[freq_amp] + analogRead(AUDIO_LEFT)) >> 1) >> 1;
|
|
86
|
+ Frequencies_Right[freq_amp] = (Frequencies_Right[freq_amp] + (Frequencies_Right[freq_amp] + analogRead(AUDIO_RIGHT)) >> 1) >> 1;
|
|
87
|
+ digitalWrite(STROBE, HIGH);
|
|
88
|
+ digitalWrite(STROBE, LOW);
|
|
89
|
+ level += Frequencies_Left[freq_amp] + Frequencies_Right[freq_amp];
|
|
90
|
+ }
|
|
91
|
+ level /= 12;
|
|
92
|
+ Serial.print(level);
|
|
93
|
+ Serial.print(' ');
|
|
94
|
+ if (level > loud) {
|
|
95
|
+ loud = level;
|
|
96
|
+ } else {
|
|
97
|
+ loud = max(level, int(round(float(loud) * (1.0 - LOUDNESS_DECAY))));
|
|
98
|
+ }
|
|
99
|
+}
|
|
100
|
+
|
|
101
|
+/*******************Light LEDs based on frequencies*****************************/
|
|
102
|
+void Graph_Frequencies() {
|
|
103
|
+ for (int i = 0; i < 3; i++)
|
|
104
|
+ {
|
|
105
|
+ int l = (Frequencies_Left[i * 2] + Frequencies_Left[i * 2 + 1]) / 2;
|
|
106
|
+ int r = (Frequencies_Right[i * 2] + Frequencies_Right[i * 2 + 1]) / 2;
|
|
107
|
+ SoftPWMSet(leds[i], NORMALIZE(l));
|
|
108
|
+ SoftPWMSet(leds[i + 3], NORMALIZE(r));
|
|
109
|
+ }
|
|
110
|
+}
|