Toggle Navigation
Hatchery
Eggs
jeopardy theme
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import utime import vibra """ F4 = 349 G4 = 392 A4 = 440 B4 = 494 C5 = 523 D5 = 587 D5_flat = 523 E5 = 659 F5 = 698 G5 = 784 A5 = 880 F_E_D_Flat_D = -1 pause = -2 # 4/4 tact, note [frequency, -1="F-E-D-Flat-D-part", -2=pause] melody = (((A4+C5)//2, 4), (F4, 4), (C5, 4), (F4, 4), ((B4+C5)//2, 4), (F5, 4), ((B4+C5)//2, 2), ((A4+C5)//2, 4), (F5, 4), (C5, 4), (F4, 4), (B4, 4), (A4, 4), (G4, 4), (pause, 4), ((A4+C5)//2, 4), (F5, 4), (C5, 4), (F4, 4), ((B4+C5)//2, 4), (F5, 4), ((B4+C5)//2, 2), ((A4+C5)//2, 4), (F5, 4), (C5, 4), (F5, 4), (A5, 6), (G5, 8), (F_E_D_Flat_D, 4), ((A4+C5)//2, 4), (F5, 4), (C5, 4), (F4, 4)) """ melody = ((481, 4), (349, 4), (523, 4), (349, 4), (508, 4), (698, 4), (508, 2), (481, 4), (698, 4), (523, 4), (349, 4), (494, 4), (440, 4), (392, 4), (-2, 4), (481, 4), (698, 4), (523,4), (349, 4), (508, 4), (698, 4), (508, 2), (481, 4), (698, 4), (523, 4), (698, 4), (880, 6), (784, 8), (-1, 4), (481, 4), (698, 4), (523, 4), (349, 4)) def play_tone(freq, dur, mul=1): freq = freq // 4 wave_length = 50000 // freq - 50 repeat = freq * 32*mul // dur for i in range(repeat): vibra.set(True) utime.sleep_us(1) vibra.set(False) utime.sleep_us(wave_length) def play_pause(dur): wave_length = 50000 repeat = 32 // dur utime.sleep_us(wave_length * repeat) def play_one_to_another(start_freq, end_freq, dur): start_freq = start_freq // 4 end_freq = end_freq // 4 repeat = ((start_freq + end_freq) // 2) * 8 // dur wavelength = 50000 // start_freq - 50 step = repeat // (wavelength - 50000 // end_freq - 50) for i in range(repeat): vibra.set(True) utime.sleep_us(1) vibra.set(False) utime.sleep_us(wavelength) wavelength += step def play_F_E_D_Flat_D(): # for f1, f2 in ((F5, E5), (E5, D5), (D5, D5_flat)): for f1, f2 in ((698, 659), (659, 587), (587, 523)): play_tone(f1, 4, 0.875) play_one_to_another(f1, f2, 2) # play_tone(D5_flat, 4, 0.75) play_tone(523, 4, 0.875) # -------------------------------------------------------------- main note_count = 0 while True: frequency = melody[note_count][0] # note duration = melody[note_count][1] # duration if frequency >= 0: print('d:%i f:%i' % (duration, frequency)) play_tone(frequency, duration) elif frequency == -1: play_F_E_D_Flat_D() else: play_pause(duration) utime.sleep_ms(50) note_count += 1 if note_count == 33: utime.sleep(3) note_count = 0