Issue
Good day,
I have a script which monitors the input from 2 motion sensors which will turn on a light should motion from either of these 2 sensors be detected (Raspberry Pi 4). After the while loop I have more code that needs to be executed, however, python does not move/continue past the while loop.
Here is the code:
from datetime import datetime
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
rightMotionActivity = 3
leftMotionActivity = 5
flashLightControl = 7
GPIO.setup(flashLightControl, GPIO.OUT)
GPIO.setup(leftMotionActivity, GPIO.IN)
GPIO.setup(rightMotionActivity, GPIO.IN)
while True:
leftMotionDetect = GPIO.input(5)
rightMotionDetect = GPIO.input(3)
# If both motion sensors are 0
if leftMotionDetect==0 and rightMotionDetect==0:
GPIO.output(flashLightControl, GPIO.LOW)
time.sleep(0.1)
# If either of the motion sensors are 1
elif leftMotionDetect==1 or rightMotionDetect==1:
GPIO.output(flashLightControl, GPIO.HIGH)
time.sleep(0.1)
# Rest of the code follows but python does not get here
Solution
Because of the while True
the while loop will never stop running. You need to implement a manner to get out of the while loop.
Something like this:
loopReady = True
while loopReady
if(sensor1 and sensor2)
loopReady = false
#rest of the code
if sensor1 and sensor 2 are 'True', loopReady will become false and the while loop exits
Answered By - KlopDesign Answer Checked By - Cary Denson (WPSolving Admin)