     digitalWrite(spectrumStrobe,HIGH);
     digitalWrite(spectrumStrobe,LOW);     
   }
+  captureBeat(spectrum[0]);
 }
 
 void sendSpectrum() {
-  Wire.write(spectrum, 7);
+  spectrum[7] = bpm;
+  if (!offBeat && !sentBeat) {
+    spectrum[8] = 1;
+    sentBeat = true;
+  } else {
+    spectrum[8] = 0;
+    sentBeat = false;
+  }
+  Wire.write(spectrum, 9);
 }
+
+
+
+void captureBeat(byte value) {
+  huge[SIZE - (p+1)] = value;
+
+  p++;
+  if (p == SLICE) {
+    p=0;
+    long newEnergy = 0;
+    long sumEnergy = 0;
+    for (int j = SIZE-SLICE; j < SIZE;j++) {       
+      newEnergy += huge[j]*huge[j];
+    }
+    for (int i = 1; i < SIZE/SLICE;i++) {
+      sumEnergy += energy[i];
+      energy[i-1] = energy[i];
+      memcpy(huge+((i-1)*SLICE),huge+(i*SLICE), SLICE * sizeof(byte));
+    }
+    sumEnergy*=SLICE;
+    sumEnergy/=SIZE;
+    energy[(SIZE/SLICE)-1] = newEnergy;
+    // Serial.print(" ");     
+    // Serial.print(newEnergy);     
+    // Serial.print(" ");     
+    // Serial.print(sumEnergy);   
+    //     Serial.println(" ");      
+    long diff = millis()-t;
+    if (newEnergy > 1.3 * sumEnergy && diff > 300) {
+      if (offBeat) {
+        float newBPM = 60000/(float)diff;
+        bpm = bpm*.9 + newBPM*.1;
+        t = millis();  
+      }
+      offBeat = false;
+    } 
+    else {
+      offBeat = true;
+    }
+  }
+
+}
+

int spectrumStrobe=4;
int spectrumAnalog=0;  

byte spectrum[7];

void setup() { 
  Wire.begin(0x29);              
  Wire.onRequest(sendSpectrum); 
  

//just loop and read - need to add normalization
void loop() {
  readSpectrum();
}

// Read 7 band equalizer.
