1 00:00:00,640 --> 00:00:05,040 To make my door alarm, I've got a new MakeCode  project. Let's get rid of the 'on start' block 2 00:00:05,040 --> 00:00:09,560 but keep the 'forever' block because we're going  to keep checking the magnetic field measured by 3 00:00:09,560 --> 00:00:18,160 the micro:bit's magnetometer, its compass. Go to  'Logic' and find the 'if' block and drag that in, 4 00:00:18,160 --> 00:00:22,680 and also in 'Logic' we'll find the comparison  block, so get that hexagonal block and drop 5 00:00:22,680 --> 00:00:29,960 it on top of 'true'. Then, if we go to 'Input'  and click on 'more', you'll find a block called 'magnetic force'. 6 00:00:29,960 --> 00:00:36,920 Drop it in the first hole in  the comparison block, and we're going to change it 7 00:00:36,920 --> 00:00:42,720 so that it measures the overall strength of the  magnetic force. Your micro::bit's magnetometer can 8 00:00:42,720 --> 00:00:47,360 measure magnetic force in different dimensions,  but we're just interested in just how strong it is overall. 9 00:00:47,360 --> 00:00:51,960 And we're going to pop a number  in here that we might need to change later but 10 00:00:51,960 --> 00:00:56,320 I'm just going to pick 200 for the moment. It  measures the strength of the magnetic field 11 00:00:56,320 --> 00:01:01,840 in a unit called 'microTeslas' and I'm going to  try 200 to start with. If the magnetic field 12 00:01:01,840 --> 00:01:09,480 falls below 200 microTeslas, I'm going to show an  angry face because what that will mean is that the 13 00:01:09,480 --> 00:01:14,960 magnet has gone away from my micro:bit, somebody's  opened the door, and we want to know if someone's 14 00:01:14,960 --> 00:01:20,560 been in the room so it's going to show an angry  face on the display. How do I know what number 15 00:01:20,560 --> 00:01:24,480 to pick for the magnetic force? How do I know  how strong my magnet is? Well, we're going to 16 00:01:24,480 --> 00:01:29,840 do something to sort that out. So, if I go  to 'Input', I drag in 'on button A pressed', 17 00:01:30,840 --> 00:01:37,200 and we'll add a 'show number' block. Let's copy  the magnetic force block if I right-click on it, 18 00:01:37,200 --> 00:01:43,480 duplicate, and drag it in here and drop it there.  What this means now is that when I press button A, 19 00:01:43,480 --> 00:01:48,000 I can measure the amount of magnetic force  measured by the micro:bit. I can see it on the display. 20 00:01:48,000 --> 00:01:53,560 So, we're ready to try this  out now. It doesn't work in the simulator, 21 00:01:53,560 --> 00:01:58,360 so we're going to put this on a real micro:bit.  So, I'm going to call it 'door alarm', 22 00:01:58,360 --> 00:02:04,520 and I'm going to download the hex file, and  transfer it to my micro:bit. I've fixed the 23 00:02:04,520 --> 00:02:09,200 micro:bit and the battery pack to the door frame  using something like Blu Tak and put the magnet 24 00:02:09,200 --> 00:02:13,800 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. 25 00:02:13,800 --> 00:02:18,960 You need to make sure the magnet  is close to the compass chip on the back of your micro:bit. 26 00:02:18,960 --> 00:02:26,840 Now we can calibrate it. With the door  closed, press button A and get a reading of 27 00:02:26,840 --> 00:02:35,320 the magnetic field strength. In my case, it's around  453 microTeslas. I'm then going to open the door, 28 00:02:35,320 --> 00:02:42,000 press button A again and take a reading with the  door open. It's 129 microTeslas now. 29 00:02:42,000 --> 00:02:46,080 It can still measure some magnetism, don't forget the Earth  is magnetic, and there are other things probably 30 00:02:46,080 --> 00:02:51,560 in my house that are magnetic as well, so the  micro:bit is still picking up some magnetic field. 31 00:02:51,560 --> 00:02:58,160 So my number of 200 should work pretty well. If  it's more than 200, it means the door is shut, 32 00:02:58,160 --> 00:03:04,000 it sounds likely. Because with the door shut  that's 453 microTeslas. Below 200, that probably 33 00:03:04,000 --> 00:03:08,600 means somebody has opened the door, in this  case I know when my door is open I have 129 microTeslas. 34 00:03:08,600 --> 00:03:13,840 So that should work OK now, I should  be able to tell when someone has opened my door, 35 00:03:13,840 --> 00:03:18,840 an angry face will appear on my micro:bit's  display, and I'll know that somebody has been in my room. 36 00:03:18,840 --> 00:03:23,080 There are lots of things you could do  to this project to augment it. You could perhaps 37 00:03:23,080 --> 00:03:27,920 have a delay to give yourself some time to get  out the room. Remember if you need to reset it, 38 00:03:27,920 --> 00:03:32,720 you could press button A to show the magnetic  force again and that will clear the angry face 39 00:03:32,720 --> 00:03:38,000 off the display of your micro:bit. Maybe you  could add an audible alarm too or 40 00:03:38,000 --> 00:03:44,960 you could add a music block so that when somebody opens  your door, it makes an audible sound to warn you 41 00:03:44,960 --> 00:03:51,920 that somebody has been inside your room. There are  all sorts of ways you could modify this project.