简体   繁体   English

我该如何制作它,以便每个循环每次出现只打印一次?

[英]How do I make it so it only prints once per occurrence per loop?

I want this to print Start and stop times so I can verify lights are going On and OFF when they should.我想让它打印开始和停止时间,这样我就可以验证灯在应该打开和关闭的时候打开和关闭。 Example: Light schedule Lights ON 7:00 AM Lights OFF 1:00 AM Lights ON 7:00 AM...示例: 照明时间表 上午 7:00 开灯 上午 1:00 关灯 上午 7:00 开灯...

What I'm getting instead is printing lights ON from 7am-1am printing lights OFF from 1am-7am...相反,我得到的是从早上 7 点到凌晨 1 点打开打印灯,从早上 1 点到早上 7 点关闭打印灯......

try:
    while True:
        now = datetime.datetime.now().time()
        GPIO.output(R1,GPIO.LOW)
        if now.hour == 7:
            GPIO.output(R1, GPIO.HIGH)
            print("Lights ON")
            print(time.strftime("%-I:%M %p"))
        elif now.hour == 1:
            GPIO.output(R1, GPIO.LOW)
            print("Lights OFF") 
            print(time.strftime("%-I:%M %p"))       
finally:
    GPIO.cleanup()

What am I missing?我错过了什么?

EDIT: Updated my answer based on more clarity.编辑:根据更清晰更新我的答案。

  1. Assuming that GPIO.output() is like an on-off switch, you need to make sure that the first default setting is outside the while loop.假设GPIO.output()就像一个开关,您需要确保第一个默认设置在 while 循环之外。

  2. Since you want the loop to run indefinitely, you cant use break .由于您希望循环无限期运行,因此您不能使用break If you do that however, the loop will keep updating the GPIO indefinitely, which you dont want either.但是,如果您这样做,则循环将无限期地更新GPIO ,这也是您不想要的。 So, I would recommending adding a flag toggle .所以,我建议添加一个 flag toggle

  3. If toggle==1 , the if condition is allowed to update the GPIO values else if can't.如果toggle==1 ,如果不能,则允许 if 条件更新GPIO值。 By default it is set to 1, so the second it encounters now.hour==7 or 1 , it will update it.默认情况下它设置为 1,所以它遇到now.hour==71的第二个,它会更新它。 But once its done implementing the code, it sets it to 0 again.但是一旦它完成了代码的实现,它就会再次将其设置为0 This prevents it from continuously updating the GPIO values.这可以防止它不断更新GPIO值。

  4. Now, when the hour changes, you want the toggle to go back to 1, so that it can check the hour and update if needed.现在,当小时更改时,您希望将 go toggle回 1,以便它可以检查小时并在需要时进行更新。 This is done by storing the value of the previous hour in lasthour and checking it as part of a elif condition.这是通过将前一小时的值存储在lasthour中并将其作为elif条件的一部分进行检查来完成的。

  5. Lastly, you use time.strftime .最后,您使用time.strftime Instead, you can simply use now.strftime to get the time printed in the right format.相反,您可以简单地使用now.strftime以正确的格式打印时间。

import datetime

testtime = datetime.datetime(2020,1,6,7)

try:
    #GPIO.output(R1, GPIO.LOW)
    toggle = 1
    lasthour = 0
    
    while True:  
        now = testtime
        
        if now.hour == 7 and toggle==1:
            #GPIO.output(R1, GPIO.HIGH)
            print("Lights ON")
            print(now.strftime("%-I:%M %p"))
            lasthour = now.hour
            toggle=0
            
        elif now.hour == 1 and toggle==1:
            #GPIO.output(R1, GPIO.LOW)
            print("Lights OFF") 
            print(now.strftime("%-I:%M %p"))
            lasthour = now.hour              #<- Save last hour of updatre
            toggle=0                         #<- Set toggle to 0
        
        elif lasthour != now.hour:           #<- If change in hour
            toggle=1                         #<- Set toggle to 1
            
        print('_________running_________; toggle ->', toggle) #Comment this
finally:
    print("out of while loop")
    #GPIO.cleanup()
Lights ON
7:00 AM
_________running_________; toggle -> 0
_________running_________; toggle -> 0
_________running_________; toggle -> 0
_________running_________; toggle -> 0
_________running_________; toggle -> 0
_________running_________; toggle -> 0
_________running_________; toggle -> 0
    try:
    while True:
        now = datetime.datetime.now().time()
        GPIO.output(R1,GPIO.LOW)
        if now.hour == 7 and now.minute == 0:
            GPIO.output(R1, GPIO.HIGH)
            print("Lights ON")
            print(time.strftime("%-I:%M %p"))
        elif now.hour == 1 and now.minute == 0:
            GPIO.output(R1, GPIO.LOW)
            print("Lights OFF") 
            print(time.strftime("%-I:%M %p"))       
finally:
    GPIO.cleanup()

You have to check the minutes too, otherwise it will print Lights ON from 7:00 till 7:59 and the same with lights off.您还必须检查分钟,否则它会从 7:00 到 7:59 打印 Lights ON 并且在关灯的情况下也是如此。

Also, be aware of the while loop.另外,请注意 while 循环。 The way you coded it, it will run forever unless you stop it manually.按照您的编码方式,它将永远运行,除非您手动停止它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM