SHARE
TWEET

Untitled

a guest Nov 17th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define vTestPin       0          
  2. #define chargePin      13        
  3. #define dischargePin   11        
  4. #define resistorValue  10000.0F
  5. #define ADC_RES        1024.0F
  6. #define V_HIGH         5
  7.  
  8. unsigned long startTime;
  9. unsigned long elapsedTime;
  10. float microFarads;                
  11. float nanoFarads;
  12.  
  13. void setup(){
  14.   pinMode(chargePin, OUTPUT);    
  15.   digitalWrite(chargePin, LOW);  
  16.   Serial.begin(9600);            
  17. }
  18.  
  19. void loop(){
  20.   // ---- Capacitance ----
  21.   // start charging capacitor, and record time
  22.   digitalWrite(chargePin, HIGH);  
  23.   startTime = millis();
  24.  
  25.   // wait for capacitor to charge ~63% (one T = RC)
  26.   while(analogRead(vTestPin) < (ADC_RES * .63)) {
  27.   }
  28.  
  29.   // T = RC -> C = T / R
  30.   elapsedTime= millis() - startTime;
  31.   microFarads = ((float)elapsedTime / resistorValue) * 1000;  
  32.   Serial.print(elapsedTime);      
  33.   Serial.print(" mS    ");        
  34.   if (microFarads > 1){
  35.     Serial.print((long)microFarads);      
  36.     Serial.println(" microFarads");        
  37.   }
  38.   else{
  39.     nanoFarads = microFarads * 1000.0;      
  40.     Serial.print((long)nanoFarads);        
  41.     Serial.println(" nanoFarads");          
  42.     delay(500);
  43.   }
  44.  
  45.   float vx = analogRead(vTestPin) * V_HIGH * ADC_RES;
  46.   Serial.print("vx: ");
  47.   Serial.println(vx);
  48.  
  49.   float ix = (V_HIGH - vTest) / resistorValue;
  50.     Serial.print("rx: ");
  51.   if( ix > 0) {
  52.     float rx = vx / ix;
  53.     Serial.println(rx);
  54.   }
  55.   Serial.println("infinite!");
  56.    
  57.  
  58.   // Allow capacitor to discharge
  59.   digitalWrite(chargePin, LOW);            
  60.   pinMode(dischargePin, OUTPUT);            
  61.   digitalWrite(dischargePin, LOW);          
  62.   while(analogRead(vTestPin) > 0){        
  63.   }
  64.   pinMode(dischargePin, INPUT);
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top