from machine import Pin
from time import sleep, sleep_ms, sleep_us
# notes names and their frequencies
PAUSE = 0
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
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(4, Pin.OUT)
button = Pin(17,Pin.OUT,Pin.PULL_DOWN)
ner1 = Pin(2,Pin.OUT)
ner2 = Pin(13,Pin.OUT)
ner3 = Pin(12,Pin.OUT)
ner4 = Pin(14,Pin.OUT)
ner5 = Pin(27,Pin.OUT)
ner6 = Pin(26,Pin.OUT)
ner7 = Pin(25,Pin.OUT)
ner8 = Pin(33,Pin.OUT)
shmsh = Pin(16,Pin.OUT)
candles = [ner1, ner2, ner3, ner4, ner5, ner6, ner7, ner8, shmsh]
for candle in candles:
candle.value(0)
day = 1 # to follow the Hanukka days
state = 1 # lamp state: 1 - to light on, 0 to light off
while True:
# wait till button is pressed
while button.value() == 0:
pass
# wait till button bouncing is over
sleep_us(100)
if state == 0: #tune off the candles
for candle in candles:
candle.value(0)
sleep_ms(20)
state = 1
else: #tune on the Hanukkah
# first the Shamash
shmsh.value(1)
sleep(1)
# then the rest of the candels (by Halacha order)
for i in range(day ,0 ,-1):
print("candle",i)
candles[i-1].value(1)
sleep(0.2)
state = 0
if day == 8:
sing(hanerot_halalu_melody,hanerot_halalu_tempo)
sleep(1)
sing(maoz_tzur_melody,maoz_tzur_tempo)
sleep(1)
day = day % 8 + 1
while button.value() == 1:
pass
sleep_us(100)