温度传感器的最大值和最小值

时间:2020-03-20 16:29:31

标签: c++ arduino

(参考最后一个if语句)我这里有一些代码,使T_High始终与T相同。如果我正在寻找代码所见的最高温度,我不确定是否需要添加另一个变量并调整我的代码或当前的问题是什么。我尝试过在线查询最大和最小时间,但是即使有多个来源,我也无法使其正常工作。我了解在我的if语句之后,我让它们彼此相等,但是我认为if语句将解决这一问题。这是在粒子IDE中(非常类似于Arduino)。

#include <math.h>
const int thermistor_output = A1;

void setup() {
Serial.begin(9600); 
}

void loop() {

float T_Low = 999;
float T_High;

int x=0;
int thermistor_adc_val;
double a,b,c,d,e,f,g,h,i,j,T;
double output_voltage, thermistor_resistance, therm_res_ln, temperature_celsius;
while (x < 10 )
{
thermistor_adc_val = analogRead(thermistor_output);
output_voltage = ( (thermistor_adc_val * 3.3) / 4095.0 );
thermistor_resistance = ( ( 3.3 * ( 10.0 / output_voltage ) ) - 10 ); /* Resistance in kilo ohms */
thermistor_resistance = thermistor_resistance * 1000 ; /* Resistance in ohms   */
therm_res_ln = log(thermistor_resistance);
  /*  Steinhart-Hart Thermistor Equation: */
  /*  Temperature in Kelvin = 1 / (A + B[ln(R)] + C[ln(R)]^3)   */
  /*  where A = 0.001129148, B = 0.000234125 and C = 8.76741*10^-8  */
temperature_celsius = ( 1 / ( 0.001129148 + ( 0.000234125 * therm_res_ln ) + ( 0.0000000876741 * therm_res_ln * therm_res_ln * therm_res_ln ) ) ); /* Temperature in Kelvin */
temperature_celsius = temperature_celsius - 273.15; /* Temperature in degree Celsius */
T = temperature_celsius * 1.8 + 29; /* Temperature in degree Fahrenheit */

delay(100);

 x ++ ;
 if (x==1){a=T;}
 if (x==2){b=T;}
 if (x==3){c=T;} 
 if (x==4){d=T;}
 if (x==5){e=T;}
 if (x==6){f=T;}
 if (x==7){g=T;}
 if (x==8){h=T;}
 if (x==9){i=T;}
 }

j=a+b+c+d+e+f+g+h+i;
T=j/9;
 x=0; 

delay(2000);

if (T > T_High) {
        T_High = T;
}

Particle.publish ("Temp", String(T));
Particle.publish ("High", String(T_High));
}
`

1 个答案:

答案 0 :(得分:0)

T_Highloop()中的局部变量。它仅在loop()函数退出之前存在。在下一次迭代中,将有一个T_High变量的 new 实例,该实例与上一个实例无关。因此,在调用T_High的整个过程中,您不会保存loop()的值。

将变量移动到文件范围内(即,在函数定义之外),以使其具有静态存储持续时间,并在整个程序执行过程中保留下来。

此外,在执行测试T_High之前,切勿将T > T_High初始化为值或将其赋值。此时T_High具有不确定的值,并且几乎以任何方式使用不确定的值都会导致程序具有未定义的行为,如此处的情况。

如果将变量移至文件范围,则此特定问题将自动得到解决,因为静态存储持续时间对象始终是零初始化的,但仍建议将T_High初始化为一个合理的值,例如:

float T_High = 0;

或适用于您的用例的任何东西。