简体   繁体   English

无限while循环python

[英]infinite while loop python

I am doing a parking sensor with raspberry pi and python我正在用raspberry pipython做一个停车传感器

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)

My problem is that the first while loop is not working, ie if the motion sensor is triggered nothing happens yet you can repress the button and the count is increased.我的问题是第一个 while 循环不起作用,即如果触发了运动传感器,则什么也没有发生,但您可以重新按下按钮并增加计数。 I'm guessing there is an easy solution to this but non seem to come to mind.我猜有一个简单的解决方案,但似乎没有想到。 Would love your help, thanks很想得到你的帮助,谢谢

Your second while loop has no break point!!!你的第二个 while 循环没有断点!!! Your first while loop runs until it detects motion and then enters the second loop, and your second loop runs forever.您的第一个 while 循环会一直运行,直到检测到运动,然后进入第二个循环,而您的第二个循环将永远运行。 If you want both loops to work simultaneously then you should use threads!!!如果您希望两个循环同时工作,那么您应该使用线程!!! If not then make a break point in second loop.如果没有,则在第二个循环中创建一个断点。

Simple example:简单的例子:

import time
count=0
count2=0
while True:    #starting first loop 
    count+=1 # counting to 100 to start second loop
    if count==100:
        print("its 100 entering second loop")
        time.sleep(1)
        while True: # entering second loop
            count2+=1 #counting to 100 to exit second loop 
            if count2==100:
                print("its 100 exitin second loop")
                time.sleep(1)
                count=0
                count2=0
                break # exiting second loop this is the break point

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM