updated lessons to use radio

This commit is contained in:
Peli de Halleux 2016-12-08 00:10:00 -08:00
parent c85b6f9507
commit 870b26a85a
3 changed files with 126 additions and 91 deletions

View File

@ -10,12 +10,10 @@ Welcome! This guided tutorial will show you how to program a script that display
### ~ ### ~
## Step 1 ## Step 1
Create a loop that will continuously update the reading of the compass. Create a loop that will continuously update the reading of the compass.
```blocks ```blocks
basic.forever(() => { basic.forever(() => {

View File

@ -2,8 +2,7 @@
### ~avatar avatar ### ~avatar avatar
Use the LEDs to display a flashing heart, and then create Use the LEDs to display a flashing heart!
an animation of a broken heart. :(
### ~ ### ~
@ -38,19 +37,19 @@ basic.clearScreen();
## Step 3 ## Step 3
Put a [forever loop](/reference/basic/forever) around it. Put a [forever loop](/reference/basic/forever) around it to repeat the animation.
```blocks ```blocks
basic.forever(() => { basic.forever(() => {
basic.showLeds(` basic.showLeds(`
. # . # . . # . # .
# # # # # # # # # #
# # # # # # # # # #
. # # # . . # # # .
. . # . .` . . # . .`
); );
basic.pause(500); basic.pause(500);
basic.clearScreen(); basic.clearScreen();
}) })
``` ```
@ -60,45 +59,50 @@ Add a [pause](/reference/basic/pause) to wait after clearing the screen.
```blocks ```blocks
basic.forever(() => { basic.forever(() => {
basic.showLeds(` basic.showLeds(`
. # . # . . # . # .
# # # # # # # # # #
# # # # # # # # # #
. # # # . . # # # .
. . # . .` . . # . .`
); );
basic.pause(500); basic.pause(500);
basic.clearScreen(); basic.clearScreen();
basic.pause(500); basic.pause(500);
}) })
``` ```
## Step 5 ## Send your heartbeats over radio!
Add a second image of a broken heart. Do you have a second @boardname@ at hand? You could use radio and send your heartbeats to other
@boardname@ and show a heart when you receive one.
* move the code in the **forever** inside
a [on data packet received](/reference/radio/on-data-packet-received) handler.
The handler will run whenever a message is received from another @boardname@.
* use [send number](/reference/radio/send-number) and [pause](/reference/basic/pause)
to broadcast a packet of data every second.
```blocks ```blocks
basic.forever(() => { basic.forever(() => {
basic.showLeds(` radio.sendNumber(0)
. # . # . basic.pause(1000)
# # # # # })
# # # # # radio.onDataPacketReceived(({receivedNumber}) => {
. # # # . basic.showLeds(`
. . # . .` . # . # .
); # # # # #
basic.pause(500); # # # # #
basic.clearScreen(); . # # # .
basic.pause(500); . . # . .`);
basic.showLeds(` basic.pause(500)
. # . # . basic.clearScreen()
# . # # # basic.pause(500)
# . . . #
. # # # .
. . # . .`
);
basic.pause(500);
basic.clearScreen();
basic.pause(500);
}) })
``` ```
Download the .hex file onto both @boardname@ and try it out!
```package
radio
```

View File

@ -12,8 +12,8 @@ Use [show leds](/reference/basic/show-leds) to make a smiley face:
```blocks ```blocks
basic.showLeds(` basic.showLeds(`
. # . # . # # . # #
. # . # . # # . # #
. . . . . . . . . .
# . . . # # . . . #
. # # # .` . # # # .`
@ -22,56 +22,89 @@ basic.showLeds(`
## Step 2 ## Step 2
Add an input block for when [button A is pressed](/reference/input/button-is-pressed), and put a Add an input block for when [button A is pressed](/reference/input/button-is-pressed),
frowny face inside it: and **move** the smiley face inside it:
```blocks ```blocks
basic.showLeds(`
. # . # .
. # . # .
. . . . .
# . . . #
. # # # .`
);
input.onButtonPressed(Button.A, () => { input.onButtonPressed(Button.A, () => {
basic.showLeds(` basic.showLeds(`
. # . # . # # . # #
. # . # . # # . # #
. . . . .
. # # # .
# . . . #`
);
});
```
## Step 3
Now add blocks so that when [button B is pressed](/reference/input/button-is-pressed), a smiley appears:
```blocks
basic.showLeds(`
. # . # .
. # . # .
. . . . .
# . . . #
. # # # .`
);
input.onButtonPressed(Button.A, () => {
basic.showLeds(`
. # . # .
. # . # .
. . . . .
. # # # .
# . . . #`
);
});
input.onButtonPressed(Button.B, () => {
basic.showLeds(`
. # . # .
. # . # .
. . . . . . . . . .
# . . . # # . . . #
. # # # .` . # # # .`
); );
}); });
``` ```
Try pressing button A!
## Step 3
Now add blocks so that when [button B is pressed](/reference/input/on-button-pressed),
a frowney appears:
```blocks
input.onButtonPressed(Button.A, () => {
basic.showLeds(`
# # . # #
# # . # #
. . . . .
# . . . #
. # # # .`
);
});
input.onButtonPressed(Button.B, () => {
basic.showLeds(`
# # . # #
# # . # #
. . . . .
. # # # .
# . . . #`
);
});
```
Try pressing ``A`` or ``B``!
## 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(`
# # . # #
# # . # #
. . . . .
. # # # .
# . . . #
`)
}
})
```