【问题标题】:Python Test sensors, then run them all again until keypressPython 测试传感器,然后再次运行它们直到按键
【发布时间】:2013-02-14 19:22:52
【问题描述】:

我正在为我的小温室制作一个 python 程序来测试不同的条件。 我需要它做的是:程序结束时,等待 5 分钟,然后再次运行所有测试。 这是我第一个完整的python程序,所以如果你能把答案保留在低学习曲线区域,那将是非常有用的。

示例:

temp=probe1  
humidity=probe2  
CO2=probe3  

if temp==25:  
      print ("25)"  
if humidity==90:  
      print ("90")  
if CO2==1000  
      print ("1000")  

import time  
time.sleep (300) 
start again from top until keypress

【问题讨论】:

    标签: python loops time sensors


    【解决方案1】:

    您可以将您现在拥有的内容放在while True loop 中以实现所需的结果。这将永远运行,每 5 分钟进行一次测量,直到您按 Ctrl-C 中断程序。

    import time
    while True:
        temp=probe1  
        humidity=probe2  
        CO2=probe3  
    
        if temp==25:  
              print ("25") 
        if humidity==90:  
              print ("90")  
        if CO2==1000  
              print ("1000")    
        time.sleep (300) 
    

    但是,我想知道您的传感器准确给出您检查的值的可能性有多大。根据传感器值的精度,您可能会持续数小时甚至更长时间没有任何输出。您可能想要检查四舍五入的传感器值,例如if round(temp) == 25

    或者您可能想知道temp 何时为 25 或更高,您可以通过if temp >= 25 查询。

    另一种可能性是始终打印传感器数据,并在值高于某个阈值的情况下打印额外警告,例如:

    import time
    while True:
        temp=probe1  
        humidity=probe2  
        CO2=probe3  
    
        print("Temp:", temp, "degrees")
        if temp>=25:  
              print ("  Too hot!")  
    
        print("Humidity:", humidity, "%")
        if humidity>=90:  
              print ("  Too humid!")  
    
        print("CO2:", CO2, "units")
        if CO2>=1000  
              print ("  Too much CO2!")    
        time.sleep (300) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 1970-01-01
      • 2022-12-19
      • 1970-01-01
      相关资源
      最近更新 更多