pxt-microbit 4.0.9 → 4.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/built/target-strings.json +2 -1
- package/built/target.js +38 -30
- package/built/target.json +38 -30
- package/built/targetlight.json +5 -5
- package/built/web/rtlsemantic.css +1 -1
- package/built/web/semantic.css +1 -1
- package/docs/blocks/comments.md +131 -0
- package/docs/blocks/loops.md +12 -0
- package/docs/extensions.md +40 -0
- package/docs/projects/SUMMARY.md +7 -1
- package/docs/projects/micro-chat.md +8 -8
- package/docs/projects/mood-radio.md +3 -1
- package/docs/projects/multi-dice.md +8 -6
- package/docs/projects/plot-acceleration.md +2 -2
- package/docs/projects/v2-blow-away.md +317 -0
- package/docs/projects/v2-clap-lights.md +195 -0
- package/docs/projects/v2-countdown.md +151 -0
- package/docs/projects/v2-morse-chat.md +297 -0
- package/docs/projects/v2-pet-hamster.md +165 -0
- package/docs/projects.md +8 -2
- package/docs/reference/basic/forever.md +17 -4
- package/docs/reference/basic/show-number.md +1 -1
- package/docs/reference/loops/every-interval.md +42 -0
- package/docs/reference/radio/on-received-buffer.md +1 -0
- package/docs/reference/radio/on-received-number.md +2 -0
- package/docs/reference/radio/on-received-string.md +1 -0
- package/docs/reference/radio/on-received-value.md +1 -0
- package/docs/reference/radio/received-packet.md +1 -0
- package/docs/reference/radio/send-buffer.md +1 -0
- package/docs/reference/radio/send-string.md +1 -0
- package/docs/reference/radio/set-group.md +8 -0
- package/docs/reference/radio/write-received-packet-to-serial.md +1 -0
- package/docs/reference/radio/write-value-to-serial.md +1 -0
- package/docs/v2tutorials.md +39 -0
- package/package.json +2 -2
- package/targetconfig.json +23 -6
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# Comments
|
|
2
|
+
|
|
3
|
+
For simple programs, is easy to understand the steps and the flow the program might take when it runs. If you have just a few steps in your program that run one after the other in a sequence, it's fairly easy to understand what's happening at each place in your program.
|
|
4
|
+
|
|
5
|
+
The following program does 4 simple things:
|
|
6
|
+
|
|
7
|
+
1. Show a "Hello" message
|
|
8
|
+
2. Display a smiley face as part of a greeting
|
|
9
|
+
2. Pause for a second so you can see the smiley
|
|
10
|
+
3. Clear the screen
|
|
11
|
+
|
|
12
|
+
```blocks
|
|
13
|
+
basic.showString("Hello!")
|
|
14
|
+
basic.showIcon(IconNames.Happy)
|
|
15
|
+
basic.pause(1000)
|
|
16
|
+
basic.clearScreen()
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
It's quite obvious what this program is doing. Each block does one thing, the next block runs after the previous one in a sequence, then the program ends. Let's say you want the user to show that they saw the greeting. You could add button press event that shows a check mark icon.
|
|
20
|
+
|
|
21
|
+
```blocks
|
|
22
|
+
basic.showString("Hello!")
|
|
23
|
+
basic.showIcon(IconNames.Happy)
|
|
24
|
+
basic.pause(1000)
|
|
25
|
+
basic.clearScreen()
|
|
26
|
+
input.onButtonPressed(Button.A, function () {
|
|
27
|
+
basic.showIcon(IconNames.Yes)
|
|
28
|
+
})
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
You know, of course, that the button press event means that the user has acknowleged your greeting. If you shared your program with someone else though, they might not understand why you wanted to add the button press to the program.
|
|
32
|
+
|
|
33
|
+
## Block comments
|
|
34
|
+
|
|
35
|
+
To let others know what certain parts of your program are supposed to do, you can add **comments**. Comments are a text description that says what that part of your program is doing. To put a comment on a block, open the block menu and select **Add comment**.
|
|
36
|
+
|
|
37
|
+

|
|
38
|
+
|
|
39
|
+
A place for your comment will appear and you can type in your description of what that block is for and what it's supposed to do.
|
|
40
|
+
|
|
41
|
+

|
|
42
|
+
|
|
43
|
+
Once a comment is added to a block, a comment icon will show in the upper-left corner of the block. Here's our program with the comment on the ``||input:on button A press||``.
|
|
44
|
+
|
|
45
|
+
```blocks
|
|
46
|
+
// Signal that the greeting was seen
|
|
47
|
+
input.onButtonPressed(Button.A, function () {
|
|
48
|
+
basic.showIcon(IconNames.Yes)
|
|
49
|
+
})
|
|
50
|
+
basic.showString("Hello!")
|
|
51
|
+
basic.showIcon(IconNames.Happy)
|
|
52
|
+
basic.pause(1000)
|
|
53
|
+
basic.clearScreen()
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
In the Blocks editor, the comment is displayed when you click on the comment icon. If you view the JavaScript or Python code, you'll see a comment line directly above the button press code.
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
// Signal that the greeting was seen
|
|
60
|
+
input.onButtonPressed(Button.A, function () {
|
|
61
|
+
basic.showIcon(IconNames.Yes)
|
|
62
|
+
})
|
|
63
|
+
basic.showString("Hello!")
|
|
64
|
+
basic.showIcon(IconNames.Happy)
|
|
65
|
+
basic.pause(1000)
|
|
66
|
+
basic.clearScreen()
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
When a program contains conditionals, loops, or functions, adding comments becomes important to help understand what's happening at those places in your program. The program can take a different path based on a condition, result from a function, or some other action.
|
|
70
|
+
|
|
71
|
+
### ~ hint
|
|
72
|
+
|
|
73
|
+
#### Workspace comments
|
|
74
|
+
|
|
75
|
+
You can add comments an notes about your project with **Workspace Comments**. Just right-click on the Workspace background and choose **Add Comment** to insert your comments for the project.
|
|
76
|
+
|
|
77
|
+
```block
|
|
78
|
+
/**
|
|
79
|
+
* This is a workspace comment.
|
|
80
|
+
*
|
|
81
|
+
* Use this space to make comments
|
|
82
|
+
*
|
|
83
|
+
* and notes about your project.
|
|
84
|
+
*/
|
|
85
|
+
// Display a message
|
|
86
|
+
function showMessage () {
|
|
87
|
+
basic.showString("Workspaces have comments!")
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### ~
|
|
92
|
+
|
|
93
|
+
The following example has a conditional inside a loop to choose one of three different mood values. For each mood, a function will display an icon for it. Each block has a comment that describes what it will do. Take a look at the JavaScript or Python code to see the comments on the code text also.
|
|
94
|
+
|
|
95
|
+
```blocks
|
|
96
|
+
/**
|
|
97
|
+
* The mood icon project.
|
|
98
|
+
*
|
|
99
|
+
* TODO: add more moods
|
|
100
|
+
*
|
|
101
|
+
* 1. Sad
|
|
102
|
+
*
|
|
103
|
+
* 2. Confused
|
|
104
|
+
*/
|
|
105
|
+
// Display an emotion of love
|
|
106
|
+
function heart () {
|
|
107
|
+
basic.showIcon(IconNames.Heart)
|
|
108
|
+
}
|
|
109
|
+
// Show a surprised expression
|
|
110
|
+
function surprised () {
|
|
111
|
+
basic.showIcon(IconNames.Surprised)
|
|
112
|
+
}
|
|
113
|
+
// Display a smiley mood
|
|
114
|
+
function smiley () {
|
|
115
|
+
basic.showIcon(IconNames.Happy)
|
|
116
|
+
}
|
|
117
|
+
// My program that shows three mood icons
|
|
118
|
+
// Run the loop to show 3 icons
|
|
119
|
+
for (let index = 0; index <= 2; index++) {
|
|
120
|
+
// Select an icon based on the index
|
|
121
|
+
if (index == 0) {
|
|
122
|
+
smiley()
|
|
123
|
+
} else if (index == 1) {
|
|
124
|
+
heart()
|
|
125
|
+
} else {
|
|
126
|
+
surprised()
|
|
127
|
+
}
|
|
128
|
+
// Pause for a second between moods
|
|
129
|
+
basic.pause(1000)
|
|
130
|
+
}
|
|
131
|
+
```
|
package/docs/blocks/loops.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
1
|
# @extends
|
|
2
2
|
|
|
3
3
|
## #specific
|
|
4
|
+
|
|
5
|
+
```cards
|
|
6
|
+
loops.everyInterval(500, function () {})
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## #seealso
|
|
10
|
+
|
|
11
|
+
[for](/blocks/loops/for),
|
|
12
|
+
[while](/blocks/loops/while),
|
|
13
|
+
[repeat](/blocks/loops/repeat),
|
|
14
|
+
[for of](/blocks/loops/for-of),
|
|
15
|
+
[every](/reference/loops/every)
|
package/docs/extensions.md
CHANGED
|
@@ -62,6 +62,14 @@ Check out [the accessories pages on microbit.org](https://microbit.org/buy/acces
|
|
|
62
62
|
|
|
63
63
|
```codecard
|
|
64
64
|
[{
|
|
65
|
+
"name": "Kitronik 128x64 Display",
|
|
66
|
+
"url": "/pkg/KitronikLtd/pxt-kitronik-128x64Display",
|
|
67
|
+
"cardType": "package"
|
|
68
|
+
}, {
|
|
69
|
+
"name": "Monk Makes 7-Segment",
|
|
70
|
+
"url": "/pkg/monkmakes/monkmakes-7-segment",
|
|
71
|
+
"cardType": "package"
|
|
72
|
+
}, {
|
|
65
73
|
"name": "Pimoroni inky:bit",
|
|
66
74
|
"url": "/pkg/pimoroni/pxt-inkybit",
|
|
67
75
|
"cardType": "package"
|
|
@@ -156,6 +164,10 @@ Check out [the accessories pages on microbit.org](https://microbit.org/buy/acces
|
|
|
156
164
|
|
|
157
165
|
```codecard
|
|
158
166
|
[{
|
|
167
|
+
"name": "TCS3200 Color sensor",
|
|
168
|
+
"url":"/pkg/joy-it/pxt-SEN-Color",
|
|
169
|
+
"cardType": "package"
|
|
170
|
+
}, {
|
|
159
171
|
"name": "MPU6050 Gyroscope",
|
|
160
172
|
"url":"/pkg/joy-it/SEN-MPU6050",
|
|
161
173
|
"cardType": "package"
|
|
@@ -316,9 +328,21 @@ Check out [the accessories pages on microbit.org](https://microbit.org/buy/acces
|
|
|
316
328
|
|
|
317
329
|
```codecard
|
|
318
330
|
[{
|
|
331
|
+
"name": "KittenBot Powerbrick",
|
|
332
|
+
"url": "/pkg/KittenBot/pxt-powerbrick",
|
|
333
|
+
"cardType": "package"
|
|
334
|
+
}, {
|
|
335
|
+
"name": "Kitronik LAB:bit",
|
|
336
|
+
"url": "/pkg/KitronikLtd/pxt-kitronik-lab-bit",
|
|
337
|
+
"cardType": "package"
|
|
338
|
+
}, {
|
|
319
339
|
"name": "PT-BOT PTKidsBIT",
|
|
320
340
|
"url": "/pkg/iBuilds/pxt-PTKidsBIT",
|
|
321
341
|
"cardType": "package"
|
|
342
|
+
}, {
|
|
343
|
+
"name": "Stemhub City",
|
|
344
|
+
"url": "/pkg/stemhub/pxt-StemhubCity",
|
|
345
|
+
"cardType": "package"
|
|
322
346
|
}, {
|
|
323
347
|
"name": "Pi Supply tinker:kit",
|
|
324
348
|
"url": "/pkg/PiSupply/pxt-tinker-kit",
|
|
@@ -388,6 +412,10 @@ Check out [the accessories pages on microbit.org](https://microbit.org/buy/acces
|
|
|
388
412
|
|
|
389
413
|
```codecard
|
|
390
414
|
[{
|
|
415
|
+
"name": "Elecfreaks Smart AI Lens ",
|
|
416
|
+
"url":"/pkg/elecfreaks/pxt-PlanetX-AI",
|
|
417
|
+
"cardType": "package"
|
|
418
|
+
}, {
|
|
391
419
|
"name": "MU Vision camera",
|
|
392
420
|
"url":"/pkg/mu-opensource/pxt-muvision",
|
|
393
421
|
"cardType": "package"
|
|
@@ -401,6 +429,18 @@ Check out [the accessories pages on microbit.org](https://microbit.org/buy/acces
|
|
|
401
429
|
|
|
402
430
|
```codecard
|
|
403
431
|
[{
|
|
432
|
+
"name": "MakeKit Hoverbit",
|
|
433
|
+
"url":"/pkg/gomakekit/Hoverbit_V2",
|
|
434
|
+
"cardType": "package"
|
|
435
|
+
}, {
|
|
436
|
+
"name": "Stemhubbit car",
|
|
437
|
+
"url":"/pkg/stemhub/pxt-Stemhubbit",
|
|
438
|
+
"cardType": "package"
|
|
439
|
+
}, {
|
|
440
|
+
"name": "MATRIX Micro",
|
|
441
|
+
"url":"/pkg/pxt-MatrixMicro",
|
|
442
|
+
"cardType": "package"
|
|
443
|
+
}, {
|
|
404
444
|
"name": "PTKidsBIT",
|
|
405
445
|
"url":"/pkg/iBuilds/pxt-PTKidsBIT-Robot",
|
|
406
446
|
"cardType": "package"
|
package/docs/projects/SUMMARY.md
CHANGED
|
@@ -7,6 +7,12 @@
|
|
|
7
7
|
* [Dice](/projects/dice)
|
|
8
8
|
* [Love Meter](/projects/love-meter)
|
|
9
9
|
* [Micro Chat](/projects/micro-chat)
|
|
10
|
+
* [Tutorials for the new micro:bit (V2)](/v2tutorials)
|
|
11
|
+
* [Pet Hamster](/projects/v2-pet-hamster)
|
|
12
|
+
* [Countdown](/projects/v2-countdown)
|
|
13
|
+
* [Morse Chat](/projects/v2-morse-chat)
|
|
14
|
+
* [Clap Lights](/projects/v2-clap-lights)
|
|
15
|
+
* [Blow Away](/projects/v2-blow-away)
|
|
10
16
|
* [Live Coding](/live-coding)
|
|
11
17
|
* [Flashing Heart](https://youtu.be/NvEOKZ8wh9s)
|
|
12
18
|
* [Name Tag](https://youtu.be/xpRI5jjQ31E)
|
|
@@ -106,7 +112,7 @@
|
|
|
106
112
|
* [Square](/projects/turtle-square)
|
|
107
113
|
* [Spiral](/projects/turtle-spiral)
|
|
108
114
|
* [Scanner](/projects/turtle-scanner)
|
|
109
|
-
* [Blocks
|
|
115
|
+
* [Blocks to JavaScript](/courses/blocks-to-javascript)
|
|
110
116
|
* [Hello JavaScript](/courses/blocks-to-javascript/hello-javascript)
|
|
111
117
|
* [Starter Blocks](/courses/blocks-to-javascript/starter-blocks)
|
|
112
118
|
* [Writing Code](/courses/blocks-to-javascript/writing-code)
|
|
@@ -6,6 +6,14 @@
|
|
|
6
6
|
|
|
7
7
|
Use the **radio** to send and receive messages with other @boardname@.
|
|
8
8
|
|
|
9
|
+
## Set a radio group
|
|
10
|
+
|
|
11
|
+
The first thing you want to do is to put in a ``||radio:set group||``. This will make certain that you and another @boardname@ will receive just the messages assigned to that group and not the messages sent from some other @boardname@ that's not part of your conversation. This like tuning to a particular channel to talk on.
|
|
12
|
+
|
|
13
|
+
```blocks
|
|
14
|
+
radio.setGroup(1)
|
|
15
|
+
```
|
|
16
|
+
|
|
9
17
|
## Sending a message
|
|
10
18
|
|
|
11
19
|
Use ``||input:on button pressed||`` to send a text message over radio with ``||radio:send string||``.
|
|
@@ -53,14 +61,6 @@ radio.onReceivedString(function (receivedString) {
|
|
|
53
61
|
|
|
54
62
|
If you have two @boardname@s, download the program to each one. Press button **A** on one and see if the other gets a message.
|
|
55
63
|
|
|
56
|
-
## Groups
|
|
57
|
-
|
|
58
|
-
Use the ``||radio:set group||`` block to assign a **group** number to your program. You will only receive messages from @boardname@s within the same group. Use this to avoid receiving messages from every @boardname@ that is transmitting.
|
|
59
|
-
|
|
60
|
-
```blocks
|
|
61
|
-
radio.setGroup(123)
|
|
62
|
-
```
|
|
63
|
-
|
|
64
64
|
```package
|
|
65
65
|
radio
|
|
66
66
|
```
|
|
@@ -15,6 +15,7 @@ between @boardname@s using the radio antenna, just like a phone can send text me
|
|
|
15
15
|
Let's add blocks that send a number when button ``A`` is pressed. We assume that `0` is the "mood code" to send for **smiley**.
|
|
16
16
|
|
|
17
17
|
```blocks
|
|
18
|
+
radio.setGroup(1)
|
|
18
19
|
input.onButtonPressed(Button.A, () => {
|
|
19
20
|
radio.sendNumber(0)
|
|
20
21
|
basic.showIcon(IconNames.Happy)
|
|
@@ -65,9 +66,10 @@ That's it. Download your code to multiple @boardname@s and try it out!
|
|
|
65
66
|
|
|
66
67
|
Try adding a new code and use the ``||input:on shake||`` event to send it.
|
|
67
68
|
|
|
68
|
-
##
|
|
69
|
+
## Complete program
|
|
69
70
|
|
|
70
71
|
```blocks
|
|
72
|
+
radio.setGroup(1)
|
|
71
73
|
input.onButtonPressed(Button.A, () => {
|
|
72
74
|
radio.sendNumber(0)
|
|
73
75
|
basic.showIcon(IconNames.Happy)
|
|
@@ -22,9 +22,9 @@ input.onGesture(Gesture.Shake, function () {
|
|
|
22
22
|
|
|
23
23
|
We need to store the result of the dice cast in a variable. A **variable** is like a place in the memory of the @boardname@ where you save information, like numbers.
|
|
24
24
|
|
|
25
|
-
* Go to the **Variables** toolbox and click
|
|
26
|
-
* Add a ``||set dice to||`` block and drag the ``||pick random||`` into it.
|
|
27
|
-
* Drag a ``||dice||`` from the **Variables** toolbox into the ``||basic:show number||`` block.
|
|
25
|
+
* Go to the **Variables** toolbox and click ``Make a Variable`` to create a new variable. We will call it **dice**.
|
|
26
|
+
* Add a ``||variables:set dice to||`` block and drag the ``||math:pick random||`` into it.
|
|
27
|
+
* Drag a ``||variables:dice||`` variable from the **Variables** toolbox into the ``||basic:show number||`` block.
|
|
28
28
|
|
|
29
29
|
```blocks
|
|
30
30
|
let dice = 0
|
|
@@ -36,9 +36,10 @@ input.onGesture(Gesture.Shake, function () {
|
|
|
36
36
|
|
|
37
37
|
## Send the dice
|
|
38
38
|
|
|
39
|
-
Put in a ``||radio:send number||`` and a ``||dice||`` to send the value stored in the
|
|
39
|
+
Put in a ``||radio:send number||`` and a ``||variables:dice||`` to send the value stored in the ``||variables:dice||`` variable via radio. Make sure to add a ``||radio:set group||`` to ``||basic:on start||`` with the group number set to the group you want to use.
|
|
40
40
|
|
|
41
41
|
```blocks
|
|
42
|
+
radio.setGroup(1)
|
|
42
43
|
let dice = 0
|
|
43
44
|
input.onGesture(Gesture.Shake, function () {
|
|
44
45
|
dice = randint(1, 6)
|
|
@@ -49,7 +50,7 @@ input.onGesture(Gesture.Shake, function () {
|
|
|
49
50
|
|
|
50
51
|
## Receive the dice
|
|
51
52
|
|
|
52
|
-
Go get an ``||radio:on received number||`` event block. This event runs when a radio message from another @boardname@ arrives. The
|
|
53
|
+
Go get an ``||radio:on received number||`` event block. This event runs when a radio message from another @boardname@ arrives. The ``||variables:receivedNumber||`` value is the value of the dice in this game.
|
|
53
54
|
|
|
54
55
|
```blocks
|
|
55
56
|
radio.onReceivedNumber(function (receivedNumber) {
|
|
@@ -58,7 +59,7 @@ radio.onReceivedNumber(function (receivedNumber) {
|
|
|
58
59
|
|
|
59
60
|
## Check your cast
|
|
60
61
|
|
|
61
|
-
Add a ``||logic:if||`` block to test if
|
|
62
|
+
Add a ``||logic:if||`` block to test if ``||variables:receivedNumber||`` is greater or equal to ``||variables:dice||``.
|
|
62
63
|
If is, you lost so display a sad face on the screen.
|
|
63
64
|
|
|
64
65
|
```blocks
|
|
@@ -78,6 +79,7 @@ If you have more than one @boardname@, download your code onto each one and try
|
|
|
78
79
|
|
|
79
80
|
```blocks
|
|
80
81
|
let dice = 0
|
|
82
|
+
radio.setGroup(1)
|
|
81
83
|
input.onGesture(Gesture.Shake, function () {
|
|
82
84
|
dice = randint(1, 6)
|
|
83
85
|
basic.showNumber(dice)
|
|
@@ -6,7 +6,7 @@ The ``||led:plot bar graph||`` uses the screen to display the _magnitude_ (how b
|
|
|
6
6
|
|
|
7
7
|
## Acceleration
|
|
8
8
|
|
|
9
|
-
In a ``||
|
|
9
|
+
In a ``||basic:forever||`` loop, ``||led:plot||`` ``||input:acceleration||`` in the ``x`` dimension on the LEDs.
|
|
10
10
|
|
|
11
11
|
```blocks
|
|
12
12
|
basic.forever(function() {
|
|
@@ -50,4 +50,4 @@ basic.forever(function() {
|
|
|
50
50
|
|
|
51
51
|
## Download and try
|
|
52
52
|
|
|
53
|
-
Download the code to your @boardname@ and test the sensors.
|
|
53
|
+
Download the code to your @boardname@ and test the sensors.
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# Blow Away
|
|
2
|
+
|
|
3
|
+
## 1. Introduction pt. 1 @unplugged
|
|
4
|
+
|
|
5
|
+
👻 Oh, no! Your @boardname@ is being haunted by a ghost named Haven 👻
|
|
6
|
+
|
|
7
|
+
For this tutorial, we'll learn how to blow Haven away 🌬️
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
## 2. Haunted ghost setup
|
|
12
|
+
|
|
13
|
+
🏚️ **BooOooOoo** 🏚️
|
|
14
|
+
|
|
15
|
+
A wild Haven has appeared!
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
► From the ``||basic:Basic||`` category, find ``||basic:show icon [ ]||`` and add it to your ``||basic:on start||`` container.
|
|
20
|
+
|
|
21
|
+
► Click the heart icon and set it to show a ghost.
|
|
22
|
+
💡 In the ``show icon`` dropdown menu options, you can hover to see what each design is called.
|
|
23
|
+
|
|
24
|
+
```blocks
|
|
25
|
+
// @highlight
|
|
26
|
+
basic.showIcon(IconNames.Ghost)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## 3. Loop setup
|
|
32
|
+
|
|
33
|
+
➰ **Looping around** ➰
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
► From the ``||loops:Loops||`` category, find the ``||loops:repeat [4] times||`` loop and snap it into your empty ``||basic:forever||`` container.
|
|
38
|
+
💡 Why do we need a [__*repeat loop*__](#repeatLoop "repeat code for a given number of times") when we already have a ``forever`` container? Because ``forever`` has an embedded delay that we want to avoid!
|
|
39
|
+
|
|
40
|
+
```blocks
|
|
41
|
+
basic.forever(function () {
|
|
42
|
+
// @highlight
|
|
43
|
+
for (let index = 0; index < 4; index++) {
|
|
44
|
+
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## 4. Conditional setup
|
|
50
|
+
|
|
51
|
+
🤔 **Conditioning and comparing** 🤔
|
|
52
|
+
|
|
53
|
+
Haven hates noise and will blow away if things get too loud. Let's use an [__*if statement*__](#ifstatement "if this condition is met, do something") to check for sounds.
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
► From ``||logic:Logic||``, grab an ``||logic:if <true> then||`` statement and snap it into your empty ``||loops:repeat [4] times do||`` loop.
|
|
58
|
+
|
|
59
|
+
► Go back to ``||logic:Logic||`` to get a ``||logic:<[0] [=] [0]>||`` comparison.
|
|
60
|
+
|
|
61
|
+
► Snap ``||logic:<[0] [=] [0]>||`` in to **replace** the ``||logic:<true>||`` condition for your ``||logic:if then||`` statement.
|
|
62
|
+
|
|
63
|
+
```blocks
|
|
64
|
+
basic.forever(function () {
|
|
65
|
+
// @highlight
|
|
66
|
+
for (let index = 0; index < 4; index++) {
|
|
67
|
+
// @highlight
|
|
68
|
+
if (0 == 0) {
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 5. Blow sound
|
|
76
|
+
|
|
77
|
+
👂 **Haven's ears** 👂
|
|
78
|
+
|
|
79
|
+
We'll be using a [__*sound threshold*__](#soundThreshold "a number for how loud a sound needs to be to trigger an event. 0 = silence to 255 = maximum noise") to act as Haven's ears.
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
► From the ``||input:Input||`` category, drag ``||input:sound level||`` in to **replace** the **_left_ ``0``** of your ``||logic:<[0] [=] [0]>||`` comparison.
|
|
84
|
+
|
|
85
|
+
► Using the dropdown in the **middle** of ``||logic:[sound level] [=] [0]||``, change the comparison to be **``>``** (greater than).
|
|
86
|
+
|
|
87
|
+
► Finally, have the **right side** of the comparison say ``128`` so your full comparison reads: **``sound level > 128``**.
|
|
88
|
+
💡 This means Haven will hear any sound above ``128``.
|
|
89
|
+
|
|
90
|
+
```blocks
|
|
91
|
+
basic.forever(function () {
|
|
92
|
+
for (let index = 0; index < 4; index++) {
|
|
93
|
+
// @highlight
|
|
94
|
+
if (input.soundLevel() > 128) {
|
|
95
|
+
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## 6. Making variables
|
|
102
|
+
|
|
103
|
+
☀️ **Variable weather** 🌨️
|
|
104
|
+
|
|
105
|
+
Let's create some [__*variables*__](#variable "a holder for information that may change") to keep track of Haven's movement.
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
► In the ``||variables:Variables||`` category, click on ``Make a Variable...`` and make a variable named ``col``.
|
|
110
|
+
💡 ``col`` is short for "column".
|
|
111
|
+
|
|
112
|
+
► Make **another** variable and name it ``row``.
|
|
113
|
+
|
|
114
|
+
## 7. Displacing LEDs part 1
|
|
115
|
+
|
|
116
|
+
🔀 **Random chance** 🔀
|
|
117
|
+
|
|
118
|
+
To show Haven is blowing away, we want to move a random set of lights sideways.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
► Your ``||variables:Variables||`` category should now have the option to ``||variables:set [row] to [0]||``. Drag that block into your empty ``||logic:if then||`` statement.
|
|
123
|
+
|
|
124
|
+
► From the ``||math:Math||`` category, find ``||math:pick random [0] to [10]||`` and snap that in to **replace** the ``[0]`` in your ``||variables:set [row] to [0]||`` block.
|
|
125
|
+
|
|
126
|
+
► Change the maximum number from ``10`` to **``4``**.
|
|
127
|
+
💡 We are setting the maximum random value to 4 because the lights on the @boardname@ are numbered 0, 1, 2, 3, and 4 for columns and rows.
|
|
128
|
+
|
|
129
|
+
```blocks
|
|
130
|
+
let row = 0
|
|
131
|
+
basic.forever(function () {
|
|
132
|
+
for (let index = 0; index < 4; index++) {
|
|
133
|
+
if (input.soundLevel() > 128) {
|
|
134
|
+
// @highlight
|
|
135
|
+
row = randint(0, 4)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## 8. Displacing LEDs part 2
|
|
142
|
+
|
|
143
|
+
► Go back into ``||variables:Variables||`` and drag out another ``||variables:set [row] to [0]||``. Place this one below the last one (at **the end**) of your `if then` statement.
|
|
144
|
+
|
|
145
|
+
► Using the **dropdown menu**, set the new block to read ``||variables:set [col] to [0]||``.
|
|
146
|
+
|
|
147
|
+
► From the ``||math:Math||`` category, grab another ``||math:pick random [0] to [10]||`` and snap that in to **replace** the ``[0]`` in your ``||variables:set [col] to [0]||`` block.
|
|
148
|
+
|
|
149
|
+
► Change the maximum number from ``10`` to **``4``**.
|
|
150
|
+
|
|
151
|
+
```blocks
|
|
152
|
+
let col = 0
|
|
153
|
+
let row = 0
|
|
154
|
+
basic.forever(function () {
|
|
155
|
+
for (let index = 0; index < 4; index++) {
|
|
156
|
+
if (input.soundLevel() > 128) {
|
|
157
|
+
row = randint(0, 4)
|
|
158
|
+
// @highlight
|
|
159
|
+
col = randint(0, 4)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
})
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## 9. Conditioning on one point
|
|
166
|
+
|
|
167
|
+
✨ **Ooh, sparkly** ✨
|
|
168
|
+
|
|
169
|
+
Time to move some lights around!
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
► From ``||logic:Logic||``, grab another ``||logic:if <true> then||`` and snap it at the **inside and at the bottom of** your ``||loops:repeat [4] times do||`` loop, right below your ``||logic:if [sound level] [>] [128]||`` statement.
|
|
174
|
+
|
|
175
|
+
► From the ``||led:Led||`` category, find ``||led:point x [0] y [0]||`` and drag it in to **replace** the ``||logic:<true>||`` condition in the **new** ``||logic:if then||`` statement.
|
|
176
|
+
💡 This block will test if the light is on at the the given ``x`` and ``y`` coordinate points.
|
|
177
|
+
|
|
178
|
+
```blocks
|
|
179
|
+
let col = 0
|
|
180
|
+
let row = 0
|
|
181
|
+
basic.forever(function () {
|
|
182
|
+
for (let index = 0; index < 4; index++) {
|
|
183
|
+
if (input.soundLevel() > 128) {
|
|
184
|
+
row = randint(0, 4)
|
|
185
|
+
col = randint(0, 4)
|
|
186
|
+
}
|
|
187
|
+
// @highlight
|
|
188
|
+
if (led.point(0, 0)) { }
|
|
189
|
+
}
|
|
190
|
+
})
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## 10. Unplotting and replotting LEDs
|
|
194
|
+
|
|
195
|
+
To create the animation effect of Haven blowing away, we will turn off (or ``unplot``) a light that is on and then turn it on again (``plot`` it) in a different spot.
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
► From ``||led:Led||``, grab ``||led:unplot x [0] y [0]||`` and snap it inside the **empty** ``||logic:if <point x [0] y [0]> then||`` statement.
|
|
200
|
+
|
|
201
|
+
► Go back to ``||led:Led||`` and get ``||led:plot x [0] y [0]||``. Snap that in **beneath** the ``||led:unplot x [0] y [0]||`` block that you just added.
|
|
202
|
+
|
|
203
|
+
```blocks
|
|
204
|
+
let col = 0
|
|
205
|
+
let row = 0
|
|
206
|
+
basic.forever(function () {
|
|
207
|
+
for (let index = 0; index < 4; index++) {
|
|
208
|
+
if (input.soundLevel() > 128) {
|
|
209
|
+
row = randint(0, 4)
|
|
210
|
+
col = randint(0, 4)
|
|
211
|
+
}
|
|
212
|
+
// @highlight
|
|
213
|
+
if (led.point(0, 0)) {
|
|
214
|
+
led.unplot(0, 0)
|
|
215
|
+
led.plot(0, 0)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
})
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## 11. Setting variables
|
|
222
|
+
|
|
223
|
+
📃 **Columns and rows** 📃
|
|
224
|
+
|
|
225
|
+
Notice how you have **three** blocks from the ``||led:Led||`` category. All three have ``||led:x||`` ``[0]`` and ``||led:y||`` ``[0]`` coordinates. In these **two** steps, we will set it so that every ``||led:x||`` is followed by the ``||variables:col||`` variable and every ``||led:y||`` is followed by the ``||variables:row||`` variable.
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
► From ``||variables:Variables||``, get three copies of ``||variables:col||``, and use them to **replace the ``x`` values** in the following three blocks:
|
|
230
|
+
**1.** ``||led:point x [0] y [0]||``
|
|
231
|
+
**2.** ``||led:unplot x [0] y [0]||``
|
|
232
|
+
**3.** ``||led:plot x [0] y [0]||``
|
|
233
|
+
|
|
234
|
+
► Go into ``||variables:Variables||``, get three copies of ``||variables:row||``, and use them to **replace the ``y`` values** in the same three blocks.
|
|
235
|
+
|
|
236
|
+
```blocks
|
|
237
|
+
let col = 0
|
|
238
|
+
let row = 0
|
|
239
|
+
basic.forever(function () {
|
|
240
|
+
for (let index = 0; index < 4; index++) {
|
|
241
|
+
if (input.soundLevel() > 128) {
|
|
242
|
+
row = randint(0, 4)
|
|
243
|
+
col = randint(0, 4)
|
|
244
|
+
}
|
|
245
|
+
// @highlight
|
|
246
|
+
if (led.point(col, row)) {
|
|
247
|
+
led.unplot(col, row)
|
|
248
|
+
led.plot(col, row)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
})
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## 12. Moving LEDs
|
|
255
|
+
|
|
256
|
+
➕ **Math makes the lights go swoosh** ➗
|
|
257
|
+
|
|
258
|
+
Right now, we are unplotting and replotting in the same spot. What we want to do is move the lights we're turning back on just a smidge to the right every time until there's nothing left on the grid.
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
► From ``||math:Math||``, find the ``||math:[0] [+] [0]||`` operation and use it to **replace** ``||variables:col||`` in your ``||led:plot x [col] y [row]||`` block.
|
|
263
|
+
💡 If you move your entire ``||basic:forever||`` container, you should find a greyed out ``col`` variable in your workspace.
|
|
264
|
+
|
|
265
|
+
► Take the greyed out ``||variables:col||`` variable (or get a new one) and use it to **replace** the **_first_ ``[0]``** so the operation reads ``||math:[col] [+] [0]||``.
|
|
266
|
+
|
|
267
|
+
► Replace the **_second_ ``[0]``** with **``[1]``** so the operation reads ``||math:[col] [+] [1]||``.
|
|
268
|
+
|
|
269
|
+
```blocks
|
|
270
|
+
let col = 0
|
|
271
|
+
let row = 0
|
|
272
|
+
basic.forever(function () {
|
|
273
|
+
for (let index = 0; index < 4; index++) {
|
|
274
|
+
if (input.soundLevel() > 128) {
|
|
275
|
+
row = randint(0, 4)
|
|
276
|
+
col = randint(0, 4)
|
|
277
|
+
}
|
|
278
|
+
// @highlight
|
|
279
|
+
if (led.point(col, row)) {
|
|
280
|
+
led.unplot(col, row)
|
|
281
|
+
led.plot(col + 1, row)
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
})
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
## 13. Testing in the simulator
|
|
288
|
+
|
|
289
|
+
🌬️ **Test what you've created** 👻
|
|
290
|
+
|
|
291
|
+
Check out the simulator!
|
|
292
|
+
|
|
293
|
+
---
|
|
294
|
+
|
|
295
|
+
► Click on the pink bar underneath the microphone icon. Drag it above the sound number you chose (we used ``128``!) to blow Haven away.
|
|
296
|
+
|
|
297
|
+
► If you have a new @boardname@ (the one with the **shiny gold** logo at the top), download this code and try it out!
|
|
298
|
+
💡 Blow close to the @boardname@ and watch Haven swoosh away 💨
|
|
299
|
+
💡 Use your @boardname@'s reset button (it's on the back!) to bring Haven back 👻
|
|
300
|
+
|
|
301
|
+
```blocks
|
|
302
|
+
let col = 0
|
|
303
|
+
let row = 0
|
|
304
|
+
basic.showIcon(IconNames.Ghost)
|
|
305
|
+
basic.forever(function () {
|
|
306
|
+
for (let index = 0; index < 4; index++) {
|
|
307
|
+
if (input.soundLevel() > 128) {
|
|
308
|
+
row = randint(0, 4)
|
|
309
|
+
col = randint(0, 4)
|
|
310
|
+
}
|
|
311
|
+
if (led.point(col, row)) {
|
|
312
|
+
led.unplot(col, row)
|
|
313
|
+
led.plot(col + 1, row)
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
})
|
|
317
|
+
```
|