2014-12-21 21 views
-1

我正在使用可以很好地显示当前温度的Arduino温度探针。但是我需要它来保持最高温度,并且只显示探头从热区域移出后的最大读数。所以我想建立当前的读数,如果当前的读数大于以前的读数,那么Serial.print(温度);但是如果当前读数小于以前的读数,那么将不会有串行打印温度,并且已经显示的读数将保持不变。我不知道该怎么做。需要仅显示最高温度的Arduino素描

+0

您是否尝试过的,大于操作? –

+0

我还没有拿出任何东西来放大之间的大于运算符。我只是有“温度”,这是目前的温度。我需要这样的东西:如果温度>以前的温度,然后打印。 – Rico

+0

什么能阻止你保存以前的温度? –

回答

1

假设您有一个如下图所示的草图,并从readTemp()等函数读取温度,则需要定义一个全局变量maxTemp,并使用此变量检查每个新温度值。

int maxTemp; 

void setup() { 
    maxTemp = -99; //assigning min temp value to make sure new value will take place of this in first comparison. 
} 

void loop() { 
    int newTemp; //This variable will keep new value. 
    newTemp = readTemp(); //Read temperature 

    if (newTemp >= maxTemp) { //Do the comparison, only if greater-than or equal. 
     maxTemp = newTemp; //Assign new temperature as maxTemp. 
     Serial.print(newTemp); //Write it to serial. 
    } 

    delay(250); //wait 250ms before another comparison. 
} 
0

使用这样的:

double maxTemp; 

void setup() { 
maxTemp = -99; // pre set value that won't be reached(lowest amount) } 

void loop() { 
    int newTemp; //This variable will keep new value. 
    newTemp = readTemp(); //Read temperature 

    if (newTemp >= maxTemp) { 
     maxTemp = newTemp; 
     Serial.print(newTemp); 
    } 

    delay(1000); 
}