       Serial.print("\t");
     }
     Serial.println("");
-
-  }
-
-  if (diff > 2 * magAvg[0] && phaseNearZero) {
-    digitalWrite(LED_PIN, HIGH);
 
     Serial.print("BPM:\t");
     for (int i = 0; i < endIndex; i++) {

    for (int i = startIndex; i < endIndex; i++) {
        float magnitude = mag[i];
        magAvg[i] = magAvg[i] * 0.99 + magnitude * 0.01;
        if (magAvg[i] > maxMag && i > 1) {
            maxMag = magAvg[i];
            maxIndex = i;
        }

  if (diff > 2 * magAvg[0]) {
    digitalWrite(LED_PIN, LOW);

    Serial.print("Prev :\t");
    for (int i = 0; i < endIndex; i++) {
      float phase = prevPhase[i];
      Serial.print(phase);
      Serial.print("\t");
    }
    Serial.println("");

    // Serial.print("Diff:\t"); Serial.println(diff);
    Serial.print("Phase:\t");
    for (int i = 0; i < endIndex; i++) {
      float phase = phaseAvg[i];
