123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #include <SoftPWM.h>
- #include <SoftPWM_timer.h>
-
- // Dimmer potentiometer pin
- #define DIMMER A5
-
- // LED strip V- pins
- // The idea was that LED pins would be PWM, but board only has six, including pin 5
- // (used as the spectrum shield's RESET pin [arggghhh]), so we use the SoftPWM library
- // which allows us to replace pin 5 with 13
- int leds[6] = { 9, 10, 11, 3, 13 /* don't you hate desoldering? */, 7};
-
-
- // Spectrum Shield pin connections
- #define STROBE 4
- #define RESET 5 // See [arggghhh] above
- #define AUDIO_LEFT A0
- #define AUDIO_RIGHT A1
-
- //Define spectrum variables
- int Frequencies_Left[7] = {0};
- int Frequencies_Right[7] = {0};
-
- #define NOISE_LEVEL 32 // even "silence" has frequencies ;)
-
- int loud = 0; // dynamically determined from sound
- int dimmer; // DIMMER pot value scaled to 8 bit
- int quiet; // level below which LED is off (computed from dimmer and loud)
- #define LOUDNESS_DECAY 0.01
- #define NORMALIZE(level) level>NOISE_LEVEL ? constrain(map(level, quiet, max(loud, (quiet * 5 / 4)), 0, dimmer), 0, 255) : 0
-
- /********************Setup*************************/
- void setup() {
- Serial.begin(9600);
- // init led pins, and flash them (to help check wiring)
- SoftPWMBegin();
- for (int i = 0; i < 6; i++) {
- pinMode(leds[i], OUTPUT);
- SoftPWMSetFadeTime(leds[i], 23, 42);
- SoftPWMSet(leds[i], 63);
- delay(250);
- SoftPWMSet(leds[i], 0);
- }
-
- //Set spectrum Shield pin configurations
- pinMode(STROBE, OUTPUT);
- pinMode(RESET, OUTPUT);
- pinMode(AUDIO_LEFT, INPUT);
- pinMode(AUDIO_RIGHT, INPUT);
- digitalWrite(STROBE, HIGH);
- digitalWrite(RESET, HIGH);
-
- //Initialize Spectrum Analyzers
- digitalWrite(STROBE, LOW);
- delay(1);
- digitalWrite(RESET, HIGH);
- delay(1);
- digitalWrite(STROBE, HIGH);
- delay(1);
- digitalWrite(STROBE, LOW);
- delay(1);
- digitalWrite(RESET, LOW);
- }
-
-
- /**************************Main Loop*****************************/
- void loop() {
- Read_Frequencies();
- dimmer = 255 - (analogRead(DIMMER) >> 2);
- quiet = map(dimmer, 255, 0, NOISE_LEVEL, max((loud * 9) / 10, (NOISE_LEVEL * 10) / 9));
- Graph_Frequencies();
- Serial.print(loud);
- Serial.print(' ');
- Serial.println(quiet);
- }
-
-
- /*******************Pull frquencies from Spectrum Shield********************/
- void Read_Frequencies() {
- int level = 0;
- //Read frequencies for each band
- for (int freq_amp = 0; freq_amp < 7; freq_amp++)
- {
- // a crude low-pass to smooth out noises
- Frequencies_Left[freq_amp] = (Frequencies_Left[freq_amp] + (Frequencies_Left[freq_amp] + analogRead(AUDIO_LEFT)) >> 1) >> 1;
- Frequencies_Right[freq_amp] = (Frequencies_Right[freq_amp] + (Frequencies_Right[freq_amp] + analogRead(AUDIO_RIGHT)) >> 1) >> 1;
- digitalWrite(STROBE, HIGH);
- digitalWrite(STROBE, LOW);
- level += Frequencies_Left[freq_amp] + Frequencies_Right[freq_amp];
- }
- level /= 12;
- Serial.print(level);
- Serial.print(' ');
- if (level > loud) {
- loud = level;
- } else {
- loud = max(level, int(round(float(loud) * (1.0 - LOUDNESS_DECAY))));
- }
- }
-
- /*******************Light LEDs based on frequencies*****************************/
- void Graph_Frequencies() {
- for (int i = 0; i < 3; i++)
- {
- int l = (Frequencies_Left[i * 2] + Frequencies_Left[i * 2 + 1]) / 2;
- int r = (Frequencies_Right[i * 2] + Frequencies_Right[i * 2 + 1]) / 2;
- SoftPWMSet(leds[i], NORMALIZE(l));
- SoftPWMSet(leds[i + 3], NORMALIZE(r));
- }
- }
|