Mercurial > espworkshop
changeset 23:a47fafb6e1e5
Add melody example
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Sat, 18 Jun 2016 13:25:24 +0200 |
| parents | 379eeec3777a |
| children | 9740ae97a3a9 |
| files | basics.rst |
| diffstat | 1 files changed, 30 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/basics.rst Fri Jun 17 12:19:45 2016 +0200 +++ b/basics.rst Sat Jun 18 13:25:24 2016 +0200 @@ -311,9 +311,36 @@ time.sleep(0.5) beeper.deinit() -Unfortunately, we can't really play any melodies, as the possible frequencies -currently don't cover the musical notes. We can make noises, and that's pretty -much it. We can play music and other sounds, but not with the PWM. +We can even play melodies! For instance, here's the musical scale:: + + from machine import Pin, PWM + import time + + tempo = 5 + tones = { + 'c': 262, + 'd': 294, + 'e': 330, + 'f': 349, + 'g': 392, + 'a': 440, + 'b': 494, + 'C': 523, + ' ': 0, + } + beeper = PWM(Pin(2, Pin.OUT)) + melody = 'cdefgabC' + rhythm = [8, 8, 8, 8, 8, 8, 8, 8] + + for tone, length in zip(melody, rhythm): + beeper.freq(tones[tone]) + time.sleep(tempo/length) + +Unfortunately, the maximum frequency of PWM is currently 1000Hz, so you can't +play any notes higher than that. + +It's possible to make the sounds louder by using a better speaker and possibly +an audio amplifier. Network
