#include "mbed.h" // Global constants const double resistance = 100000; // 100K resistor, 99K read on meter const int ratedCapacitance = 470; // Global variables DigitalOut source(p5); DigitalInOut discharge(p6); AnalogIn capacitor(p20); // Global variables bool charged; float meter; int runs; int good; int bad; int main() { runs = 1; while (runs <= 10) { // reset counters good = 0; bad = 0; Timer t; // init timer double capacitance; // create precision variable for capacitance // Turn off source source = 0; // Discharge capacitor discharge.output(); // configure discharge pin as output only discharge = 0; charged = 0; wait(1); // Reset timer t.reset(); // Disable discharge pin discharge.input(); // reconfigure pin as input only discharge.mode(PullNone); // configure with no internal pullup or pulldown printf("\n-----\n"); printf("\nRun: %d\n", runs); // display number of runs // Turn on source voltage // and start timer source = 1; t.start(); // Continuously read capacitor until 63.2% charged while (charged != 1) { meter = capacitor.read(); good++; if ( (meter >= 0.632) && (meter < 0.634) ) { charged = 1; } else if (meter > 0.634) { bad++; printf("* debug - bad meter value: %.3f *\t", meter); printf("reads: %d\t", good); printf("bad: %d\n", bad); } } t.stop(); // stop timer capacitance = t.read_us()/resistance; // C will be uF, because time in seconds is in us printf("\n\nRated capacitance: %d uF\n", ratedCapacitance); printf("Last read: %.3f\n", meter); printf("Total reads: %d\t", good); printf("bad: %d\n", bad); printf("Micro-seconds: %d\n", t.read_us()); printf("Charge resistor: %g\n", resistance); printf("Capacitance:\t%.1f uF\n", capacitance); // output to terminal // Turn off source source = 0; // Discharge capacitor discharge.output(); discharge = 0; wait(1); // increment run count runs++; } printf("\nAll done.\n"); } // end main();