-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram1.py
More file actions
37 lines (28 loc) · 1.01 KB
/
program1.py
File metadata and controls
37 lines (28 loc) · 1.01 KB
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
import time
import RPi.GPIO as GPIO #type: ignore
from datetime import datetime
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
def main():
# My design uses pin 12 as input and pin 11 as output.
INPUT_PIN = 12
LED_PIN = 11
RUNTIME = 10 # Run for 10 seconds
GPIO.setup(INPUT_PIN, GPIO.IN) # sets pin as input
GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.LOW) # sets pin as output
start_time = time.time()
try:
while (time.time() - start_time) < RUNTIME:
input_state = GPIO.input(INPUT_PIN) # returns True if input high
if input_state == GPIO.HIGH:
GPIO.output(LED_PIN, GPIO.HIGH) # Turn LED on
print("led on")
else:
GPIO.output(LED_PIN, GPIO.LOW) # Turn LED off
print("led off")
time.sleep(1) # Small delay to prevent CPU overload
except KeyboardInterrupt:
print("Exiting...")
finally:
GPIO.cleanup()
main()