Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. #include <math.h>
  2. #include<Console.h>
  3. //#include <NewSoftSerial.h> //if display in LCD(seeed serial LCD driver)
  4. //#include <SerialLCD.h> //if display in LCD(seeed serial LCD driver)
  5. //#include "temp.h"
  6.  
  7. const int temp_analog_in = A0; // Analog input pin connect to temperature sensor SUR pin
  8. const int objt_analog_in = A1; // Analog input pin connect to temperature sensor OBJ pin
  9. float temp_calibration=0; //this parameter was used to calibrate the temperature
  10. //float objt_calibration=0.000; //this parameter was used to calibrate the object temperature
  11. float temperature_range=10; //we make a map of temperature-voltage according to sensor datasheet. 10 is the temperature step when sensor and
  12. //object distance is 9CM.
  13. float offset_vol=0.014; //this parameter was used to set the mid level voltage,when put the sensor in normal environment after 10 min,
  14. //the sensor output 0.For example,the surrounding temperature is 29℃,but the result is 27℃ via the sensor,
  15. //you should set the reerence to 0.520 or more,according to your sensor to change.
  16. //the unit is V
  17.  
  18. //SerialLCD slcd(11,12);
  19. float tempValue = 0;
  20. float objtValue= 0;
  21. float current_temp=0;
  22. float temp=0;
  23. float temp1=0;
  24. float temp2=0;
  25. unsigned int temp3=0;
  26. const float reference_vol=0.500;
  27. unsigned char clear_num=0;//when use lcd to display
  28. float R=0;
  29. float voltage=0;
  30.  
  31.  
  32. long res[100]={
  33. 318300,302903,288329,274533,261471,249100,237381,226276,215750,205768,
  34. 196300,187316,178788,170691,163002,155700,148766,142183,135936,130012,
  35. 124400,119038,113928,109059,104420,100000,95788,91775,87950,84305,
  36. 80830,77517,74357,71342,68466,65720,63098,60595,58202,55916,
  37. 53730,51645,49652,47746,45924,44180,42511,40912,39380,37910,
  38. 36500,35155,33866,32631,31446,30311,29222,28177,27175,26213,
  39. 25290,24403,23554,22738,21955,21202,20479,19783,19115,18472,
  40. 17260,16688,16138,15608,15098,14608,14135,13680,13242,12819,
  41. 12412,12020,11642,11278,10926,10587,10260,9945,9641,9347,
  42. 9063,8789,8525,8270,8023,7785,7555,7333,7118,6911};
  43.  
  44. float obj [13][12]={
  45. /*0*/ { 0,-0.274,-0.58,-0.922,-1.301,-1.721,-2.183,-2.691,-3.247,-3.854,-4.516,-5.236}, //
  46. /*1*/ { 0.271,0,-0.303,-0.642,-1.018,-1.434,-1.894,-2.398,-2.951,-3.556,-4.215,-4.931}, //→surrounding temperature,from -10,0,10,...100
  47. /*2*/ { 0.567,0.3,0,-0.335,-0.708,-1.121,-1.577,-2.078,-2.628,-3.229,-3.884,-4.597}, //↓object temperature,from -10,0,10,...110
  48. /*3*/ { 0.891,0.628,0.331,0,-0.369,-0.778,-1.23,-1.728,-2.274,-2.871,-3.523,-4.232},
  49. /*4*/ { 1.244,0.985,0.692,0.365,0,-0.405,-0.853,-1.347,-1.889,-2.482,-3.13,-3.835},
  50. /*5*/ { 1.628,1.372,1.084,0.761,0.401,0,-0.444,-0.933,-1.47,-2.059,-2.702,-3.403},
  51. /*6*/ { 2.043,1.792,1.509,1.191,0.835,0.439,0,-0.484,-1.017,-1.601,-2.24,-2.936},
  52. /*7*/ { 2.491,2.246,1.968,1.655,1.304,0.913,0.479,0,-0.528,-1.107,-1.74,-2.431},
  53. /*8*/ { 2.975,2.735,2.462,2.155,1.809,1.424,0.996,0.522,0,-0.573,-1.201,-1.887},
  54. /*9*/ { 3.495,3.261,2.994,2.692,2.353,1.974,1.552,1.084,0.568,0,-0.622,-1.301},
  55. /*10*/ { 4.053,3.825,3.565,3.27,2.937,2.564,2.148,1.687,1.177,0.616,0,-0.673},
  56. /*11*/ { 4.651,4.43,4.177,3.888,3.562,3.196,2.787,2.332,1.829,1.275,0.666,0},
  57. /*12*/ { 5.29,5.076,4.83,4.549,4.231,3.872,3.47,3.023,2.527,1.98,1.379,0.72}
  58. };
  59.  
  60. float binsearch(long x)// this function used for measure the surrounding temperature
  61. {
  62. int low,mid,high;
  63. low=0;
  64. //mid=0;
  65. high=100;
  66. while (low<=high)
  67. {
  68. mid=(low+high)/2;
  69. if(x<res[mid])
  70. low= mid+1;
  71. else//(x>res[mid])
  72. high=mid-1;
  73. }
  74. return mid;
  75. }
  76.  
  77. float arraysearch(float x,float y)//x is the surrounding temperature,y is the object temperature
  78. {
  79. int i=0;
  80. float tem_coefficient=100;//放大器倍数100
  81. i=(x/10)+1;//环境温度
  82. voltage=(float)y/tem_coefficient;//转化后对应的原始电压
  83. //Serial.print("sensor voltage:t");
  84. //Serial.print(voltage,5);
  85. //Serial.print("V");
  86. for(temp3=0;temp3<13;temp3++)
  87. {
  88. if((voltage>obj[temp3][i])&&(voltage<obj[temp3+1][i]))
  89. {
  90. return temp3;
  91. }
  92. }
  93. }
  94. float temp_measure()
  95. {
  96. unsigned char i=0;
  97. float current_temp1=0;
  98. int signal=0;
  99. tempValue=0;
  100.  
  101. for(i=0;i<10;i++) //
  102. {
  103. tempValue+= analogRead(temp_analog_in);
  104. delay(10);
  105. }
  106. tempValue=tempValue/10;
  107. temp = tempValue*1.1/1023;
  108. R=2000000*temp/(2.50-temp);
  109. signal=binsearch(R);
  110. current_temp=signal-1+temp_calibration+(res[signal-1]-R)/(res[signal-1]-res[signal]);
  111. //current_temp1=current_temp;
  112. //slcd.setCursor(0,1);
  113. //slcd.print("sur T:");
  114. //slcd.print((int)current_temp1,DEC);
  115. Console.print("Surrounding temperature:");
  116. Console.print(current_temp);
  117. return current_temp;
  118. }
  119.  
  120. float objt_measure()
  121. {
  122. unsigned char i=0;
  123. unsigned char j=0;
  124. float sur_temp=0;
  125. unsigned int array_temp=0;
  126. float temp1,temp2;
  127. float final_temp=0;
  128. objtValue=0;
  129. for(i=0;i<10;i++)
  130. {
  131. objtValue+= analogRead(objt_analog_in);
  132. delay(10);
  133. }
  134. objtValue=objtValue/10;//平均值处理
  135. temp1=objtValue*1.1/1023;//+objt_calibration;
  136. sur_temp=temp1-(reference_vol+offset_vol);
  137. Console.print("t Sensor voltage:");
  138. Console.print(sur_temp,3);
  139. Console.print("V");
  140. //slcd.setCursor(11,1);
  141. //slcd.print((int)(temp1*10000),DEC);
  142. array_temp=arraysearch(current_temp,sur_temp*1000);
  143. temp2=current_temp;
  144. temp1=(temperature_range*voltage)/(obj[array_temp+1][(int)(temp2/10)+1]-obj[array_temp][(int)(temp2/10)+1]);
  145. final_temp=temp2+temp1;
  146. if((final_temp>100)||(final_temp<=-10))
  147. {
  148. Console.println ("t out of range!");
  149. }
  150. else
  151. {
  152. Console.print("t object temperature:");
  153. Console.println(final_temp,2);
  154. //Serial.println("℃");
  155. }
  156. ////////////////////////////////////////////////////
  157. //The fllowing code is used for display by serialLCD
  158. /*
  159. slcd.setCursor(0,0);
  160. slcd.print("obj T:");
  161. if((final_temp>=0)&&(final_temp<=100))
  162. {
  163. slcd.print(final_temp,DEC);
  164. }
  165. else if((final_temp<0)&&(final_temp>=-10))
  166. {
  167. slcd.setCursor(10,0);
  168. slcd.print('-');
  169. slcd.print(abs(final_temp),DEC);
  170. }
  171. else
  172. {
  173. slcd.setCursor(5,0);
  174. slcd.print("out of range");
  175. }*/
  176. //////////////////////////////////////////
  177. }
  178.  
  179. void setup()
  180. {
  181. Bridge.begin();
  182. Console.begin();
  183. while (!Console);
  184. Console.println("Hi, what's your name?");// initialize serial communications at 9600 bps:
  185. //slcd.begin();//Add while using LCD to display
  186. //slcd.backlight();//Turn on the backlight of LCD while using LCD to display
  187. analogReference(INTERNAL);//set the refenrence voltage 1.1V,the distinguishability can up to 1mV.
  188. //analogReference(INTERNAL1V1);//(mega only)set the refenrence voltage 1.1V,the distinguishability can up to 1mV.
  189. }
  190. //If use LCD to display, adding below code to clear the screen in time
  191. void loop()
  192. {
  193. temp_measure();
  194. objt_measure();
  195.  
  196. /*
  197. if(clear_num==10)
  198. {
  199. slcd.clear();
  200. clear_num=0;
  201. }
  202. else
  203. clear_num++;
  204. */
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement