-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram3.py
More file actions
51 lines (34 loc) · 1.15 KB
/
program3.py
File metadata and controls
51 lines (34 loc) · 1.15 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
51
# Program 3
# 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 is me 10 second loop
while (time.time() - start) < 10:
switch_state = GPIO.input(SWITCH_PIN)
if switch_state == 0:
interval = 2
switch_str = "off"
GPIO.output(LED_PIN, GPIO.LOW) # Force LED off
led_state = False # Reset state so blink restarts cleanly
else:
interval = 1
switch_str = "on"
led_state = not led_state # Toggle only when ON
GPIO.output(LED_PIN, led_state)
time.sleep(interval)
# Log
elapsed = time.time() - start
elapsed_ms = round(elapsed, 3)
f.write(f"{elapsed_ms:.3f}\t{switch_str}\n")
f.flush()