我做了倒車雷達raspberry pi
和python
無限while循環蟒蛇
這是代碼:
import RPi.GPIO as GPIO
import time
#from picamera import PiCamera
from time import sleep
from gpiozero import MotionSensor
import smtplib
sender = '*****@gmail.com'
reciever = '*****@gmail.com'
def BlueLED(): #Blue LED Function
GPIO.output(27, GPIO.HIGH)
time.sleep(3)
GPIO.output(27, GPIO.LOW)
def RedLED(): #Red LED Function
GPIO.output(22,GPIO.HIGH)
time.sleep(3)
GPIO.output(22, GPIO.LOW)
def Buzzer(): #Buzzer Function
GPIO.output(17, GPIO. HIGH)
time.sleep(3)
GPIO.output(17, GPIO.LOW)
def email(sender,reciever,msg):
try :
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.login(sender,'******')
server.sendmail(sender,reciever,msg)
server.close()
print('Email sent!')
except :
print('Error')
try :
GPIO.setmode(GPIO.BCM)
#camera = PiCamera()
pir = MotionSensor(4)
GPIO.setwarnings(False)
GPIO.setup(27, GPIO.OUT) #blueLED
GPIO.setup(22, GPIO.OUT) #redLED
GPIO.setup(17, GPIO.OUT) #buzzer
GPIO.setup(18, GPIO.OUT) #tempsensor
GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP) #entry button
count = 0
while True :
if (pir.motion_detected):
print('Motion Detected')
#Calling the buzzer function
#Buzzer()
#The content that is going to be sent via email
msg = """Subject : Car Park
(Picture) """
email(sender,reciever,msg)
print('\nPlease press the button for the gate to open')
while True :
if(GPIO.input(21) == False):
if (count < 5):
BlueLED()
print('\nThere are ',(5-count), ' parking spaces empty ')
else :
RedLED()
print('\nSorry but the parking is full')
count = count + 1
except Exception as ex :
print('Error occured',ex)
我的問題是,第一個while循環不工作,也就是說,如果運動傳感器觸發什麼也沒有發生,但你可以壓制按鈕和計數增加。我猜這有一個簡單的解決方案,但似乎沒有想到。會愛你的幫助,謝謝
這是很清楚你的問題是什麼。 「我的問題是第一個while循環不起作用」是什麼意思? – Carcigenicate
因爲我有兩個while循環,循環整個程序的循環不是無限循環(儘管這是它應該做的)。這意味着當運動傳感器觸發任何類型的運動時,什麼都不會發生。第二個while循環控制計數器和按鈕工作正常。因此,當按下按鈕時,計數器會增加,即使不會發生這種情況,因爲運動傳感器應該先觸發,然後才能按下按鈕。希望這是明確的 –