-
Notifications
You must be signed in to change notification settings - Fork 1
/
InputSensors.py
68 lines (60 loc) · 1.69 KB
/
InputSensors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import RPi.GPIO as GPIO
import time
#GPIO SETUP
print("Setting up gpio...")
GPIO.setmode(GPIO.BCM)
FLAME = 21
GPIO.setup(FLAME, GPIO.IN)
TRIG = 23
ECHO = 24
flame_detect = False
pot_detect = False
GPIO.add_event_detect(FLAME, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW
def dist_sensor():
global pot_detect
while pot_detect == False:
print ("Distance Measurement In Progress")
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG,False)
print ("Wait For Sensor To Settle")
time.sleep(2)
GPIO.output(TRIG,True)
time.sleep(0.00001)
GPIO.output(TRIG,False)
while GPIO.input(ECHO)==0:
start=time.time()
while GPIO.input(ECHO)==1:
end=time.time()
duration= end-start
dist = round(duration*17150,2)
print ("Distance is: ",dist,"cm")
if dist <= 5:
pot_detect = True
def callback(FLAME):
global flame_detect
if GPIO.input(FLAME): #if pin is HIGH
print("Flame detected")
flame_detect = True
else:
print("No flame detected")
flame_detect = False
GPIO.add_event_callback(FLAME, callback) # assign function to GPIO PIN, Run function on change
def check_input():
dist_sensor()
print ("Flame is now",flame_detect)
print ("Pot is now",pot_detect)
if pot_detect == True and flame_detect == True:
print ("Flame and Pot detected!")
cooking = True
else:
cooking = False
return cooking
# infinite loop
while True:
cooking = check_input()
if cooking == True:
GPIO.cleanup()
break
else:
time.sleep(1)