pxt-calliope/docs/projects/smiley-buttons.md

167 lines
3.1 KiB
Markdown
Raw Normal View History

2016-06-14 17:12:13 +02:00
# smiley buttons
## ~avatar
2016-06-11 20:40:09 +02:00
2016-11-10 17:37:13 +01:00
Use buttons to show a smiley face!
## ~
2016-06-11 20:40:09 +02:00
## Step 1
Use [show leds](/reference/basic/show-leds) to make a smiley face:
2016-06-11 20:40:09 +02:00
```blocks
basic.showLeds(`
2016-12-08 09:10:00 +01:00
# # . # #
# # . # #
. . . . .
# . . . #
. # # # .`
);
2016-06-11 20:40:09 +02:00
```
## Step 2
2016-12-08 09:10:00 +01:00
Add an input block for when [button A is pressed](/reference/input/button-is-pressed),
and **move** the smiley face inside it:
2016-06-11 20:40:09 +02:00
```blocks
input.onButtonPressed(Button.A, () => {
basic.showLeds(`
2016-12-08 09:10:00 +01:00
# # . # #
# # . # #
. . . . .
2016-12-08 09:10:00 +01:00
# . . . #
. # # # .`
);
2016-06-11 20:40:09 +02:00
});
```
2016-12-08 09:10:00 +01:00
Try pressing button A!
2016-06-11 20:40:09 +02:00
## Step 3
2017-03-28 14:44:28 +02:00
Add blocks so that when [button B is pressed](/reference/input/on-button-pressed),
2016-12-08 09:10:00 +01:00
a frowney appears:
2016-06-11 20:40:09 +02:00
```blocks
input.onButtonPressed(Button.A, () => {
basic.showLeds(`
2016-12-08 09:10:00 +01:00
# # . # #
# # . # #
. . . . .
2016-12-08 09:10:00 +01:00
# . . . #
. # # # .`
);
2016-06-11 20:40:09 +02:00
});
input.onButtonPressed(Button.B, () => {
basic.showLeds(`
2016-12-08 09:10:00 +01:00
# # . # #
# # . # #
. . . . .
2016-12-08 09:10:00 +01:00
. # # # .
# . . . #`
);
2016-06-11 20:40:09 +02:00
});
2016-06-25 23:22:50 +02:00
```
2016-12-08 09:10:00 +01:00
Try pressing ``A`` or ``B``!
2017-03-28 14:44:28 +02:00
## Step 4
You can also have a secret mode where ``A`` and ``B`` are pressed together.
In that case, add multiple ``||basic:show leds||`` blocks to create an animation...
2017-03-28 14:44:28 +02:00
```blocks
input.onButtonPressed(Button.AB, () => {
basic.showLeds(`
. . . . .
. # . # .
. . . . .
# . . . #
. # # # .
`)
basic.showLeds(`
. . . . .
. # . # .
. . . . .
# # # # #
. . . . .
`)
basic.showLeds(`
. . . . .
. # . # .
. . . . .
. # # # .
# . . . #
`)
basic.showLeds(`
. . . . .
. # . # .
. . . . .
# # # # #
. . . # #
`)
basic.showLeds(`
. . . . .
# . # . .
. . . . .
# . . . #
. # # # .
`)
basic.showLeds(`
. . . . .
. . # . #
. . . . .
# . . . #
. # # # .
`)
})
```
2016-12-08 09:10:00 +01:00
## Send your smileys over radio
Do you have a second @boardname@ at hand? You could use radio and send your smileys or frownies to other
@boardname@.
Since radio can send numbers, we decide that ``0`` is the code for displaying a smiley
and ``1`` is the code for a frowney.
Change your code as follows:
* [radio send number](/reference/radio/send-number) sends a number
to any other @boardname@ in range
* [radio on data packet received](/reference/radio/on-data-packet-received) runs code
when data is received over radio
```blocks
input.onButtonPressed(Button.A, () => {
radio.sendNumber(0)
})
input.onButtonPressed(Button.B, () => {
radio.sendNumber(1)
})
radio.onDataPacketReceived(({receivedNumber}) => {
if (receivedNumber == 0) {
basic.showLeds(`
# # . # #
# # . # #
. . . . .
# . . . #
. # # # .
`)
} else {
basic.showLeds(`
# # . # #
# # . # #
. . . . .
. # # # .
# . . . #
`)
}
})
2016-12-08 18:16:27 +01:00
```
```package
radio
```