/* * Code for Raphael Arar's Installation at SCMAH 2016-10-22 * Work in progress * Uses a spectrum shield to isolate 7 frequency bands stereo on audio input * This sketch expresses that data in a couple of ways. */ // Neopixels #include #define NUM_LEDS 24 #define DATA_PIN 6 CRGB leds[NUM_LEDS]; // Spectrum shield pins #define STROBE 4 #define RESET 5 #define DC_One A0 #define DC_Two A1 #define BANDS 7 class Ring { public: int firstLed = -1; int ledCount = 0; Ring(int firstLed, int ledCount) { this->firstled = firstLed; this->ledCount = ledCount; } } #define FAKE_THE_FREQUENCIES 1 void initSpectrumShield() { pinMode(STROBE, OUTPUT); pinMode(RESET, OUTPUT); pinMode(DC_One, INPUT); pinMode(DC_Two, 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); } void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.print("hello"); FastLED.addLeds(leds,NUM_LEDS); LEDS.setBrightness(30); initSpectrumShield(); } int frequencyAmplitudesLeft[BANDS] = {0}; int frequencyAmplitudesRight[BANDS] = {0}; // scale spectrum shield input a nice amount. int analogReadAndPin(int pin) { int v = analogRead(pin); v = v * 2 / 3; if(v > 255) v = 255; return v; } void readFrequencies() { #if FAKE_THE_FREQUENCIES for(int ix = 0; ix < BANDS; ix++) { if(random(0,44) == 13) { frequencyAmplitudesLeft[ix] = random(128,255); frequencyAmplitudesRight[ix] = random(128,255); } else { frequencyAmplitudesLeft[ix] *= 0.85; frequencyAmplitudesRight[ix] *= 0.85; } } return; #endif // from https://learn.sparkfun.com/tutorials/spectrum-shield-hookup-guide // Read frequencies for each band for (int ix = 0; ix= 0 && x < NUM_LEDS) leds[xI] += color; xI++; if(xI >= 0 && x < NUM_LEDS) leds[xI] += color2; } static float migrate(float x, float amount, float towards) { float del = towards - x; if(abs(del) <= amount) x = towards; else if(del < 0) x -= amount; else x += amount; return x; } static int migrateI(int x, int amount, int towards) { { int del = towards - x; if(abs(del) <= amount) x = towards; else if(del < 0) x -= amount; else x += amount; return x; } } void fadeall(int amt) { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(amt); } } float ptn1Current = 0; float ptn1Target = 0; void doPtn0Piece(int b1, int b2, int hueBase, int xBase) { // Lowest frequency ptn1Target = frequencyAmplitudesLeft[b1] * NUM_LEDS / 4 / 255; ptn1Current = migrate(ptn1Current, 3.8, ptn1Target); int hue = hueBase + frequencyAmplitudesLeft[b2] / 5; setPixel(xBase + ptn1Current, CHSV(hue, 255,255)); } void doPtn0() { doPtn0Piece(0,1, 0, 0); doPtn0Piece(2,3, 85, 10); doPtn0Piece(4,5, 170, 20); fadeall(220); } /* -------------------- * Pattern '1' * Pulses the entire rign one color based on band1. * band2 influence the color just a little bit. * We smooth the fadeout slightly */ static int ptn1Level = 0; static int ptn1Hue; void doPtn1(int hue, int band1, int band2) { int newLevel = frequencyAmplitudesRight[band1]; //if(newLevel > ptn1Level) ptn1Level = newLevel; // else // { // ptn1Level = migrateI(ptn1Level, 1, newLevel); // } ptn1Hue = migrateI(hue, 10, hue + frequencyAmplitudesLeft[band2] / 9); CHSV c = CHSV(ptn1Hue, 255, ptn1Level); for(int ix = 0; ix < NUM_LEDS; ix++) leds[ix] = c; } /* --------------------------- * Pattern '2' * Shows pairs of lamps, Left & Right for each frequency band. * Plain & Simple. */ void doPtn2(int hue) { for(int ix = 0; ix < NUM_LEDS; ix++) leds[ix] = CHSV(0,0,0); for(int ix = 0; ix < BANDS; ix++) { int k = ix * 3; leds[k] = CHSV(0, 255, frequencyAmplitudesLeft[ix]); leds[k + 1] = CHSV(hue, 255, frequencyAmplitudesRight[ix]); } } unsigned long ticks = 0; void loop() { ticks++; int whichPattern = (ticks / 400) % 3; readFrequencies(); switch(whichPattern) { case 0: doPtn0(); break; case 1: doPtn1(60, 0,1); break; case 2: doPtn2(200 + ticks / 2000); // let the hue drive a little, very little, over long time. break; } FastLED.show(); delay(20); return; // for(int ix = 0; ix < NUM_LEDS; ix++) // leds[ix] = CHSV(ix * ticks, 255, ticks * ticks + ix); FastLED.show(); delay(10); }