     // 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;
     }
 

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


    return pass_test;
}


bool test_averages() {
    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;
        if (debug) {
          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 80% and 120% their theoretical value.
    for(uint8_t i=1; i<4; i++) {
        if((avg_times[i] < 2*0.8*avg_times[i-1]) || (avg_times[i] > 2*1.2*avg_times[i-1])) {
            pass_test = false;
            if (debug) {
              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 resetSettings() {
  adc->setAveraging(16); // set number of averages
  adc->setResolution(12); // set bits of resolution
  adc->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED); // change the conversion speed
  adc->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED); // change the sampling speed

  adc->setAveraging(16, ADC_1); // set number of averages
  adc->setResolution(12, ADC_1); // set bits of resolution
  adc->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED, ADC_1); // change the conversion speed
  adc->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED, ADC_1); // change the sampling speed
  adc->adc1->recalibrate();
  
}

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(12); // set bits of resolution
    adc->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED); // change the conversion speed
    adc->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED); // change the sampling speed
    adc->adc0->recalibrate();

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

    delay(2000);

    ////// START TESTS /////////////
    // do the tests
       
    bool pullup_test = test_pullup_down(true, false);
    resetSettings();
    Serial.print("PULLUP TEST "); Serial.println(pullup_test ? "PASS" : "FAIL");
    bool pulldown_test = test_pullup_down(false, false);
    resetSettings();
    Serial.print("PULLDOWN TEST "); Serial.println(pulldown_test ? "PASS" : "FAIL");
    bool compare_test = test_compare(true);
    resetSettings();
    Serial.print("COMPARE TEST "); Serial.println(compare_test ? "PASS" : "FAIL");
    bool compare_range_test = test_compare_range(true);
    resetSettings();
    Serial.print("COMPARE RANGE TEST "); Serial.println(compare_range_test ? "PASS" : "FAIL");
    bool averages_test = test_averages(true);
    resetSettings();
    Serial.print("AVERAGES TEST "); Serial.println(averages_test ? "PASS" : "FAIL");
}


