     // this should be ok
     value = adc->analogRead(pin_cmp, ADC_0);
     if(adc->adc0->fail_flag != ADC_ERROR::CLEAR) {
+      if (debug) {
         Serial.println("Some other error happened when comparison should have succeeded.");
         Serial.println(adc->adc0->getError());
+      }
         pass_test = false;
     }
     adc->adc0->resetError();
 
+    adc->adc0->disableCompare();
 
     return pass_test;
 }
 
 
-bool test_averages() {
+bool test_averages(bool debug=false) {
     elapsedMicros timeElapsed;
     bool pass_test = true;
-    const uint32_t num_samples = 10000;
     const uint8_t averages[] = {4, 8, 16, 32};
     float avg_times[4];
     volatile int value = 0;

    // this is even worse for Teensy LC, where 4 avgs take about 2.6 more than none.
    if((avg_times[0] < 2*one_avg_time) || (avg_times[0] > 4*one_avg_time)) {
        pass_test = false;
        Serial.print("4 averages should take about 4 times longer than one, but it took ");
        Serial.print(avg_times[0]/one_avg_time); Serial.println(" times longer");
    }
    // check that the times are between 90% and 110% their theoretical value.
    for(uint8_t i=1; i<4; i++) {
        if((avg_times[i] < 2*0.9*avg_times[i-1]) || (avg_times[i] > 2*1.1*avg_times[i-1])) {
            pass_test = false;
            Serial.print(averages[i]); Serial.print(" averages should take about twice as long as "); Serial.print(averages[i-1]);
            Serial.print(", but it took "); Serial.print(avg_times[i]/avg_times[i-1]); Serial.println(" times longer.");
        }
    }

    return pass_test;
}

void setup() {

    pinMode(LED_BUILTIN, OUTPUT);

    Serial.begin(9600);


    ///// ADC0 ////
    // reference can be ADC_REFERENCE::REF_3V3, ADC_REFERENCE::REF_1V2 (not for Teensy LC) or ADC_REFERENCE::REF_EXT.
    //adc->setReference(ADC_REFERENCE::REF_1V2, ADC_0); // change all 3.3 to 1.2 if you change the reference to 1V2

    adc->setAveraging(16); // set number of averages
    adc->setResolution(16); // set bits of resolution

    // it can be any of the ADC_CONVERSION_SPEED enum: VERY_LOW_SPEED, LOW_SPEED, MED_SPEED, HIGH_SPEED_16BITS, HIGH_SPEED or VERY_HIGH_SPEED
    // see the documentation for more information
    // additionally the conversion speed can also be ADACK_2_4, ADACK_4_0, ADACK_5_2 and ADACK_6_2,
    // where the numbers are the frequency of the ADC clock in MHz and are independent on the bus speed.
    adc->setConversionSpeed(ADC_CONVERSION_SPEED::LOW_SPEED); // change the conversion speed
    // it can be any of the ADC_MED_SPEED enum: VERY_LOW_SPEED, LOW_SPEED, MED_SPEED, HIGH_SPEED or VERY_HIGH_SPEED
    adc->setSamplingSpeed(ADC_SAMPLING_SPEED::LOW_SPEED); // change the sampling speed

    // If you enable interrupts, notice that the isr will read the result, so that isComplete() will return false (most of the time)
    //adc->enableInterrupts(ADC_0);


    ////// ADC1 /////
    #if ADC_NUM_ADCS>1
    adc->setAveraging(16, ADC_1); // set number of averages
    adc->setResolution(16, ADC_1); // set bits of resolution
    adc->setConversionSpeed(ADC_CONVERSION_SPEED::LOW_SPEED, ADC_1); // change the conversion speed
    adc->setSamplingSpeed(ADC_SAMPLING_SPEED::LOW_SPEED, ADC_1); // change the sampling speed

    //adc->setReference(ADC_REFERENCE::REF_1V2, ADC_1);


    // If you enable interrupts, note that the isr will read the result, so that isComplete() will return false (most of the time)
    //adc->enableInterrupts(ADC_1);

    #endif

    adc->adc0->recalibrate();
    #if ADC_NUM_ADCS>1
    adc->adc1->recalibrate();
    #endif

    delay(2000);

    ////// START TESTS /////////////

    bool pullup_test = test_pullup_down(true);
    Serial.print("PULLUP TEST "); Serial.println(pullup_test ? "PASS" : "FAIL");
    bool pulldown_test = test_pullup_down(false);
    Serial.print("PULLDOWN TEST "); Serial.println(pulldown_test ? "PASS" : "FAIL");
    bool compare_test = test_compare();
    Serial.print("COMPARE TEST "); Serial.println(compare_test ? "PASS" : "FAIL");
    bool compare_range_test = test_compare_range();
    Serial.print("COMPARE RANGE TEST "); Serial.println(compare_range_test ? "PASS" : "FAIL");
    bool averages_test = test_averages();
    Serial.print("AVERAGES TEST "); Serial.println(averages_test ? "PASS" : "FAIL");
    if (pullup_test & pulldown_test & compare_test & compare_range_test & averages_test) {
      Serial.println("ALL TEST PASSED.");
    }
}



    // this should be ok
    value = adc->analogRead(pin_cmp, ADC_0);
    if(adc->adc0->fail_flag != ADC_ERROR::CLEAR) {
      if (debug) {
        Serial.println("Some other error happened when comparison should have succeeded.");
        Serial.println(adc->adc0->getError());
      }
        pass_test = false;
    }

