Added a tutorial for the play sound block (#4706)
This commit is contained in:
Родитель
66f7c9607b
Коммит
c62ebd8725
|
@ -0,0 +1,144 @@
|
|||
# Dance to the Beat
|
||||
|
||||
## 1. Introduction @unplugged
|
||||
|
||||
The new micro:bits have speakers, which leaves you free to move around in ways you weren't able to before!
|
||||
|
||||
Let's use movement to create a beat box of your own.
|
||||
|
||||
![Dance beat banner message](/static/mb/projects/dance-beat.png)
|
||||
|
||||
|
||||
## 2. Understanding Input
|
||||
|
||||
Let's find out what numbers the micro:bit produces when you move it around.
|
||||
|
||||
---
|
||||
|
||||
⇼ Open the ``||serial:^ Advanced||`` category to show the ``||serial:Serial||`` label.
|
||||
|
||||
⇼ From ``||serial:Serial||``, drag the ``||serial:serial write value ["x"] = [0]||``
|
||||
block into the ``||basic:forever||`` loop container.
|
||||
|
||||
|
||||
|
||||
```blocks
|
||||
basic.forever(function(){
|
||||
serial.writeValue("x", 0)
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 3. See the Console
|
||||
|
||||
When your code runs again, you'll see a button below the micro:bit that says
|
||||
"Show console Simulator".
|
||||
|
||||
Click that button to see what happens.
|
||||
|
||||
---
|
||||
|
||||
The graph for the simulator should stay flat at 0
|
||||
and the text in the console below should say "x:0".
|
||||
|
||||
|
||||
|
||||
## 4. Acceleration Values
|
||||
|
||||
For the graph to change with the speed of your movement, we need to replace the "0"
|
||||
with the micro:bit **acceleration** value.
|
||||
|
||||
---
|
||||
|
||||
⇼ Open the ``||input:Input||`` category and drag ``||input:acceleration (mg) [x]||``
|
||||
over to replace "0" in the ``||serial:serial write value ["x"] = [0]||``
|
||||
block.
|
||||
|
||||
⇼ Change "x" to "a" (for "acceleration".)
|
||||
|
||||
|
||||
|
||||
```blocks
|
||||
basic.forever(function(){
|
||||
serial.writeValue("a", input.acceleration(Dimension.X))
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 5. Look Again
|
||||
|
||||
Click the "Show console Simulator" button again.
|
||||
|
||||
---
|
||||
|
||||
Now you should see your graph and text change between **-1023** and **1023** as you click
|
||||
around on the micro:bit simulator to pretend like you're swinging it around.
|
||||
|
||||
|
||||
|
||||
## 6. Compass Values
|
||||
|
||||
For the graph to change with the speed of your movement, we need to replace the "0" with
|
||||
the micro:bit **acceleration** value.
|
||||
|
||||
---
|
||||
|
||||
⇼ Open the ``||input:Input||`` category and drag ``||input:acceleration (mg) [x]||``
|
||||
over to replace "0" in the ``||serial:serial write value ["x"] = [0]||``
|
||||
block.
|
||||
|
||||
⇼ Change "x" to "a" (for "acceleration".)
|
||||
|
||||
|
||||
|
||||
```blocks
|
||||
basic.forever(function(){
|
||||
serial.writeValue("a", input.acceleration(Dimension.X))
|
||||
})
|
||||
```
|
||||
|
||||
## Finale
|
||||
|
||||
👏 **YOU DID IT!** 👏
|
||||
|
||||
Don't forget to test your code in the simulator!
|
||||
|
||||
If you have a new @boardname@ (the one with the **shiny gold** logo at the top), download this code and try it out!
|
||||
|
||||
```blocks
|
||||
basic.forever(function () {
|
||||
serial.writeValue("Accel", input.acceleration(Dimension.X))
|
||||
serial.writeValue("Compass", input.compassHeading())
|
||||
music.playSoundEffect(music.createSoundEffect(
|
||||
WaveShape.Sine,
|
||||
input.acceleration(Dimension.X),
|
||||
input.compassHeading(),
|
||||
255,
|
||||
0,
|
||||
500,
|
||||
SoundExpressionEffect.None,
|
||||
InterpolationCurve.Linear
|
||||
), SoundExpressionPlayMode.UntilDone)
|
||||
})
|
||||
```
|
||||
|
||||
```ghost
|
||||
|
||||
basic.forever(function () {
|
||||
serial.writeValue("Accel", input.acceleration(Dimension.X))
|
||||
serial.writeValue("Compass", input.compassHeading())
|
||||
music.playSoundEffect(music.createSoundEffect(
|
||||
WaveShape.Sine,
|
||||
input.acceleration(Dimension.X),
|
||||
input.compassHeading(),
|
||||
255,
|
||||
0,
|
||||
500,
|
||||
SoundExpressionEffect.None,
|
||||
InterpolationCurve.Linear
|
||||
), SoundExpressionPlayMode.UntilDone)
|
||||
})
|
||||
|
||||
```
|
|
@ -0,0 +1,167 @@
|
|||
# Dance to the Beat
|
||||
|
||||
## 1. Introduction @unplugged
|
||||
|
||||
The new micro:bit has speakers so you can hear sounds without being tied down!
|
||||
|
||||
Let's use movement to create an electronic beat of our own.
|
||||
|
||||
![Dance beat banner message](/static/mb/projects/dance-beat.png)
|
||||
|
||||
|
||||
## 2. Add Play Sound Block
|
||||
|
||||
To start your electronic beat, you'll want to repeat a sound forever.
|
||||
|
||||
---
|
||||
|
||||
⇼ Open the ``||music:Music||`` category and drag the ``||music:play sound [♫ ∿∿∿∿ +] [until done]||``
|
||||
block into the empty ``||basic:forever||`` loop container.
|
||||
|
||||
|
||||
|
||||
```blocks
|
||||
basic.forever(function(){
|
||||
music.playSoundEffect(music.createSoundEffect(WaveShape.Sine, 5000, 0, 255, 0, 500, SoundExpressionEffect.None, InterpolationCurve.Linear), SoundExpressionPlayMode.UntilDone)
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 3. Listen Close
|
||||
|
||||
When your code runs again, you should hear a short laser/alarm
|
||||
sound that repeats over and over forever.
|
||||
|
||||
---
|
||||
|
||||
💡 _You can press the stop button (_⏹️_) beneath the simulator to prevent the noise from bothering you while you're coding._
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## 4. Make a Change
|
||||
|
||||
For the sound to change with your speed of movement, we need to put the
|
||||
micro:bit **acceleration** value in the sound block.
|
||||
|
||||
---
|
||||
|
||||
⇼ On the ``||music:play sound [♫ ∿∿∿ +] [until done]||`` block, click the plus icon (**+**)
|
||||
to show the start frequency value of 5000.
|
||||
|
||||
⇼ From the ``||input:Input||`` category, drag the ``||input:acceleration (mg) [x]||``
|
||||
value block to replace **5000**.
|
||||
|
||||
|
||||
```blocks
|
||||
basic.forever(function(){
|
||||
music.playSoundEffect(music.createSoundEffect(WaveShape.Sine, input.acceleration(Dimension.X), 0, 255, 0, 500, SoundExpressionEffect.None, InterpolationCurve.Linear), SoundExpressionPlayMode.UntilDone)
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 5. Listen Again
|
||||
|
||||
Run your code again.
|
||||
|
||||
---
|
||||
|
||||
This time, hover around above the micro:bit to simulate swinging it from side to side.
|
||||
You should hear the sound change as the micro:bit moves.
|
||||
|
||||
|
||||
|
||||
## 6. Rotation Values
|
||||
|
||||
You can make the beat even more fun by changing the end frequency as the micro:bit rotates.
|
||||
|
||||
---
|
||||
|
||||
⇼ From the ``||input:...more||`` category, drag the ``||input:rotation (°) [pitch]||``
|
||||
value block to replace the frequency **0**.
|
||||
|
||||
|
||||
```blocks
|
||||
basic.forever(function(){
|
||||
music.playSoundEffect(music.createSoundEffect(WaveShape.Sine, input.acceleration(Dimension.X), input.rotation(Rotation.Pitch), 255, 0, 500, SoundExpressionEffect.None, InterpolationCurve.Linear), SoundExpressionPlayMode.UntilDone)
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 7. Listen Again
|
||||
|
||||
Run your code again.
|
||||
|
||||
|
||||
This time, roll over the micro:bit in all directions to simulate turning it while you
|
||||
swing it around.
|
||||
|
||||
---
|
||||
|
||||
You should hear the sound change from high to low and low to high.
|
||||
|
||||
Try moving the micro:bit in different ways. Can you make a fun beat?
|
||||
|
||||
|
||||
|
||||
## 8. Customize Your Beat
|
||||
|
||||
Try changing the **duration** of the beat to something other than **500**. <br/>
|
||||
Then, change the dropdown selection inside both the **acceleration** and **rotation**
|
||||
blocks.
|
||||
|
||||
What else can you click on to edit your sound?
|
||||
|
||||
|
||||
|
||||
|
||||
## Finale
|
||||
|
||||
👏 **YOU DID IT!** 👏
|
||||
|
||||
Imagine how exciting this project will be when it's loaded on a micro:bit!
|
||||
|
||||
If you have a micro:bit v2 (the one with the **shiny gold** logo at the top),
|
||||
download this code and hold your micro:bit while you dance.
|
||||
|
||||
Congratulations, you are your own DJ!
|
||||
|
||||
|
||||
|
||||
```blocks
|
||||
basic.forever(function () {
|
||||
music.playSoundEffect(music.createSoundEffect(
|
||||
WaveShape.Sine,
|
||||
input.acceleration(Dimension.X),
|
||||
input.rotation(Rotation.Pitch),
|
||||
255,
|
||||
0,
|
||||
500,
|
||||
SoundExpressionEffect.None,
|
||||
InterpolationCurve.Linear
|
||||
), SoundExpressionPlayMode.UntilDone)
|
||||
})
|
||||
```
|
||||
|
||||
```ghost
|
||||
|
||||
basic.forever(function () {
|
||||
serial.writeValue("Accel", input.acceleration(Dimension.X))
|
||||
serial.writeValue("Rotation", input.rotation(Rotation.Pitch),)
|
||||
music.playSoundEffect(music.createSoundEffect(
|
||||
WaveShape.Sine,
|
||||
input.acceleration(Dimension.X),
|
||||
input.rotation(Rotation.Pitch),
|
||||
255,
|
||||
0,
|
||||
500,
|
||||
SoundExpressionEffect.None,
|
||||
InterpolationCurve.Linear
|
||||
), SoundExpressionPlayMode.UntilDone)
|
||||
})
|
||||
|
||||
```
|
Двоичный файл не отображается.
После Ширина: | Высота: | Размер: 6.1 KiB |
Двоичный файл не отображается.
После Ширина: | Высота: | Размер: 88 KiB |
Загрузка…
Ссылка в новой задаче