Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #define vTestPin 0
  2. #define chargePin 13
  3. #define dischargePin 11
  4. #define resistorValue 10000.0F
  5. #define ADC_RES 1023.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 (true) {
  27. float charge = analogRead(vTestPin);
  28. elapsedTime = millis() - startTime;
  29.  
  30. if (charge > (ADC_RES * .63)) {
  31. microFarads = ((float)elapsedTime / resistorValue) * 1000;
  32. break;
  33. }
  34.  
  35. if (elapsedTime > 2000) {
  36. microFarads = 0;
  37. break;
  38. }
  39. }
  40.  
  41. // T = RC -> C = T / R
  42. elapsedTime = millis() - startTime;
  43. microFarads = ((float)elapsedTime / resistorValue) * 1000;
  44.  
  45. if ((microFarads > 0) && (microFarads < 30)) {
  46.  
  47. Serial.println("The electronic component is a capacitor");
  48.  
  49. if (microFarads > 1) {
  50. Serial.print((long)microFarads);
  51. Serial.println(" microFarads is the value of the capacitor and");
  52. delay(1600);
  53.  
  54. if (microFarads > 10) {
  55. Serial.println("falls into the category HIGH");
  56. }
  57.  
  58. else if ((microFarads > 1) && (microFarads < 10)) {
  59. Serial.println("falls into the category MEDIUM");
  60. }
  61.  
  62. else if (microFarads < 1) {
  63. Serial.println("falls into the category LOW");
  64. }
  65.  
  66. else {
  67. nanoFarads = microFarads * 1000.0;
  68. Serial.print((long)nanoFarads);
  69. Serial.println(" nanoFarads");
  70. delay(1600);
  71. }
  72. }
  73. }
  74.  
  75. if (microFarads > 100) {
  76. Serial.println("Please insert electronic component");
  77. }
  78.  
  79. float vx = analogRead(vTestPin) * V_HIGH / ADC_RES;
  80. Serial.print("The electric component is a resistor \n");
  81. Serial.print("Voltage across the electronic component in Volts = ");
  82. Serial.println(vx);
  83.  
  84. float ix = (V_HIGH - vx) / resistorValue;
  85. Serial.print("Resistance of the electronic component in Ohms = ");
  86. if ( ix > 0) {
  87. float rx = vx / ix;
  88. Serial.println(((int) rx));
  89. }
  90.  
  91. else {
  92. Serial.println("infinite!");
  93. }
  94.  
  95. // Allow capacitor to discharge
  96. digitalWrite(chargePin, LOW);
  97. pinMode(dischargePin, OUTPUT);
  98. digitalWrite(dischargePin, LOW);
  99. while (analogRead(vTestPin) > 0) {
  100. }
  101. pinMode(dischargePin, INPUT);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement