<p>我刚拿起我的第一个树莓派和2通道继电器。我正在尝试学习如何用Python编写代码,所以我认为使用Pi是一个很好的起点。我有一个关于通过GPIO引脚的继电器的时间的问题。在</p>
<p>不过,首先是用我的文字编辑。到目前为止,我的剧本请看以下内容:</p>
<pre><code># !/usr/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# init list with pin numbers
pinList = [14]
# loop through pins and set mode and state to 'high'
for i in pinList:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)
# time to sleep between operations in the main loop
SleepTimeL = 60 #1 minute
# main loop
try:
GPIO.output(14, GPIO.LOW)
print "open"
time.sleep(SleepTimeL);
GPIO.cleanup()
#Reset GPIO settings
GPIO.cleanup()
# end program cleanly
except <a href="https://www.cnpython.com/pypi/keyboard" class="inner-link">keyboard</a>Interrupt:
print "done"
</code></pre>
<p>现在工作得很好了,它打开了连接到引脚14的继电器没问题。它按要求循环60秒,然后结束程序。一旦程序结束,GPIO设置被重置,继电器关闭,但这是程序的结束,这是我的问题开始。在</p>
<p>我想要这个脚本做的是打开继电器60秒,然后关闭它180秒。一旦达到180秒,它必须重新运行'try'语句,然后再打开继电器60秒,依此类推。简而言之,我想要一个无限循环,它只能通过取消运行脚本来中断。我不知道如何告诉Python关闭中继180秒,然后重新运行try语句,或者如何使其成为无限循环。在</p>
<p>我真的很感谢社区的一些意见。如有任何反馈或帮助,我们将不胜感激。谢谢大家。在</p>