MISSION 10

Cookie Jar Protector

Cookies are important; cookies are precious. Let’s create an alarm to keep them safe.

You could use the same code and setup from Mission 8 here. What I find, though, is that people spot the tinfoil and know that the alarm is there. In this mission, we create an alarm that’s invisible until it’s too late, and the perps are caught!

I keep my cookies in a solid jar, none of that see-through nonsense. “Out of sight, out of mind” is my motto when it comes to guarding cookies. This means that when the cookies are safe, it’s dark. When someone has opened the cookie lid, it’s bright. We’re also going to add an extra level of security with each device: a compass sensor for the micro:bit and a sound level sensor for the Circuit Playground Express.

This is how we’ll create our alarm.

Algorithm

Images

OR

In Mission 6, we used an AND statement in our algorithm. We were making sure that both button A and button B were not pressed. This time we’re using an OR statement. If the light is bright OR the compass is not pointing north/sound is detected, then sound the alarm. We don’t want both to have to happen for the alarm to go off. Just one. That’s a really important difference between AND and OR.

Build

Find a cookie jar that is wide enough and deep enough for:

Images   Your cookies

Images   The micro:bit or the Circuit Playground Express and a battery pack

Images   Crocodile clips and a set of headphones

The device needs to go on top of the cookies so that the light sensor won’t be obscured. The jar cannot be see-through!

Images

Figure 10.1    Cookie jar ready to go.

micro:bit

Code

How Bright Is Bright?

You know what you need to do here: science! We need to know what brightness is to the micro:bit light sensor. Let’s show it forever so that when we open the jar, it will change from darkness to bright as we watch the micro:bit (and not the delicious cookies).

Download this code, and place the micro:bit in your jar with the battery attached. Make sure that the micro:bit’s lights are facing up, because these are the light sensors. Close the lid. Wait. Open the lid. Wait. What’s the number?

My cookie jar is quite deep, so even with the lid off, it’s not very bright in there. For my cookie jar, anything above 1 means brightness is detected. If your cookie jar is slightly see-through, your number might be higher than 1.

Test for Brightness

Images

Figure 10.2    What’s your brightness number?

1.   Create your selection code inside a forever loop.

2.   Remember, we need to pause after the melody to allow it to play. This is going to keep playing until the lid is put back on.

Images

Figure 10.3    What melody will you choose?

Test for Movement

Now let’s add our second level of security. Cookie thieves don’t always operate in daylight. Sometimes they sneak around in the dark with the lights turned off. Our second level of security will involve the compass that is built into the micro:bit.

Images

1.   Again, we need to do some science! The micro:bit in my cookie jar is going to point between 300 and 360 degrees. That means that it will be roughly pointing north/northwest. If it’s not pointing toward the north, someone has moved it!

2.   Create and download this code to find which way is north from where your cookie jar normally sits. You’ll need to place the micro:bit facing in the same direction in the jar every time.

3.   Place the micro:bit down flat, and wait a few seconds before you take the reading. Make sure that you’re not near something metal, like the fridge, that could throw off the compass.

Images

Figure 10.4    Getting the compass heading.

Note: You might have to calibrate the compass before it will give you a reading. On my micro:bit, it displayed a message “TILT TO FILL SCREEN” before I could continue. I had to tilt the micro:bit until all the dots were filled in. This will happen every time you power up the micro:bit and try to use the compass.

Images

Figure 10.5    Checking compass movement.

4.   Let’s code this separate from the light code.

5.   I’ve also added a compass display on the A button so that I can place the micro:bit in the right spot.

6.   Download the code, and test it. How much do you need to move the cookie jar before the alarm goes off? Do you need to change the number 300?

Double Security

1.   We’ve got our two tests, so let’s combine them. Now we want the alarm to go off if the lid is opened or the jar is moved. Not both. We need an or block between our two tests.

2.   Now place your selections inside the diamonds.

Images

Figure 10.6    OR block.

Images

Figure 10.7    Full code.

Debug

As always, we’re missing our clear block!

Images

Figure 10.8    Clear screen.

If you use my numbers above, you might find that this sensor doesn’t work for your cookie jar in your location. You need to do the experiments and work out the best numbers for your jar in your location. This can be frustrating, especially if you’re working in low light.

Expert Level

In Mission 11, we will add a number lock to the cookie jar to turn off the alarm. There’s also the radio alarm in Mission 12. Separate from these, you could add more sensors. The alarm will go off if any of these conditions are true:

Images   If the compass heading is less than 300 degrees

Images   If the brightness is greater than 1

Images   If the cookie jar is tilted left or right

Images   If the lid is opened (use the tinfoil from Mission 8)

It will be the most secure cookie jar in the world!

Circuit Playground Express

Code

How Bright Is Bright?

You know what you need to do here: science! We need to know what brightness is to the Circuit Playground Express light sensor.

1.   In the Light menu, there’s a graph block that lets us show the light level in lights.

2.   The light level block came from the Input menu.

3.   Download the code, and place the Circuit Playground Express in the cookie jar. Press the A button and wait.

4.   Run the experiment again. This time put the lid on quickly after you press A.

Images

Figure 10.9    Science!

Here are my results on a bright sunny day in Yorkshire:

Images

Zero is not a great number to work from. I’m going to rerun the experiment with the graph set to 50.

Images

Test for Brightness

Images

Figure 10.10    What will your alarm sound like?

Test for Brightness

Now we’re ready to create the light sensor code. Create your selection code inside a forever loop.

Test for Laughing

Now let’s add our second level of security. Thieves like to cackle loudly as they steal my cookies. Let’s use the sound sensor on the Circuit Playground Express to catch them.

Again, we need to do some science! How loud is a cackling laugh? Clear the code for the light level and test the sound level on its own. It's a good idea to test these separately so the data doesn't interfere with each other. Let’s graph it out of 255.

Images

Figure 10.11    How loud is a cackling laugh?

This is what I got testing with my own evil laugh:

Images

Images

Figure 10.12    Checking for laughter.

1.   Let’s code this separate from the light code.

2.   Download the code, and test it. How loud do you need to laugh before the alarm goes off? Do you need to change the number 150?

Double Security

We’ve got our two tests, so let’s combine them. Now we want the alarm to go off if the lid is opened OR a laugh is sensed. Not both. We need an OR block between our two tests.

Now place your selections inside the diamonds.

Images

Figure 10.13    OR block.

Images

Figure 10.14    Full code.

Debug

As always, we’re missing our clear block!

Images

Figure 10.15    Clear screen.

If you use my numbers above, you might find that these security features don’t work for your cookie jar in your location. You need to do the science and work out the best numbers for your situation.

Expert Level

In Mission 11, we will add a number lock to the cookie jar to turn off the alarm. There’s also the radio alarm in Mission 12. Separate from these, you could add more sensors. The alarm will go off if any of these conditions are true:

Images   If the brightness is greater than 1

Images   If the cookie jar is tilted left or right

Images   If the lid is opened (use the tinfoil from Mission 8)

They’ll be the most secure cookies in the world!

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

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