To make my door alarm, I've got a new MakeCode project. Let's get rid of the 'on start' block
but keep the 'forever' block because we're going to keep checking the magnetic field measured by
the micro:bit's magnetometer, its compass. Go to 'Logic' and find the 'if' block and drag that in,
and also in 'Logic' we'll find the comparison block, so get that hexagonal block and drop
it on top of 'true'. Then, if we go to 'Input' and click on 'more', you'll find a block called 'magnetic force'.
Drop it in the first hole in the comparison block, and we're going to change it
so that it measures the overall strength of the magnetic force. Your micro::bit's magnetometer can
measure magnetic force in different dimensions, but we're just interested in just how strong it is overall.
And we're going to pop a number in here that we might need to change later but
I'm just going to pick 200 for the moment. It measures the strength of the magnetic field
in a unit called 'microTeslas' and I'm going to try 200 to start with. If the magnetic field
falls below 200 microTeslas, I'm going to show an angry face because what that will mean is that the
magnet has gone away from my micro:bit, somebody's opened the door, and we want to know if someone's
been in the room so it's going to show an angry face on the display. How do I know what number
to pick for the magnetic force? How do I know how strong my magnet is? Well, we're going to
do something to sort that out. So, if I go to 'Input', I drag in 'on button A pressed',
and we'll add a 'show number' block. Let's copy the magnetic force block if I right-click on it,
duplicate, and drag it in here and drop it there. What this means now is that when I press button A,
I can measure the amount of magnetic force measured by the micro:bit. I can see it on the display.
So, we're ready to try this out now. It doesn't work in the simulator,
so we're going to put this on a real micro:bit. So, I'm going to call it 'door alarm',
and I'm going to download the hex file, and transfer it to my micro:bit. I've fixed the
micro:bit and the battery pack to the door frame using something like Blu Tak and put the magnet
on the door itself, very close to it. Have a look on the back of the micro:bit board and see where the compass is.
You need to make sure the magnet is close to the compass chip on the back of your micro:bit.
Now we can calibrate it. With the door closed, press button A and get a reading of
the magnetic field strength. In my case, it's around 453 microTeslas. I'm then going to open the door,
press button A again and take a reading with the door open. It's 129 microTeslas now.
It can still measure some magnetism, don't forget the Earth is magnetic, and there are other things probably
in my house that are magnetic as well, so the micro:bit is still picking up some magnetic field.
So my number of 200 should work pretty well. If it's more than 200, it means the door is shut,
it sounds likely. Because with the door shut that's 453 microTeslas. Below 200, that probably
means somebody has opened the door, in this case I know when my door is open I have 129 microTeslas.
So that should work OK now, I should be able to tell when someone has opened my door,
an angry face will appear on my micro:bit's display, and I'll know that somebody has been in my room.
There are lots of things you could do to this project to augment it. You could perhaps
have a delay to give yourself some time to get out the room. Remember if you need to reset it,
you could press button A to show the magnetic force again and that will clear the angry face
off the display of your micro:bit. Maybe you could add an audible alarm too or
you could add a music block so that when somebody opens your door, it makes an audible sound to warn you
that somebody has been inside your room. There are all sorts of ways you could modify this project.