我正在编写一个程序,它将运行一个计时器,直到用户将电线插入两个端口并按下一个大红色按钮。在家里,我的程序工作得很好,但是当很多人在同一个房间时,他们只需触摸端口即可停止计时器。这有点问题,我不知道如何打击它。
简而言之,我的GPIO引脚设置如下:
def pressBigButton(pin):
self.paused = 1
self.label.configure(fg='#FFFFFF')self.switchPin = 4
GPIO.setup(self.switchPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(self.switchPin,GPIO.FALLING)
GPIO.add_event_callback(self.switchPin,pressBigButton)
根据我的理解,这应该将引脚设置为高电压,并且应该在用户按下按钮时掉落。我希望这可以消除静电引起电压尖峰的可能性,过早地使定时器跳闸。此外,我将端子连接到GND引脚,但这似乎没有帮助。
我可能有一些错误的符号。道歉。
如果你需要完整的代码,我可以在这里找到它。
import Tkinter as tk
from Tkinter import *
import RPi.GPIO as GPIO
import math
import time
class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
# make it cover the entire screen
w, h = self.winfo_screenwidth(), self.winfo_screenheight()
self.attributes("-fullscreen", True)
self.geometry("%dx%d+0+0" % (w, h))
self.label = tk.Label(self, text="", width=9, height=3, font=("Courier Bold", 160), fg='brown', bg='black')
self.label.pack()
self.startTime=time.time()
print "startTime", time.time()
#self.total can be changed to alter the timer's length, but it should never be changed in runtime
#Default = 3600
self.total = 3600
self.remaining = self.total
#timerMode 1 = standard clock, 2 = percentage
self.timerMode = 2
self.paused = 1
def pressSpace(event):
if(self.paused == 0 and self.remaining > 0):
self.paused = 1
self.label.configure(fg='#FFFFFF')
else:
self.paused = 0
self.label.configure(fg='#0000FF')
self.mainLoop()
def pressEsc(event):
if(self.paused == 1):
self.destroy()
def pressBigButton(pin):
self.paused = 1
self.label.configure(fg='#FFFFFF')
#bind spacebar to pause the timer
self.bind("<space>", pressSpace)
#bind Esc to kill the process
self.bind("<Escape>", pressEsc)
#initialize GPIO pin
self.switchPin = 4
GPIO.setup(self.switchPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(self.switchPin,GPIO.FALLING)
GPIO.add_event_callback(self.switchPin,pressBigButton)
self.pinStatus = GPIO.input(self.switchPin)
self.setTime() #sets the initial clock before unpausing
self.mainLoop()
def mainLoop(self):
if self.remaining <= 0:
#Time has expired. Players lose
self.paused = 1
self.label.configure(text="TIME!", font=("Courier Bold", 160), fg='#000000', bg='#FF0000')
else:
#There is still time on the clock. Check if player has flipped the switch to stop the timer
if not(GPIO.input(self.switchPin)):
self.paused = 1
self.label.configure(fg='white')
strTimer = self.setTime()
self.label.configure(text= strTimer)
if(self.paused == 0): #Check if the timer is toggled to pause
self.remaining = self.remaining - 1
self.after(1000, self.mainLoop)
#time.sleep(1.0 - ((time.time() - self.startTime) % 1.0))
self.setColor()
def setColor(self):
fremaining = (1.0*self.remaining)
blueDown = self.twoDigitHex(255*(fremaining/self.total))
redUp = self.twoDigitHex(255-(255*(fremaining/self.total)))
color = "#" + redUp + "00" + blueDown
self.label.configure(fg=color)
def twoDigitHex(self, number):
return '%02x' % number
def setTime(self):
if(self.timerMode == 1):
#STANDARD CLOCK
#creates a readout of constant length regardless of significant digits
h = str(math.trunc(self.remaining/3600))
remainder = (math.trunc(self.remaining % 3600))
m = str(remainder / 60)
s = str(remainder % 60)
time = []
index = 0
time.append(h)
time.append(m)
time.append(s)
for i in time:
if (len(i)==1):#If a number is 1 digit, make it 2 digits
time[index] = ('0' + i)
index += 1
if(time[0] == '01'):
return ('1:' + time[1] + ':' + time[2])
elif(time[0] == '00' and time[1] != '00'):
return (time[1] + ':' + time[2])
elif(time[0]== '00' and time[1]== '00'):
return (time[2])
else:
return (time[0] + ':' + time[1] + ':' + time[2])
elif(self.timerMode == 2):
#PERCENT CLOCK
percent = (100*(self.remaining / (self.total*(1.0))))
return(str('%.1f%%' % percent))
if __name__ == "__main__":
GPIO.setmode(GPIO.BCM)
app = ExampleApp()
app.mainloop()
GPIO.cleanup()
tl; dr:GPIO.FALLING有时会在用户触摸终端A和B时跳闸。我怀疑是静电。请指教。
答案 0 :(得分:0)
我不确定我是否完全理解您的问题,但如果您可以更改引脚排列和电阻的位置,您可以将其用作下拉,您需要更改代码以检测上升沿。 所以你必须改变这个:
int(n/10)
到这个
GPIO.setup(self.switchPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(self.switchPin,GPIO.FALLING)
在您的电路中,您需要将端子GPIO.setup(self.switchPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(self.switchPin,GPIO.RISING)
连接到A
引脚而不是3.3V
引脚,然后移除10k电阻。如果仍然得到错误的结果,您可以尝试将10k电阻连接到GND
,输入引脚(在这种情况下为引脚GND
)。
我没有足够的代表发布图片,但这里是链接: schematics (忽略连接器上引脚1和2的标签,它只是我能找到的唯一一个快速的标签) 希望这有帮助