-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9_tilt_3_alarm.py
51 lines (35 loc) · 1.19 KB
/
day9_tilt_3_alarm.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
# Imports
from machine import Pin
import time
import tone
# Set up tilt sensor pin
tilt = Pin(26, Pin.IN, Pin.PULL_DOWN)
# Set up a counter variable at zero
tiltcount = 0
# Create a state variable at zero
state = 0
volume = 1000
def alarm():
notes = [
(tone.G, 0.3, 0.3),
(tone.A2, 0.2, 0.3),
(tone.D, 0.2, 0.3),
(tone.A2, 0.3, 0.3),
(tone.B2, 0.4, 0.3),
(tone.D2, 0.1, 0.1),
(tone.C2, 0.1, 0.1),
(tone.B2, 0.1, 0.1),
(tone.G, 0.2, 0.3),
]
for note, delay1, delay2 in notes:
tone.play(note, volume, delay1, delay2)
while True: # Run forever
time.sleep(0.1) # Short delay
if state == 0 and tilt.value() == 1: # If state is 0 and our pin is HIGH
tiltcount = tiltcount + 1 # Add +1 to tiltcount
state = 1 # Change state to 1
print("tilts =",tiltcount) # Print our new tiltcount
if tiltcount == 3:
alarm()
if state == 1 and tilt.value() == 0: # If state is 1 and our pin is LOW
state = 0 # Change the state to 0