-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram2.py
More file actions
50 lines (35 loc) · 1.11 KB
/
program2.py
File metadata and controls
50 lines (35 loc) · 1.11 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
38
39
40
41
42
43
44
45
46
47
48
49
50
# Program 2
# My design uses pin 12 as input and pin 11 as output.
# When 0 switch off it'll blink at 2 second intervals and blink at 1 second intervals when 1.
import RPi.GPIO as GPIO # type: ignore
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
LED_PIN = 11
SWITCH_PIN = 12
GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(SWITCH_PIN, GPIO.IN) # This is external 330Ω pull out
start = time.time()
led_state = False
# Open log file once in append mode
f = open("data.txt", "a")
# This 10 second loop makes the LED blink correctly.
while (time.time() - start) < 10:
switch_state = GPIO.input(SWITCH_PIN)
if switch_state == 0:
interval = 2
switch_str = "off"
else:
interval = 1
switch_str = "on"
led_state = not led_state
GPIO.output(LED_PIN, led_state)
time.sleep(interval)
GPIO.output(LED_PIN, GPIO.LOW) # Makes sure LED is off
# Log in a txt file
elapsed = time.time() - start
elapsed_ms = round(elapsed, 3)
f.write(f"{elapsed_ms:.3f}\t{switch_str}\n")
f.flush()
# Close log file after loop
f.close()