|
| 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 | + |
| 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 | + |
| 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 | + |
| 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/). |
0 commit comments