【问题标题】:infinite while loop python无限while循环python
【发布时间】:2017-10-12 18:55:56
【问题描述】:

我正在用raspberry pipython 做一个停车传感器

这是代码:

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 循环不起作用”是什么意思?
  • 因为我有两个 while 循环,循环整个程序的那个不是无限循环。(即使这是它应该做的)。这意味着当运动传感器触发任何类型的运动时,什么都不会发生。是的,控制计数器和按钮的第二个 while 循环工作正常。因此,当您按下按钮时,即使这不会发生,计数器也会增加,因为应该首先触发运动传感器,然后才能按下按钮。希望这很清楚

标签: python while-loop infinite-loop


【解决方案1】:

你的第二个 while 循环没有断点!!! 您的第一个 while 循环一直运行,直到它检测到运动,然后进入第二个循环,而您的第二个循环将永远运行。 如果您希望两个循环同时工作,那么您应该使用线程!!! 如果没有,则在第二个循环中设置一个断点。

简单示例:

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

【讨论】:

  • 您能否详细解释一下断点是什么意思?
  • 你必须有一些东西才能退出第二个循环才能回到第一个循环一个简单的中断命令——添加了一个例子
猜你喜欢
  • 2020-11-30
  • 2015-05-22
  • 2020-08-18
  • 2012-08-28
  • 2015-06-25
  • 2018-08-30
  • 1970-01-01
  • 2014-03-29
  • 2016-08-27
相关资源
最近更新 更多