Tottillo Healthkits
Published © GPL3+

Star Wars Theme Music Generator

How to make your micro:bit work as a touch-activated music generator, that plays Star Wars theme.

BeginnerFull instructions provided1 hour4,148
Star Wars Theme Music Generator

Things used in this project

Story

Read more

Schematics

Circuit diagram for music playing micro:bit

Code

Code for music playing Micro:bit

Python
from microbit import *
import time

def beep (pin, noteFrequency, noteDuration, sleepDuration = 100):
  microsecondsPerWave = 1e6/noteFrequency
  millisecondsPerCycle = 1000/(microsecondsPerWave * 2)
  loopTime = noteDuration * millisecondsPerCycle
  for x in range(loopTime):
    pin.write_digital(1)
    time.sleep_us(int(microsecondsPerWave))
    pin.write_digital(0)
    time.sleep_us(int(microsecondsPerWave))
  sleep(sleepDuration)

def BuzzerStarWars(pin):
  SW_NOTES = [293.66, 293.66, 293.66, 392.0, 622.25, 554.37, 523.25, 454, 932.32, 622.25, 554.37, 523.25, 454, 932.32, 622.25, 554.37, 523.25, 554.37, 454]
  SW_DURATION = [180, 180, 180, 800, 800, 180, 180, 180, 800, 400, 180, 180, 180, 800, 400, 180, 180, 180, 1000]
  SW_SLEEP = [40, 40, 40, 100, 100, 40, 40, 40, 100, 50, 40, 40, 40, 100, 50, 40, 40, 40, 100]
  for i in range(len(SW_NOTES)):
    beep(pin, SW_NOTES[i], SW_DURATION[i], SW_SLEEP[i])

def BuzzerGamme(pin):
  G_NOTES = [261.63, 293.66, 329.54, 349.23, 392, 440, 493.88, 523.25]
  for i in range(len(G_NOTES)):
    beep(pin, G_NOTES[i], 250, 50)

def BuzzerR2D2(pin):
  R2D2_NOTES = [3520, 3135.96, 2637.02, 2093, 2349.32, 3951.07, 2793.83, 4186.01, 3520, 3135.96, 2637.02, 2093, 2349.32, 3951.07, 2793.83, 4186.01]
  for i in range(len(R2D2_NOTES)):
    beep(pin, R2D2_NOTES[i], 80, 20)


while True:
  if pin0.is_touched():
    BuzzerStarWars(pin0)
  if pin1.is_touched():
    BuzzerGamme(pin1)
  if pin2.is_touched():
    BuzzerR2D2(pin2)

Credits

Tottillo Healthkits

Tottillo Healthkits

21 projects • 91 followers
I am e-health engineer Diana. Interested in general wellness and awareness of what parameters affect our well-being and health.

Comments