Skip to content

Commit c975d8b

Browse files
tutorial: basic content (#10)
1 parent 9979c70 commit c975d8b

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

basic-sleep-wake.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Pressure switch alarm
2+
3+
## Introduction Step @showdialog
4+
You can use the ``||power:Power||`` blocks to control when your micro:bit goes to sleep in low power mode and wakes up to full power mode.
5+
6+
This will help save on batteries when you are running a program for a long time.
7+
8+
You can tell that the micro:bit is saving battery power as the red power LED on the rear of the board will turn off during low-power mode and on during full-power mode.
9+
10+
#### Simulator support
11+
The power saving extension is not yet supported in the MakeCode simulator, so you will need to flash the program to the micro:bit to try it out.
12+
13+
![micro:bit power LED](https://microbit-foundation.github.io/pxt-microbit-v2-power/docs/static/power-led.png)
14+
15+
## Make a micro:bit pressure switch alarm @showdialog
16+
17+
In this tutorial you will create an intruder alarm that wakes up the micro:bit when someone steps on a home-made pressure sensor.
18+
19+
It is inspired by the [Make it: Code it pressure switch alarm project](https://microbit.org/projects/make-it-code-it/pressure-switch-alarm/) available on the [microbit.org](https://microbit.org) website
20+
21+
![micro:bit connected to pressure sensor](https://cdn.sanity.io/images/ajwvhvgo/production/b7072e2101643d75cd09dd89b0dd289dbbea33cc-600x450.jpg)
22+
23+
## Step 1: Start with a basic program
24+
Let's start with a simple program that shows an angry face and plays alarming music when a pin is pressed.
25+
26+
```template
27+
input.onPinPressed(TouchPin.P0, function () {
28+
basic.showIcon(IconNames.Angry)
29+
music.playMelody("C5 G C5 G C5 G C5 G ", 120)
30+
})
31+
```
32+
33+
## Step 2: Put the micro:bit to sleep
34+
Displaying a face and playing a melody forever is going to use up battery power pretty quickly. Let's use the ``||power:Power||`` blocks to save energy before an intruder is detected and after the alarm has been raised
35+
36+
Drag a ``||power:request low power||`` block underneath the ``||music:play melody||`` block. After the alarm sounds, the micro:bit will now try to sleep and save power.
37+
38+
```blocks
39+
input.onPinPressed(TouchPin.P0, function () {
40+
basic.showIcon(IconNames.Angry)
41+
music.startMelody(music.builtInMelody(Melodies.Baddy), MelodyOptions.Forever)
42+
power.lowPowerRequest()
43+
})
44+
```
45+
46+
## Step 3: Tell the micro:bit how to wake up
47+
Good work. You've now told the micro:bit when to go to sleep, but it doesn't yet know how to wake up.
48+
49+
When you use the ``||Power||`` blocks to put the micro:bit to sleep, you will always need to tell it how to wake up again, otherwise it will just ignore you!
50+
51+
Drag ``||basic:on start||`` into the workspace and then add ``||power:full power on pin P0||``underneath ``||basic:on start||`` to tell the micro:bit to wake up when Pin 0 is touched.
52+
53+
```blocks
54+
input.onPinPressed(TouchPin.P0, function () {
55+
basic.showIcon(IconNames.Angry)
56+
music.startMelody(music.builtInMelody(Melodies.Baddy), MelodyOptions.Forever)
57+
power.lowPowerRequest()
58+
})
59+
power.fullPowerOn(FullPowerSource.P0)
60+
```
61+
62+
## Step 4: Try it out
63+
Click ``|Download|`` to transfer your code.
64+
65+
When the micro:bit has sounded the alarm it will enter low power mode until you touch Pin 0 to wake it up again.
66+
67+
## Step 5: Make the alarm @showdialog
68+
69+
![micro:bit connected to pressure sensor](https://cdn.sanity.io/images/ajwvhvgo/production/b7072e2101643d75cd09dd89b0dd289dbbea33cc-600x450.jpg)
70+
71+
Now you have your program, try making a pressure input switch out of cardboard and tin foil like in the picture. Connect the two foil pads on one side to pins ``0`` and ``GND`` on the micro:bit. When you step on the switch, the foil on the top completes an electrical circuit, and it sounds the alert.
72+
73+
More information is available in the [Make it: Code it pressure switch alarm project](https://microbit.org/projects/make-it-code-it/pressure-switch-alarm/).

docs/static/power-led.png

36.6 KB
Loading

pxt.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "power",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "Power saving. micro:bit (V2) only.",
55
"dependencies": {
66
"core": "*"
77
},
88
"files": [
99
"power.cpp",
1010
"power.ts",
11-
"README.md"
11+
"README.md",
12+
"basic-sleep-wake.md"
1213
],
1314
"testFiles": [
1415
"test.ts"

0 commit comments

Comments
 (0)