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