MISSION 14

Treasure Box Alarm

Like all normal adults, I have a treasure box full of valuables in my room. I need an alarm to keep my treasure safe, but all my previous alarms won’t work.

My treasure box is so full that the lid won’t close. This means that I can’t use the tinfoil alarm or the brightness alarm. My treasure box is also really heavy. No one is going to move it. A thief is just going to open the lid and take my treasure. I can’t use the compass alarm.

What I can use is a movement alarm. If the lid is lifted up, it can be sensed and set off an alarm.

Algorithm

Images

micro:bit

Build

I’ve taped my micro:bit inside the treasure box lid so that to the outside world it’s invisible. The battery is also taped inside.

Code

Find Z

Oh yes, it’s science time! The micro:bit measures acceleration using three axes: X, Y, and Z. If the micro:bit is lying flat on the table, X and Y should be 0, and Z will be around -1,000. This is because gravity is pulling on the micro:bit. If you lift up the front of the micro:bit, Z and Y will change. If you pick up one of the sides, Z and X will change. Z is the only point that always changes with movement. Let’s measure Z.

Images

Figure 14.1    Measuring inside the lid.

Images

Images

Figure 14.2    Test for Z.

We need to know what Z is when the lid is closed. Let’s record Z in a variable that we can look at later.

1.   Download this code to the micro:bit, and place it inside the treasure box lid.

2.   Press A, and then close the lid.

3.   Wait a few seconds, and open the lid.

4.   Press B to show the value of Z when the lid was closed.

5.   Repeat the experiment when the lid is open.

My micro:bit showed 1,024 when the lid was closed, and it showed -1,008 when the lid was fully open. That’s a huge range.

Let’s see what Z is when the lid is halfway open. Press A, half close the lid, keep it steady for 5 seconds, wait a bit longer, and then open it and press B. I got 992.

 

Fully open: -1,008

Fully closed: 1,024

Halfway open: 992

Checking Z

If I say if Z is less than 1,000, that should catch anyone opening the treasure box, even the tiniest bit.

Images

Figure 14.3    Checking for thieves.

Let’s add a speaker and another micro:bit alarm.

Images

Figure 14.4    Radio and music.

And the second micro:bit can alert us to our treasure being touched. Remember, this code is in Mission 12 (Figure 12.6).

Debug

I left a bug in the preceding code on purpose. Honest, I did. The radio group on the micro:bit inside the treasure box is 1. The radio group on the micro:bit outside is 33. These micro:bits will not talk to each other! To do that, they need to be in the same group.

My debugging on this mission all happened in the build stage. It took me several attempts to get the range right. Sometimes you have to tweak your code, download, test, tweak again, download, and test again to get it working in live conditions. This can be a slow process.

Expert Level

How many times a day is your treasure box opened? Record an opening in a variable, and display that variable on a button press

Circuit Playground Express

Build

I’ve taped the Circuit Playground Express inside the treasure lid so that it’s invisible to the outside world. The battery is also taped inside.

Code

Find Z

Time for some experiments! The Circuit Playground Express measures acceleration using three axes: X, Y, and Z. If the Circuit Playground Express is lying flat, X and Y should be 0, and Z will be around -1,000. This is because gravity is pulling on the Circuit Playground Express. If you lift up the front of the Circuit Playground Express, Z and Y will change. If you pick up one of the sides, Z and X will change. Z is the one axis that changes any time there’s movement. Let’s measure Z.

We need to know what Z is when the lid is closed. Let’s record Z in a variable that we can look at later.

Images

Figure 14.5    Test for Z.

1.   Download this code to the Circuit Playground Express, and place it inside the treasure box lid.

2.   Press A, and then close the lid.

3.   Wait a few seconds, and open the lid.

4.   Press B to get the value of Z when the lid was closed.

5.   Repeat the experiment when the lid is open.

My Circuit Playground Express showed all ten lights on when the lid was closed. Each light represents 102.4. My Circuit Playground Express showed four lights on when the lid was fully open. Four lights means 102.4 × 4 = 409.6.

 

Fully closed = 1,024

Fully open = 409.6

Checking Z

If I say if Z is less than 900, that should catch anyone opening the treasure box, even the tiniest bit.

Images

Figure 14.6    Checking for thieves.

Let’s add a speaker and another Circuit Playground Express alarm.

Images

Figure 14.7    Radio and music.

And the second Circuit Playground Express can alert us to our treasure being touched. Remember that this code is in Mission 12 (Figure 12.12).

Debug

The alarm sounds on the treasure box and then sends a message to the other Circuit Playground Express. You might want to send the message first, because it takes a few seconds for the siren to finish playing.

When you’re coding projects that you also have to build, it’s difficult to know where the bug is. It took me several attempts to get the number 900 right. I had it set too low and then too high! I had to change the code, download it, and put the Circuit Playground Express back in the treasure box. This was tiresome, but now it works.

Expert Level

Like the door alarm, record how many times a day your treasure box has been opened in a variable. Display that variable on a button press. When you leave the house in the morning, open the treasure box so that the number will be 1. When you get home, is the number 2 or is it 1?? If it’s 1, someone opened the treasure box and then reset the Circuit Playground Express to try to get away with it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.56.28