-#define DDS_REFCLK_DEFAULT 19200
+#define DDS_REFCLK_DEFAULT 38400
 #define DDS_REFCLK_OFFSET  0
 #define DDS_DEBUG_SERIAL
 

}

ISR(ADC_vect) {
  TIFR1 = _BV(ICF1);
  PORTD |= _BV(2);
  dds.clockTick();
  PORTD &= ~_BV(2);
}

enum Sets {
  SET_REF,
  SET_TONE,
  SET_AMPLITUDE,
  SET_ADC_HALF
} setting = SET_TONE;

char freqBuffer[8];
char *freqBufferPtr = freqBuffer;
uint16_t lastFreq = 1200;

volatile uint16_t recordedPulseLength;
volatile bool recordedPulse = false;
volatile bool listening = false;
volatile uint8_t maxADC = 0, minADC = 255, adcHalf = 40;

void loop() {
  static uint16_t samples = 0;
  static uint16_t pulse;
  static uint32_t lastOutput = 0;
  if(recordedPulse) {
    uint32_t pulseAveraging;
    uint16_t tmpPulse;
    cli();
    recordedPulse = false;
    tmpPulse = recordedPulseLength;
    sei();
    if(samples++ == 0) {
      pulse = tmpPulse;
    } else {
      pulseAveraging = (pulse + tmpPulse) >> 1;
      pulse = pulseAveraging;
    }
    if((lastOutput + 1000) < millis()) {
      Serial.print(F("Pulse:   "));
      Serial.println(pulse);
      Serial.print(F("Last:    "));
      Serial.println(tmpPulse);
      Serial.print(F("Samples: "));
      Serial.println(samples);
      Serial.print(F("ADC M/M: "));
      Serial.print(minADC); minADC = 255;
      Serial.print(F(" / "));
      Serial.println(maxADC); maxADC = 0;
      Serial.print(F("Freq:    "));
      // F = 1/(pulse*(1/ref))
      // F = ref/pulse
      Serial.println((float)(dds.getReferenceClock()+(float)DDS_REFCLK_OFFSET)/(float)pulse);
      samples = 0;
      lastOutput = millis();
    }
  }
  while(Serial.available()) {
    char c = Serial.read();
    Serial.print(c);
