我有一个关于树莓派继电器的问题。早些时候我写了关于LED的文章,问题也是类似的,但这次这些方法不起作用。在
#!/usr/bin/env python
import sys
import time
import datetime
import RPi.GPIO as GPIO
import SDL_DS1307
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
Rele1 = 17
Rele2 = 27
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
filename = time.strftime("%Y-%m-%d%H:%M:%SRTCTest") + ".txt"
starttime = datetime.datetime.utcnow()
ds1307 = SDL_DS1307.SDL_DS1307(1, 0x68)
ds1307.write_now()
while True:
currenttime = datetime.datetime.utcnow()
deltatime = currenttime - starttime
data=time.strftime("%Y"+"%m"+"%d"+"%H"+"%M")
with open('data.txt') as f:
for line in f:
parts=line.split()
if parts[0]<=(data)<=parts[1]:
GPIO.output(Rele1, True)
GPIO.output(Rele2, False)
break
else:
GPIO.output(Rele1, False)
GPIO.output(Rele2, True)
sleep(0.10)
我已经编辑了一点代码,现在当if
是{
当if
是False
,那么中继就正常工作了。如果我把break
放在else
之后,代码将停止执行.txt文件检查,如果有更多的日期,程序将不执行任何操作
你需要在你的逻辑上下功夫。想想当时间介于第三行的值之间时会发生什么数据.txt. 当您的代码读取第一行时,结果为false,因此相应地设置输出,然后在第二行,结果为false,输出再次设置为false,然后第三行结果为true,因此输出设置为true。然后立即再次读取文件,第一行结果为false,第二行为false,第三行为true。所以每次读取文件时,你的中继都会被设置为假-假-真,一次又一次。在
您需要使代码只在任何测试为真(逻辑OR)时设置输出,而不是为每个测试的结果设置输出。在
您可能需要在文件的所有行中计算OR,然后在完成文件后,如果结果是true或false,则相应地设置输出。在
作为调试的提示,如果在if语句的每个分支中都放置了一个简单的print语句,那么您将在控制台上看到第一行后面的一行有一个真正的结果,那么每次读取文件时输出都会被切换。在
`
相关问题 更多 >
编程相关推荐