Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define vTestPin 0
- #define chargePin 13
- #define dischargePin 11
- #define resistorValue 10000.0F
- #define ADC_RES 1023.0F
- #define V_HIGH 5
- unsigned long startTime;
- unsigned long elapsedTime;
- float microFarads;
- float nanoFarads;
- void setup() {
- pinMode(chargePin, OUTPUT);
- digitalWrite(chargePin, LOW);
- Serial.begin(9600);
- }
- void loop() {
- // ---- Capacitance ----
- // start charging capacitor, and record time
- digitalWrite(chargePin, HIGH);
- startTime = millis();
- // wait for capacitor to charge ~63% (one T = RC)
- // if it takes more than 2 seconds, assume it's not a capacitor
- while (true) {
- float charge = analogRead(vTestPin);
- elapsedTime = millis() - startTime;
- if (charge > (ADC_RES * .63)) {
- microFarads = ((float)elapsedTime / resistorValue) * 1000;
- break;
- }
- if (elapsedTime > 2000) {
- microFarads = 0;
- break;
- }
- }
- // ---- Resistance ----
- float vx = analogRead(vTestPin) * V_HIGH / ADC_RES;
- float ix = (V_HIGH - vx) / resistorValue;
- float rx = -1; // If this isn't overwritten, we have an open circuit
- if ( ix > 0) {
- float rx = vx / ix;
- }
- // If this was a capacitor, print its capacitance
- if ((microFarads > 0.1) && (microFarads < 30)) {
- Serial.println("The electronic component is a capacitor");
- // If it can be printed in microfarads, do so
- if (microFarads > 1) {
- Serial.print((long)microFarads);
- Serial.println(" microFarads is the value of the capacitor and");
- if (microFarads > 10) {
- Serial.println("falls into the category HIGH");
- }
- else {
- Serial.println("falls into the category MEDIUM");
- }
- }
- // Otherwise print in nanofarads
- else {
- nanoFarads = microFarads * 1000.0;
- Serial.print((long)nanoFarads);
- Serial.println(" nanoFarads is the value of the capacitor and falls into the category LOW");
- }
- delay(1600);
- }
- // If this was a resistor, print its resistance
- else if (rx > 0) {
- Serial.println("The electronic component is a resistor");
- Serial.print(rx);
- Serial.println(" ohms is the value of the resistor");
- else {
- Serial.println("Insert component");
- }
- }
- // Allow capacitor to discharge
- digitalWrite(chargePin, LOW);
- pinMode(dischargePin, OUTPUT);
- digitalWrite(dischargePin, LOW);
- while (analogRead(vTestPin) > 0) {
- }
- pinMode(dischargePin, INPUT);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement