而真正的循环Python [英] While true loop Python

查看:57
本文介绍了而真正的循环Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用rasberry pi和python做停车传感器这是代码:

I am doing a parking sensor with rasberry pi and python this is the code :

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循环不起作用,即运动传感器触发没有任何反应你可以按下按钮,计数增加。我猜这有一个简单的解决方案,但似乎没有想到。非常感谢你的帮助,谢谢



我的尝试:



首先尝试按钮并且它工作但在我看来并不合逻辑



My problem is that the first while loop is not working, i.e if the motion sensor is triggered nothing happens yet you can repress the button and the count is increased. I'm guessing there is an easy solution to this but non seem to come to mind. Would love your help, thanks

What I have tried:

Tried to the button first and it work but was not that logical correct in my opinion

推荐答案

正如0x01AA所述:

内在一旦检测到动作,就永远不会留下循环。



更确切地说:

您的外循环正在工作并循环直到检测到运动。然后你有另一个无限的while循环,它阻止返回到外循环。



所以你必须删除内部while循环语句或使用中断 [ ^ ]声明。



你应该避免在没有中断条件的情况下使用无限循环。使用时,您只能在应用程序中使用一个,因为只能通过终止应用程序来保留它。
As already noted by 0x01AA:
The inner while loop is never left once a motion has been detected.

To be more precise:
Your outer loop is working and looping until a motion is detected. Then you have another infinite while loop which prevents returning to the outer loop.

So you have to remove the inner while loop statement or leave the loop using a break[^] statement.

You should avoid using infinite loops without break conditions. When using such, you can only use a single one within an application because it can be only left by killing the application.


这篇关于而真正的循环Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆