Home CodePython Microbit tune selector example

Microbit tune selector example

by shedboy71

This example melds together our Microbit and an AD keypad and our Microbit built in tunes and allows you to play a different tune based on a key being pressed

We only selected 9 buttons of the 16 available

Code

[codesyntax lang=”python”]

from microbit import *
import music

while True:
    reading = pin1.read_analog()  
    sleep(100)
    if reading > 1 and reading < 39:
        music.play(music.DADADADUM)
    elif reading > 40 and reading < 94:
        music.play(music.ENTERTAINER)
    elif reading > 95 and reading < 164:
        music.play(music.PRELUDE)
    elif reading > 165 and reading < 218:
        music.play(music.RINGTONE)
    elif reading > 219 and reading < 290:
        music.play(music.FUNK)
    elif reading > 291 and reading < 354:
        music.play(music.BLUES)
    elif reading > 355 and reading < 418:
        music.play(music.BIRTHDAY)
    elif reading > 419 and reading < 485:
        music.play(music.WEDDING)
    elif reading > 486 and reading < 544:
        music.play(music.PUNCHLINE)
    else:
        num = 0

[/codesyntax]

You may also like

Leave a Comment