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