Advertisement
Guest User

Untitled

a guest
Jul 10th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <math.h>
  2. #define trigPin 12
  3. #define echoPin 13
  4. //Schematic:
  5. // [Ground] ---- [10k-Resister] -------|------- [Thermistor] ---- [+5v]
  6. // |
  7. // Analog Pin 0
  8.  
  9. double Thermistor(int RawADC) {
  10. // Inputs ADC Value from Thermistor and outputs Temperature in Celsius
  11. // requires: include <math.h>
  12. // Utilizes the Steinhart-Hart Thermistor Equation:
  13. // Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3}
  14. // where A = 0.001129148, B = 0.000234125 and C = 8.76741E-08
  15. long Resistance; double Temp; // Dual-Purpose variable to save space.
  16. Resistance=((10240000/RawADC) - 10000); // Assuming a 10k Thermistor. Calculation is actually: Resistance = (1024 * BalanceResistor/ADC) - BalanceResistor
  17. Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later. // "Temp" means "Temporary" on this line.
  18. Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp)); // Now it means both "Temporary" and "Temperature"
  19. Temp = Temp - 273.15; // Convert Kelvin to Celsius // Now it only means "Temperature"
  20.  
  21. // BEGIN- Remove these lines for the function not to display anything
  22. Serial.print("ADC: "); Serial.print(RawADC); Serial.print("/1024"); // Print out RAW ADC Number
  23. Serial.print(", Volts: "); printDouble(((RawADC*4.860)/1024.0),3); // 4.860 volts is what my USB Port outputs.
  24. Serial.print(", Resistance: "); Serial.print(Resistance); Serial.print("ohms");
  25. // END- Remove these lines for the function not to display anything
  26.  
  27. // Uncomment this line for the function to return Fahrenheit instead.
  28. Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert to Fahrenheit
  29. return Temp; // Return the Temperature
  30. }
  31.  
  32. void printDouble(double val, byte precision) {
  33. // prints val with number of decimal places determine by precision
  34. // precision is a number from 0 to 6 indicating the desired decimal places
  35. // example: printDouble(3.1415, 2); // prints 3.14 (two decimal places)
  36. Serial.print (int(val)); //prints the int part
  37. if( precision > 0) {
  38. Serial.print("."); // print the decimal point
  39. unsigned long frac, mult = 1;
  40. byte padding = precision -1;
  41. while(precision--) mult *=10;
  42. if(val >= 0) frac = (val - int(val)) * mult; else frac = (int(val) - val) * mult;
  43. unsigned long frac1 = frac;
  44. while(frac1 /= 10) padding--;
  45. while(padding--) Serial.print("0");
  46. Serial.print(frac,DEC) ;
  47. }
  48. }
  49.  
  50. void setup() {
  51. Serial.begin(115200);
  52. pinMode(trigPin, OUTPUT);
  53. pinMode(echoPin, INPUT);
  54.  
  55.  
  56. }
  57.  
  58. #define ThermistorPIN 1 // Analog Pin 0
  59. double temp;
  60.  
  61.  
  62.  
  63. void loop() {
  64. temp=Thermistor(analogRead(ThermistorPIN)); // read ADC and convert it to Celsius
  65. Serial.print(", Celsius: "); printDouble(temp,3); // display Celsius
  66. //temp = (temp * 9.0)/ 5.0 + 32.0; // converts to Fahrenheit
  67. //Serial.print(", Fahrenheit: "); printDouble(temp,3); // display Fahrenheit
  68. Serial.println(""); // End of Line
  69. delay(100); // Delay a bit... for fun, and to not Serial.print faster than the serial connection can output
  70.  
  71. float newTemp = 1/((331.5+(temp*.6))/10000);
  72. float duration, distance;
  73. digitalWrite(trigPin, HIGH);
  74. delayMicroseconds(1000);
  75. digitalWrite(trigPin, LOW);
  76. duration = pulseIn(echoPin, HIGH);
  77. distance = (duration/2) / newTemp;
  78. if (distance >= 200 || distance <= 0){
  79. Serial.println("Out of range");
  80. }
  81. else {
  82. Serial.print(distance);
  83. Serial.println(" cm");
  84. }
  85. delay(500);
  86.  
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement