'''
ESP32 with Buzzer hanukka Song
Based on arduino C++ project and code by Dipto Pratyaksa, updated on 31/3/13
Modified for micropython hanukka by Ofer Dayan , on Dec. 9th, 2024.
'''
from machine import Pin
from time import sleep, sleep_ms, sleep_us
# notes names and their frequencies
PAUSE = 0
SI_0 = 31
DO_1 = 33
DO_D1 = 35
RE_1 = 37
RE_D1 = 39
MI_1 = 41
FA_1 = 44
FA_D1 = 46
SOL_1 = 49
SOL_D1 = 52
LA_1 = 55
LA_D1 = 58
SI_1 = 62
DO_2 = 65
DO_D2 = 69
RE_2 = 73
RE_D2 = 78
MI_2 = 82
FA_2 = 87
FA_D2 = 93
SOL_2 = 98
SOL_D2 = 104
LA_2 = 110
LA_D2 = 117
SI_2 = 123
DO_3 = 131
DO_D3 = 139
RE_3 = 147
RE_D3 = 156
MI_3 = 165
FA_3 = 175
FA_D3 = 185
SOL_3 = 196
SOL_D3 = 208
LA_3 = 220
LA_D3 = 233
SI_3 = 247
DO_4 = 262
DO_D4 = 277
RE_4 = 294
RE_D4 = 311
MI_4 = 330
FA_4 = 349
FA_D4 = 370
SOL_4 = 392
SOL_D4 = 415
LA_4 = 440
LA_D4 = 466
SI_4 = 494
DO_5 = 523
DO_D5 = 554
RE_5 = 587
RE_D5 = 622
MI_5 = 659
FA_5 = 698
FA_D5 = 740
SOL_5 = 784
SOL_D5 = 831
LA_5 = 880
LA_D5 = 932
SI_5 = 988
DO_6 = 1047
DO_D6 = 1109
RE_6 = 1175
RE_D6 = 1245
MI_6 = 1319
FA_6 = 1397
FA_D6 = 1480
SOL_6 = 1568
SOL_D6 = 1661
LA_6 = 1760
LA_D6 = 1865
SI_6 = 1976
DO_7 = 2093
DO_D7 = 2217
RE_7 = 2349
RE_D7 = 2489
MI_7 = 2637
FA_7 = 2794
FA_D7 = 2960
SOL_7 = 3136
SOL_D7 = 3322
LA_7 = 3520
LA_D7 = 3729
SI_7 = 3951
maoz_tzur_melody = [
DO_4, SOL_3, DO_4, FA_4, MI_4, RE_4, DO_4, SOL_4,
SOL_4, LA_4, RE_4, MI_4, FA_4, MI_4, RE_4, DO_4, DO_4,
DO_4, SOL_3, DO_4, FA_4, MI_4, RE_4, DO_4, SOL_4,
SOL_4, LA_4, RE_4, MI_4, FA_4, MI_4, RE_4, DO_4,
SOL_4, SOL_4, LA_4, SI_4, DO_5, SOL_4,
DO_5, SI_4, LA_4, SOL_4, SOL_4, FA_4, MI_4, FA_4, RE_4,
MI_4, FA_4, SOL_4, LA_4, RE_4, MI_4, FA_4,
MI_4, DO_4, LA_4, SOL_4, FA_4, MI_4, FA_4, SOL_4,
MI_4, FA_4, SOL_4, LA_4, RE_4, MI_4, FA_4,
MI_4, DO_4, LA_4, SOL_4, FA_4, MI_4, RE_4, DO_4]
maoz_tzur_tempo = [
4, 4, 4, 4, 4, 4, 3, 8,
4, 4, 4, 8, 8, 4, 4, 3, 8,
4, 4, 4, 4, 4, 4, 3, 8,
4, 4, 4, 8, 8, 4, 4, 2,
3, 8, 4, 4, 2, 2,
4, 4, 4, 4, 8, 8, 8, 8, 2,
3, 8, 3, 8, 3, 8, 2,
4, 4, 4, 8, 8, 4, 4, 2,
3, 8, 3, 8, 3, 8, 2,
4, 4, 4, 8, 8, 4, 4, 2]
hanerot_halalu_melody = [\
MI_4, MI_4, MI_4, MI_4, MI_4, MI_4, MI_4,
MI_4, RE_4, DO_4, RE_4, MI_4, DO_4, SOL_4,
SOL_4, SOL_4, SOL_4, SOL_4, SOL_4, SOL_4,
SOL_4, FA_4, MI_4, FA_4, SOL_4, MI_4,
SOL_4, DO_5, MI_4, FA_4, SOL_4, SOL_4,
SOL_4, DO_5, MI_4, FA_4, SOL_4,
LA_4, SOL_4, FA_4, MI_4, RE_4, SOL_4,
MI_4, MI_4, RE_4, RE_4, DO_4,
MI_4, MI_4, MI_4, MI_4,
MI_4, RE_4, DO_4, RE_4, MI_4, DO_4,
SOL_4, SOL_4, SOL_4, SOL_4,
SOL_4, FA_4, MI_4, FA_4, SOL_4, MI_4,
SOL_4, DO_5, MI_4, FA_4, SOL_4,
SOL_4, DO_5, MI_4, FA_4, SOL_4,
LA_4, SOL_4, FA_4, MI_4, RE_4, SOL_4,
MI_4, RE_4, DO_4]
hanerot_halalu_tempo = [\
8, 4, 6, 16, 4, 6, 16,
8, 8, 8, 8, 4, 8, 8,
4, 6, 16, 4, 6, 16,
8, 8, 8, 8, 4, 4,
8, 8, 8, 8, 3, 8,
8, 8, 8, 8, 2,
8, 8, 8, 8, 4, 4,
8, 8, 8, 8, 2,
4, 4, 4, 4,
8, 8, 8, 8, 4, 4,
4, 4, 4, 4,
8, 8, 8, 8, 4, 4,
8, 8, 8, 8, 2,
8, 8, 8, 8, 2,
8, 8, 8, 8, 4, 4,
4, 4, 3]
def buzz(target_pin, frequency, note_length):
if frequency != 0:
delay_value = int(1000000 / frequency / 2) # calculate the delay value between transitions
# 1 second's worth of microseconds, divided by the frequency, then split in half since
# there are two phases to each cycle
num_cycles = int(frequency * note_length / 1000) # calculate the number of cycles for proper timing
# multiply frequency, which is really cycles per second, by the number of seconds to
# get the total number of cycles to produce
for i in range(num_cycles): # for the calculated length of time...
target_pin.value(1) # write the buzzer pin high to push out the diaphram
sleep_us(delay_value) # wait for the calculated delay value
target_pin.value(0) # write the buzzer pin low to pull back the diaphram
sleep_us(delay_value) #wait again for the calculated delay value
else: # if pause
delay_value = 1000 // note_length
sleep_us(delay_value) # wait for the calculated delay value
def sing(melody, tempo):
# iterate over the notes of the melody:
for this_note in range(len(melody)):
# to calculate the note duration, take one second
# divided by the note type.
# e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
note_duration = 1000 // tempo[this_note]
buzz(buzzer_pin, melody[this_note], note_duration)
# to distinguish the notes, set a minimum time between them.
# the note's duration + 30% seems to work well:
pause_between_notes = int(note_duration * 1.3)
sleep_ms(pause_between_notes)
# stop the tone playing:
buzz(buzzer_pin, 0, note_duration)
buzzer_pin = Pin(2, Pin.OUT)
while True:
sing(hanerot_halalu_melody,hanerot_halalu_tempo)
sleep(1)
sing(maoz_tzur_melody,maoz_tzur_tempo)
sleep(1)