Last active 1700878528

https://blahaj.zone/notes/9mehzr6pjs5zsfs8

Revision e4f874014183e46d378d89ad1292ccf71b35126d

main.py Raw
1import machine
2import utime
3
4notes = {
5 "B3": 247,
6 "E4": 330,
7 "FS4": 370,
8 "GS4": 415,
9 "A4": 440,
10 "D7": 2349,
11}
12
13green = machine.Pin(21, machine.Pin.OUT)
14green.value(0)
15
16buzzer = machine.PWM(machine.Pin(22))
17
18rtc=machine.RTC()
19timestamp=rtc.datetime()
20#file = open("test","a")
21#file.write("%04d-%02d-%02d %02d:%02d:%02d.%04d\n"%(timestamp[0:3] + timestamp[4:8]))
22#file.close()
23
24minute = [
25 ("D7", 0.1)
26]
27hour = [
28 ("GS4", 0.4),
29 ("E4", 0.4),
30 ("FS4", 0.4),
31 ("B3", 0.4),
32 ("", 0.4),
33 ("", 0.4),
34 ("B3", 0.4),
35 ("FS4", 0.4),
36 ("GS4", 0.4),
37 ("E4", 0.4)
38]
39
40while True:
41 timestamp=rtc.datetime()
42# print("%04d-%02d-%02d %02d:%02d:%02d.%04d"%(timestamp[0:3] + timestamp[4:8]))
43 song = []
44 # internal RTC starts on 2021-01-01 00:00:01.0000
45 if timestamp[6]-1 == 0:
46 if timestamp[5] == 0:
47 song = hour
48 else:
49 song = minute
50 for note,length in song:
51 if length > 0.5:
52 raise Exception
53 green.value(1)
54 if note:
55 buzzer.freq(notes[note])
56 buzzer.duty_u16(1000)
57 utime.sleep(length)
58 green.value(0)
59 if note:
60 buzzer.duty_u16(0)
61 utime.sleep(0.5-length)
62 if len(song)%2 != 0:
63 utime.sleep(0.5)
64 else:
65 utime.sleep(1)