     // 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.
+        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.9*avg_times[i-1]) || (avg_times[i] > 2*1.1*avg_times[i-1])) {
+        if((avg_times[i] < 2*0.8*avg_times[i-1]) || (avg_times[i] > 2*1.2*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.");
+            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(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);
-
+    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(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->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 /////////////
-
-    bool pullup_test = test_pullup_down(true);
+    // 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);
+    bool pulldown_test = test_pullup_down(false, false);
+    resetSettings();
     Serial.print("PULLDOWN TEST "); Serial.println(pulldown_test ? "PASS" : "FAIL");
-    bool compare_test = test_compare();
+    bool compare_test = test_compare(true);
+    resetSettings();
     Serial.print("COMPARE TEST "); Serial.println(compare_test ? "PASS" : "FAIL");
-    bool compare_range_test = test_compare_range();
+    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();
+    bool averages_test = test_averages(true);
+    resetSettings();
     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.");
-    }
 }
 
 


#if defined(ADC_TEENSY_LC) // teensy LC
#define PINS 13
#define DIG_PINS 13
#define PINS_DIFF 2
uint8_t adc_pins[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8, A9,A10,A11, A12};
uint8_t adc_pins_dig[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8, A9,A10,A11, A12};
uint8_t adc_pins_diff[] = {A10, A11};

#elif defined(ADC_TEENSY_3_0)  // teensy 3.0
#define PINS 14
#define DIG_PINS 10
#define PINS_DIFF 4
uint8_t adc_pins[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13};
uint8_t adc_pins_dig[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9};
uint8_t adc_pins_diff[] = {A10, A11, A12, A13};

#elif defined(ADC_TEENSY_3_1) || defined(ADC_TEENSY_3_2)  // teensy 3.1/3.2
#define PINS 21
#define DIG_PINS 16
#define PINS_DIFF 4
uint8_t adc_pins[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,
                      A14, A15,A16,A17,A18,A19,A20 };
uint8_t adc_pins_dig[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9, A15,A16,A17,A18,A19,A20 };
uint8_t adc_pins_diff[] = {A10, A11, A12, A13};

#elif defined(ADC_TEENSY_3_5)  // Teensy 3.5
#define PINS 27
#define DIG_PINS 21
#define PINS_DIFF 2
uint8_t adc_pins[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8, A9, A10,
                      A11,A12,A13,A14,A15,A16,A17,A18,A19,A20,A21,A22,A23,A24, A25, A26};
uint8_t adc_pins_dig[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8, A9, A12,A13,A14,A15,A16,A17,A18,A19,A20,A23,A24};
uint8_t adc_pins_diff[] = {A10, A11};

#elif defined(ADC_TEENSY_3_6)  // Teensy 3.6
#define PINS 25
#define DIG_PINS 21
#define PINS_DIFF 2
uint8_t adc_pins[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8, A9, A10,
                      A11,A12,A13,A14,A15,A16,A17,A18,A19,A20,A21,A22,A23,A24};
uint8_t adc_pins_dig[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8, A9, A12,A13,A14,A15,A16,A17,A18,A19,A20,A23,A24};
uint8_t adc_pins_diff[] = {A10, A11};

#elif defined(ADC_TEENSY_4)  // Teensy 4
#define PINS 14
#define DIG_PINS 10
#define PINS_DIFF 0
uint8_t adc_pins[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8, A9, A10,
                      A11,A12,A13};
uint8_t adc_pins_dig[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8, A9};
uint8_t adc_pins_diff[] = {};
#endif // defined


int value = 0;

// test the analog pins that also have a digital function
bool test_pullup_down(bool pullup) {
    uint8_t mode = pullup ? INPUT_PULLUP : INPUT_PULLDOWN;

    const int max_val = adc->getMaxValue(ADC_0);

    bool pass_test = true;

    for (int i=0;i<DIG_PINS;i++) {
        pinMode(adc_pins_dig[i], mode);
        delay(50); // settle time
        value = adc->analogRead(adc_pins_dig[i]);
        bool fail_condition = pullup ? (value < 0.95*max_val) : (value > 0.05*max_val);
        if (fail_condition) {
            Serial.print("A"); Serial.print(i); Serial.print(": ");
            Serial.print("FAILED "); Serial.print(pullup ? "INPUT_PULLUP" : "INPUT_PULLDOWN");
            Serial.print("("); Serial.print(value); Serial.print(")");
            Serial.println(". ");
            pass_test = false;
        }
    }

    return pass_test;
}

const uint8_t pin_cmp = A0;

bool test_compare() {
    bool pass_test = true;

    // measurement will be ready if value < 1.0V
    adc->enableCompare(1.0/3.3*adc->getMaxValue(ADC_0), 0, ADC_0);

    pinMode(pin_cmp, INPUT_PULLUP); // set to max
    // this should fail
    value = adc->analogRead(pin_cmp, ADC_0);
    if (adc->adc0->fail_flag != ADC_ERROR::COMPARISON) {
        Serial.println("Comparison didn't fail.");
        Serial.println(adc->adc0->getError());
        pass_test = false;
    }
    adc->adc0->resetError();

    pinMode(pin_cmp, INPUT_PULLDOWN); // set to min
    // 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_compare_range() {
    bool pass_test = true;

    // ready if value lies out of [1.0,2.0] V

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

