Hallo ich lerne, Python auf Himbeer pi 3 Modell B. zu codieren und herumspielen mit GPIO. Mein Skript schaltet eine LED ein, wenn der Empfangseingang == 1 ist, und schaltet sich bei Eingabe aus! = 1. Ich möchte auch die Zeit, wenn LED eingeschaltet ist und die Zeit, wenn es ausgeschaltet ist. (Startzeit und Endzeit) Ich benutze mehrere If/Elif-Bedingung, aber ich bin sicher, es gibt bessere Möglichkeiten, dies zu tun. Bitte erleuchte mich!himbeer pi python gpio timer
import RPi.GPIO as GPIO
import time
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(7,GPIO.OUT)
GPIO.output(7,0) #set ping 7 to be 0 at default
CatchTime = True
startTime = []
startTimeRead = []
endTime = []
try:
while True:
time.sleep(0.25)
if (GPIO.input(11) ==1)and CatchTime==True : #start counting
GPIO.output(7,1)
print(time.ctime())
startTime.append(time.time())
startTimeRead.append(time.ctime())
CatchTime = False
elif (GPIO.input(11) ==1)and CatchTime != True : #within count
GPIO.output(7,1)
print(time.ctime())
elif (GPIO.input(11) !=1) and CatchTime != True : #end of count
GPIO.output(7,0)
CatchTime = True
endTime.append(time.time())
else: #steady not count
GPIO.output(7,0)
CatchTime = True
except KeyboardInterrupt:
GPIO.cleanup()
print('start time:',startTimeRead)
print('end time:',endTime)
Diese Frage kann für die [rasberry-pi] (http://raspberrypi.stackexchange.com/) Seite oder [Code-Review] (http://codereview.stackexchange.com/) – Aaron
Dank @Aaron relevanter sein . Ich werde auch dort suchen. Ich bin ein Nooby ~ –