【メロディーメイカー】クラクラ

# 概要

子ども向けのプログラミング教育プロジェクトの一環として、Adoのクラクラをコーディングしてみました。

<iframe width="400" height="712" src="https://www.youtube.com/embed/QmIxro0qBrc" title="電子工作でクラクラ" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>

# ソースコード

```python:main.py
from machine import Pin, PWM
from utime import sleep

# 演奏中ならTrue, 演奏していない時はFalse
is_playing = False

# スケール（melodyの中で使っている音階）
scale = []

# BPM
bpm = 60

# LED
led_pins = [0, 2, 3, 9, 10, 1, 11, 12]
leds = [Pin(pin, Pin.OUT, value=1) for pin in led_pins]

# NPNトランジスタのベース
buzzer = PWM(Pin(15))

# 再生ボタン
playing_button = Pin(13, Pin.IN, Pin.PULL_DOWN)

# 停止ボタン
stop_button = Pin(14, Pin.IN, Pin.PULL_DOWN)

def stop(pin):
    global is_playing
    is_playing = False

stop_button.irq(trigger=Pin.IRQ_RISING, handler=stop)

# 音階周波数
REST=    0
C3, Db3, D3, Eb3, E3, F3, Gb3, G3, Ab3, A3, Bb3, B3 = 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247
C4, Db4, D4, Eb4, E4, F4, Gb4, G4, Ab4, A4, Bb4, B4 = 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494
C5, Db5, D5, Eb5, E5, F5, Gb5, G5, Ab5, A5, Bb5, B5 = 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988
C6, Db6, D6, Eb6, E6, F6, Gb6, G6, Ab6, A6, Bb6, B6 =1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976


def turn_on_led(index=None):
    """
    LEDを点灯する。
    """
    if index is None:
        for led in leds:
            led.value(0)
    else:
        leds[index].value(0)
        
def turn_off_led(index=None):
    """
    LEDを消灯する。
    """
    if index is None:
        for led in leds:
            led.value(1)
    else:
        leds[index].value(1)

def tone(frequency, duration):
    """
    パッシブブザーで音を鳴らす。

    Parameters:
        frequency(int): 周波数
        duration(int): 音符の音の長さ（四分音符なら4、八分音符なら8）
    """
    beat = 60 / bpm
    duration = beat * 4 / duration

    if frequency == REST:
        turn_on_led()
        sleep(duration)
        turn_off_led()
        return
    index = scale.index(frequency) % 8
    turn_on_led(index)
    buzzer.freq(frequency)
    buzzer.duty_u16(30000)
    sleep(duration)
    buzzer.duty_u16(0)
    sleep(0.002)
    turn_off_led(index)


def kurakura():
    """
    クラクラ
    """
    global is_playing
    global bpm
    global scale

    # BPMを更新する
    bpm = 188

    # メロディーを定義する
    melody = [
        [G5,8],[Ab5,8],[Bb5,4],[G5,8],[Ab5,8],[Bb5,4],[G5,8],[Ab5,8],[Bb5,4],[Ab5,4],[G5,8],[F5,8],[Eb5,8],[REST,8],
        [Bb5,4],[G5,8],[Ab5,8],[Bb5,4],[G5,8],[Ab5,8],[Bb5,4],[Ab5,4],[G5,8],[F5,8],[Eb5,8],[REST,8],
        [C5,4],[D5,8],[Eb5,8],[F5,4],[G5,8],[Ab5,8],[Bb5,4],[G5,8],[F5,8],[Eb5,8],[REST,8],
        [Eb5,8],[F5,8],[G5,4],[Ab5,8],[G5,8],[Ab5,8],[G5,8],[F5,8],[Eb5,8],[F5,4/2.5],[REST,8],
        [G5,8],[Ab5,8],[Bb5,4],[G5,8],[Ab5,8],[Bb5,4],[G5,8],[Ab5,8],[Bb5,4],[Ab5,4],[G5,8],[F5,8],[Eb5,8],[REST,8],
        [Bb5,4],[G5,8],[Bb5,4],[Bb5,4],[Bb5,8],[G5,8],[Bb5,8],[C6,8],[D6,8],[Eb6,4],
        [Eb5,4],[Eb6,4],[D6,8],[C6,8],[Bb5,8],[REST,8],
        [Eb5,8],[Eb5,8],[C6,4],[Bb5,8],[Ab5,8],[Bb5,8],[REST,8],
        [Eb5,8],[Eb5,8],[Bb5,4],[Ab5,8],[Gb5,8],[F5,8],[REST,8],[Db5,4],[Eb5,4/1.5],[Bb3,8],[Eb4,8],[F4,8],
        [C6,8],[D6,8],[Eb6,4],[D6,8],[C6,8],[Bb5,8],[REST,8],
        [Eb5,8],[Eb5,8],[C6,4],[Bb5,8],[Ab5,8],[Bb5,8],[REST,8],
        [Eb5,8],[Eb5,8],[Bb5,4],[Ab5,8],[Gb5,8],[F5,8],[REST,8],
        [Db5,4],[Eb5,4],[Bb5,8],[Db5,4],[Eb5,4],[REST,8],
        [C5,4],[C5,8],[Bb4,4],[C5,4],[REST,4]

    ] 

    # スケールを更新する
    unique_elements = []
    for note in melody:
        if note[0] not in unique_elements and note[0] != REST:
            unique_elements.append(note[0])
    scale = sorted(unique_elements)

    # 演奏を開始する
    is_playing = True
    turn_off_led()
    for m in melody:
        if is_playing:
            tone(m[0], m[1])
        else:
            break

index = 0
while True:
    turn_on_led(index)
    if playing_button.value() == 1:
        kurakura()
    sleep(0.1)
    turn_off_led(index)
    if index < 7:
        index += 1
    else:
        index = 0

```

# メロディーメイカーの作り方

動画で作り方を解説しています。

https://youtu.be/d7pTfG6R1M8