Can I use variables from inside of "while" in this "alternative"?
Thank you!
Code: Select all
while condition:
statements
Code: Select all
statements
I'm trying to send an email everytime an LED lights up.joan wrote:Basically a while loop isIf you don't want to loop it just becomesCode: Select all
while condition: statementsWhat are you trying to achieve?Code: Select all
statements
Code: Select all
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_PIR = 7 # Pin 26
GPIO_LED = 4 # Pin 7
GPIO.setup(GPIO_LED,GPIO.OUT)
GPIO.setup(GPIO_PIR,GPIO.IN)
print " Press Ctrl & C to Quit"
try:
while GPIO.input(GPIO_PIR)==1:
Motion = 0
print " Ready"
Motion = 0
while True:
if GPIO.input(GPIO_PIR)==1:
if Motion == 0:
print " Motion detected!"
Motion = 1
GPIO.output(GPIO_LED, True)
else:
if Motion == 1:
print " Clear "
Motion = 0
GPIO.output(GPIO_LED, False)
import smtplib
content = 'example stuff'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login('EMAIL','PASSWORD')
mail.sendmail('EMAIL','RECEIVER_EMAIL',content)
mail.close()
except KeyboardInterrupt:
print " Quit"
GPIO.cleanup()
Okay, I'll try that. My original understanding was that "while" loops.kusti8 wrote:All you need to do is put another while in the while loop which waits until the led turns off, and then continues.
Sorry, but could you show me exactly where to put it?kusti8 wrote:All you need to do is put another while in the while loop which waits until the led turns off, and then continues.
Code: Select all
last_state=false
while true:
new_state=GPIO input
if new_state and not last_state:
send email
last_state=new_state