pxt-calliope/docs/projects/hack-your-headphones/code.md

78 lines
3.5 KiB
Markdown
Raw Normal View History

# Code
2016-03-26 00:47:20 +01:00
## ~avatar avatar
2016-03-26 00:47:20 +01:00
Have you ever tried to making beat box sounds based on the light level? Let's try making a beatbox with code!
## ~
2016-03-26 00:47:20 +01:00
Let's start by adding a variable where you can store data. Name the variable as ``light`` and ``||variables:set||`` the value of the variable to the ``||input:light level||`` block from the ``||input:Input||`` drawer. This will get the light level as some value between `0` (dark) and `255` (bright). The light is measured by using various LEDs from the screen. Your code will look like this:
2016-03-26 00:47:20 +01:00
2016-03-29 00:02:08 +02:00
```blocks
let light = input.lightLevel();
2016-03-29 00:02:08 +02:00
```
2016-03-26 00:47:20 +01:00
We also want to play music when the **A** button is pressed. To do that, register an event handler that will execute whenever you click on the **A** button. Open the ``||input:Input||`` drawer and get out an ``||input:on button A pressed||`` block. Next, add a ``||music:rest||`` to play nothing for `1/16` of a beat. Pull the ``||variables:set light||`` block in there too. Your code should look like this:
2016-03-26 00:47:20 +01:00
2016-03-29 00:02:08 +02:00
```blocks
input.onButtonPressed(Button.A, () => {
music.rest(music.beat(BeatFraction.Sixteenth));
2016-04-13 17:48:42 +02:00
let light = input.lightLevel();
2016-03-29 00:02:08 +02:00
});
```
Click on the ``||logic:Logic||`` drawer and find an ``||logic:if||`` block to use so that some of your code will run conditionally depending on whether a [Boolean](/types/boolean) condition is either `true` or `false`. Go get the ``||logic:0 < 0||`` comparison block an put it in as the condition for the ``||logic:if||``. Then insert the variable ``||variables:light||`` into the part of the inequality where the first `0` is. You'll find the variable ``||variables:light||`` in the ``||variables:Variables||`` drawer. Finally, insert `25` in the second part of the comparison instead of `0`. Modify your code so that it checks the light level like this:
* If ``||logic:light < 25||``, play ``||music:ring tone||`` for ``Middle C``
* If this condition is not `true`, play ``||music:ring tone||`` for ``Middle A``
2016-03-26 00:47:20 +01:00
2016-03-29 00:02:08 +02:00
```blocks
input.onButtonPressed(Button.A, () => {
music.rest(music.beat(BeatFraction.Sixteenth));
2016-04-13 17:48:42 +02:00
let light = input.lightLevel();
2016-03-29 00:02:08 +02:00
if (light < 25) {
music.ringTone(music.noteFrequency(Note.C));
}
else {
music.ringTone(music.noteFrequency(Note.A));
}
});
```
2016-03-26 00:47:20 +01:00
Now, we want to add more conditional statements by clicking on the **(+)** at the bottom of the `if`. Add **5** ``||logic:else if||`` and **1** final ``||logic:else||`` inside the ``||logic:if||`` block structure. Set the comparison blocks inside the new conditionals as:
* If ``||logic:light < 50||``, play ``||music:ring tone||`` ``Middle D``
* If ``||logic:light < 100||``, play ``||music:ring tone||`` ``Middle E``
* If ``||logic:light < 150||``, play ``||music:ring tone||`` ``Middle F``
* If ``||logic:light < 180||``, play ``||music:ring tone||`` ``Middle G``
* If these conditions are not true, play ``||music:ring tone||`` ``Middle A``
2016-03-29 00:02:08 +02:00
```blocks
input.onButtonPressed(Button.A, () => {
music.rest(music.beat(BeatFraction.Sixteenth));
2016-04-13 17:48:42 +02:00
let light = input.lightLevel();
2016-03-29 00:02:08 +02:00
if (light < 25) {
music.ringTone(music.noteFrequency(Note.C));
}
else if (light < 50) {
music.ringTone(music.noteFrequency(Note.D));
}
else if (light < 100) {
music.ringTone(music.noteFrequency(Note.E));
}
else if (light < 150) {
music.ringTone(music.noteFrequency(Note.F));
}
else if (light < 180) {
music.ringTone(music.noteFrequency(Note.G));
}
else {
music.ringTone(music.noteFrequency(Note.A));
}
});
```
2016-03-26 00:47:20 +01:00
Alright, now click **Download** and run your code on the @boardname@!