我在 Raspberry Pi 上运行 python 脚本,我使用 4 个 gpio 引脚,3 个作为输出,1 个作为输入。当输入设置为低时,它会激活一个程序,即输出都在不同时间打开和关闭,并使用 pygame.mixer 播放声音效果。我开始遇到一些问题,即输入不再激活程序。当我进一步查看它时,我发现它只能工作 120 次,然后输入将不再激活程序,并且我不会收到任何错误消息。我绝不是 python 编码方面的专家,所以我的代码可能看起来很乱。这是代码。
import RPi.GPIO as GPIO
import sys
import time
import pygame
import os
from array import *
print "loading program"
GPIO.setmode(GPIO.BCM)
GpioPinsOut=[4,17,27]
for a in range (0, len(GpioPinsOut)):
GPIO.setup(GpioPinsOut[a], GPIO.OUT)
GpioPinsIn=[7,]
for y in range (0, len(GpioPinsIn)):
GPIO.setup(GpioPinsIn[y], GPIO.IN)
start=0
prevTriggered=0
triggered=0
elapsed=0
vars=[0,]
inVars=[0]
prevInVars=[5,5]
sceneNames=["Night"]
onoffVar= [1, 0]
onoffVar2= ["On", "Off"]
outputNamesFinal=['output1','output2','output3']
outputNames= [[['output1','output2','output3'],['output1','output2','output3','reset']]]
outputs= [[[4 ,17 ,27 ],[4 ,17 ,27 ,'reset']]]
times= [[[0.0 ,0.0 ,0.0 ],[11.7 ,18.0 ,18.0 ,18.0 ]]]
printGpioVar=[[[0 ,0 ,0 ],[0 ,0 ,0 ,0 ]]]
outputsAlwaysOn=[[]]
for va in range (0, len(GpioPinsOut)):
GPIO.output(GpioPinsOut[va], False)
for za in range (0, len(outputsAlwaysOn[0])):
GPIO.output(outputsAlwaysOn[0][za], True)
effects=[[],[]]
effectslocations=[['/root/audio/soundeffect.wav']]
pygame.mixer.init(44100, -16, 2, 2048)
for ba in range (0, len(effectslocations)):
for bb in range (0, len(effectslocations[ba])):
effectsVar = pygame.mixer.Sound(effectslocations[ba][bb])
effects[ba].append(effectsVar)
effectsNumbers=[[0 ]]
effectsTimes= [[0.0]]
sfxVAR = 0
numberOfTimes=0
print "program ready"
try:
while True:
for z in range (0, len(GpioPinsIn)):
if GPIO.input(GpioPinsIn[z]) == False:
inVars[z]=0
else:
inVars[z]=1
if inVars[0] == 0:
triggered = 1
if triggered == 1:
if prevTriggered != triggered:
print "Trigger Activated"
if start == 0:
print "Start Time Activated"
numberOfTimes+=1
print numberOfTimes
start = time.clock()
elapsed = time.clock() - start
for g in range(0, len(effectsTimes[0])):
if elapsed >= effectsTimes[0][g]:
if sfxVAR == 0:
effects[0][effectsNumbers[0][g]].play()
sfxVAR = 1
for c in range (0, len(times[0])):
for d in range (0, len(times[0][c])):
if elapsed >= times[0][c][d]:
if outputs[0][c][d] == "reset":
print "done with %s procedures"%sceneNames[0]
for e in range (0, len(printGpioVar[0])):
for f in range (0, len(printGpioVar[0][e])):
printGpioVar[0][e][f]=0
start=0
prevTriggered=0
sfxVAR=0
triggered=0
else:
if printGpioVar[0][c][d]==0:
GPIO.output(outputs[0][c][d], onoffVar[c])
print "Turned %s %s"%(onoffVar2[c], outputNames[0][c][d])
printGpioVar[0][c][d]=1
prevTriggered = triggered
for g in range (0, len(inVars)):
prevInVars[g]=inVars[g]
except KeyboardInterrupt:
GPIO.cleanup()
sys.exit()