0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

概要

プリキュアのメロディに合わせてLEDを光らせています。

電子回路

  • NPNトランジスタとベースに接続する1kΩ抵抗
  • パッシブブザー
  • ボタン
  • LEDと220Ω抵抗

上記をブレッドボードに差し込んでいます。
動画でもさらっと触れています。
詳しいピン番号はソースコードに記載されています。

ソースコード

勢いで書いたので荒ぶっていますが、参考にして下さい。

main.py
from machine import Pin, PWM
from utime import sleep

# NPNトランジスタのベース
buzzer = PWM(Pin(15))
# ボタンの入力
button = Pin(16, Pin.IN, Pin.PULL_DOWN)
# LEDのピン
red = Pin(0, Pin.OUT, value=1)
green = Pin(1, Pin.OUT, value=1)
blue = Pin(2, Pin.OUT, value=1)
yellow = Pin(3, Pin.OUT, value=1)
white = Pin(4, Pin.OUT, value=1)
count = 0
ALL = 'ALL'

# 音の周波数
REST=    0
B4  =  494
Db5 =  554
D5  = 587
E5  =  659
Gb5 =  740
G5  =  784
Ab5 =  831
A5  =  880
Bb5 =  932
B5  =  988
C6  = 1047
Db6 = 1109
D6  = 1175
Eb6 = 1245
E6  = 1319

def led_on(all=None):
    if all == 'ALL':
        red.off()
        green.off()
        blue.off()
        yellow.off()
        white.off()
    elif count == 0:
        red.off()
    elif count == 1:
        green.off()
    elif count == 2:
        blue.off()
    elif count == 3:
        yellow.off()
    elif count == 4:
        white.off()
    
def led_off():
    global count
    red.on()
    green.on()
    blue.on()
    yellow.on()
    white.on()
    if count == 4:
        count = 0
    else:
        count += 1


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

    Parameters:
        frequency(int): 周波数
        duration(float): 音の長さ
    """
    if frequency == REST:
        led_off()
        sleep(duration)
        return
    led_on(all)
    buzzer.freq(frequency)
    buzzer.duty_u16(30000)
    sleep(duration)
    buzzer.duty_u16(0)
    sleep(0.002)
    led_off()


def puricure():
    BPM = 184
    BEAT = 60/ BPM
    melody = [
        [D6, BEAT/2],[C6, BEAT/2],[B5, BEAT/2],[A5, 1.5*BEAT],[G5, BEAT],
        [D6, BEAT/2],[C6, BEAT/2],[B5, BEAT/2],[A5, 1.5*BEAT],[G5, BEAT],
        [D6, BEAT/2],[C6, BEAT/2],[B5, BEAT/2],[A5, 1.5*BEAT],[A5, BEAT/2],[B5,BEAT],
        [Gb5, BEAT],[Gb5, BEAT],[G5, BEAT/2],[G5, 2*BEAT],[REST, BEAT/2],
        [G5, BEAT/2],[C6, BEAT/2],[B5, BEAT/2],[A5, BEAT/2],[B5, 2*BEAT],
        [G5, BEAT/2],[E6, BEAT/2],[D6, BEAT/2],[C6, BEAT/2],[D6, 1.5*BEAT],
        [G5, BEAT],[A5, 1.5*BEAT],[D6, BEAT/2],[D6, 4*BEAT],
        [D6, BEAT/2],[C6, BEAT/2],[B5, BEAT/2],[A5, 1.5*BEAT],[G5, BEAT],
        [D6, BEAT/2],[C6, BEAT/2],[B5, BEAT/2],[A5, 1.5*BEAT],[G5, BEAT],
        [D6, BEAT/2],[C6, BEAT/2],[B5, BEAT/2],[A5, 1.5*BEAT],[A5, BEAT/2],[B5,BEAT/2],[REST, BEAT/2],
        [Gb5, BEAT/2],[REST, BEAT/2],[G5, 2.5*BEAT],
        [A5, BEAT],[B5,BEAT],[Db6, 1.5*BEAT],[A5, BEAT/2],[A5, 3*BEAT],
        [D5, BEAT],[D5, BEAT],[G5,BEAT/2],[A5, BEAT],[B5,BEAT/2],[C6, BEAT],[B5,3*BEAT],
        [D5, BEAT],[D5, BEAT],[G5,BEAT/2],[D6, BEAT],[C6,BEAT/2],[B5, BEAT/2],[C6,BEAT/2],[B5,BEAT/2],[A5, BEAT/2],[G5,2*BEAT],
        [G5,BEAT/2],[Gb5,BEAT/2],[G5,BEAT],[D6, BEAT/2],[C6,4*BEAT],[REST, BEAT/2],
        [D6, BEAT],[C6,BEAT],[B5,BEAT],[REST, BEAT],[D6, 3*BEAT, ALL]
    ]
    for m in melody:
        tone(m[0], m[1], m[2] if len(m) == 3 else None)


try:
    while True:
        if button.value() == 1:
            puricure()
except Exception as e:
    print(e)
    buzzer.duty_u16(0)
    

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?