MISSION 1

Zombie Detector

Sometimes it’s difficult to tell whether someone is a zombie. Pale skin and a slow limp are obvious signs, but maybe the person is just not feeling well, or he or she has stubbed his or her toe? Follow this project to create an electronic test to see whether your family members are zombies or not.

To protect ourselves from zombies, we’re going to create a touch sensor because it’s a well-known fact that zombies don’t conduct electricity like humans do.

Algorithm

Let’s figure out what it is we’re doing in simple English:

Images

This seems easy enough! Let’s get building.

micro:bit

Build

The micro:bit can detect a conductive item (you) between a data pin and ground. To test the suspected zombie, ask him or her to hold the GND pin on the right of the micro:bit and the pin labeled 0 on the left. To get the tick, the suspected zombie has to let go of 0.

Images

Figure 1.1    Zombie!

Code

Go to the MakeCode website for the micro:bit (https://makecode.microbit.org/), and select New Project.

1.   Let’s keep things tidy: delete the blocks on start and forever.

2.   From the Input menu, drag out on pin P0 pressed.

3.   From the Basic menu, drag out show icon, and drop it inside on pin P0 pressed.

4.   Change the love heart icon to a tick icon by clicking on it and selecting the tick.

This block acts like a button pressed. A button is pressed when it is pressed down and then let go. To test with the micro:bit, you will need to get the suspected zombie to press and then let go of pin 0. The suspected zombie can keep hold of the GND pin the whole time.

5.   At the bottom of the screen, instead of Untitled, give your project a name.

6.   Download the code to the micro:bit, and test it out. (See the Introduction.)

Images

Figure 1.2   Pin pressed.

Images

Figure 1.3   Tick.

Debug

Now, here’s the problem: this code only works once, for one person. This mission is to test the whole family. We’re going to have a queue of suspicious people whom we will want to test!

Let’s clear the tick after a few seconds so that we can test again.

1.   From the Basic menu, drag out pause (ms) 100, and place it under the show icon.

2.   Change 100 to 3000. This is 3 seconds. (See the box “There Are 1,000 Milliseconds in a Second.”)

3.   From the Basic menu, click on the submenu More.

4.   Drag out clear screen.

5.   Add it under the pause (ms) 3000 block.

Now we can test more than one suspected zombie.

THERE ARE 1,000 MILLISECONDS IN A SECOND

There are 1,000 milliseconds in a second. Four seconds is 4,000 milliseconds. How many milliseconds in half a second?

Images

Figure 1.4   Debug.

Expert Level

Here’s a challenge for you to level up your skills. Until a human presses a button, show a cross.

We’re changing a lot about the project. Let’s create a new algorithm:

Images

Answers are found on this book’s website (savetheworld.mcunderwood.org)! Don’t peek. Try this challenge yourself.

Circuit Playground Express

Build

The Circuit Playground Express can detect when you touch some of its pins, such as A2.

Code

Go to the MakeCode website for Circuit Playground Express (http://adafruit.makecode.com), and select New Project to start coding. On the Circuit Playground Express, the touch-sensitive pins are A1 to A7.

1.   Let’s keep things tidy: delete the forever block.

2.   From the Input menu, drag out on button A click.

3.   Change button A to pin A2.

4.   Change click to down.

5.   From the Light menu, drag out set all pixels to, and place it inside on pin A2 click.

6.   Change the red circle to a green one.

Time to test your suspected zombie!

7.   At the bottom of the screen, instead of Untitled, give your project a name.

8.   Download the code to the Circuit Playground Express, and test it out. (See the Introduction.)

Debug

When testing my zombie sensor, I noticed that the green light stays on all the time. How do we know the suspected zombie has touched the pin? Let’s clear the lights after a few seconds so that we can test again.

Images

Figure 1.5   On pin.

Images

Figure 1.6   Set pixels.

1.   Add a pause 100 ms block from the Loops menu.

2.   Change 100 to 3000. This is 3 seconds. (See the box “There Are 1,000 Milliseconds in a Second.”)

3.   From the Light menu, scroll to the very bottom for the clear block.

4.   Add this under pause 3000 ms.

Images

Figure 1.7   Debug.

Time to test all the suspected zombies.

Expert Level

Here’s a challenge for you to level up your skills. Until a human presses a button, show red lights. We’re changing a lot about the project, so let’s create a new algorithm:

Images

Answers are on this book’s website (savetheworld.mcunderwood.org). Don’t peek. Try this challenge yourself.

Raspberry Pi

Build

None of the pins on the Raspberry Pi are touch sensitive. You need to add an extra piece of equipment to run this project on a Pi.

I used Pimoroni’s Touch pHAT. This is a cheap add-on board that sits on the Raspberry Pi pins and creates six capacitive touch buttons. You and/or your adult will need to solder a female header to the Touch pHAT to get it working with the Raspberry Pi. Pimoroni has some guides on soldering on its website (https://learn.pimoroni.com/tutorial/sandyj/soldering-phats). Note: When you add the hat to the Raspberry Pi, always turn the Pi off first.

Images

Figure 1.8   Raspberry Pi Touch pHAT.

Code

Download the Library

1.   Let’s start by downloading some libraries.

DOWNLOAD A LIBRARY

The Raspberry Pi doesn’t have all the Python code in the world installed and ready to go. Sometimes you have to go and download it from the internet.

Your Raspberry Pi needs to be connected to the internet to download the library. Install the Touch pHAT library from Pimoroni first.

2.   Open up a terminal by selecting the terminal icon on the Raspberry Pi home screen.

3.   Type:

curl https://get.pimoroni.com/touchphat | bash

and press ENTER. The line just before the word bash is tricky to find on your keyboard. On my keyboard, it’s to the left of the letter Z. You need to press SHIFT and that key to get that line to appear.

4.   I had to type Y and press ENTER twice to continue—once to turn on I2C, which is turned off by default, and again to install the Pimoroni examples and documentation.

Images

Figure 1.9    Terminal window.

5.   When step 4 has finished, in the terminal again type:

  pip3 install savetheworld

and press ENTER.

Images

Figure 1.10   Installing the second library.

This will install my library for this mission.

Code the Pi

1.   From the Raspberry Pi menu, select Programming and then Python 3.

2.   Select File and then New to open up a new file to type code in.

3.   Let’s start by importing some libraries.

LIBRARY

Python doesn’t have all the code it needs loaded all the time. Sometimes we need to tell Python to go fetch the code we need. We do this by typing “Import” and the name of the library. Earlier, we were downloading the libraries. Now we’re importing them into our code.

The library savetheworld touch has a function in it called touched. We can send this function the name of the button we want to know about. This function returns either True (the button was pressed) or False (the button was not pressed). We can also send the word Any to check on any of the buttons on the Touch pHAT.

Images

Figure 1.11   Code.

Images

Figure 1.12   Code.

Images

Figure 1.13   Running function.

Images

Figure 1.14   Code.

2.   Did you manage to get the program to display True? I didn’t! This code runs once and only once. Let’s put it in a forever loop and try again. You need to indent the last sentence so that it runs inside the while loop. Also watch out for the colon!

3.   Run this code, and press a button on the Touch pHAT.

Images

Figure 1.15   Forever running.

Success! The program spotted that I pressed some buttons on the Touch pHAT.

1.   Click on the Shell that’s displaying True and False, and press CTRL and C on the keyboard to stop the program from running. This will interrupt the program and show an error—but that’s fine for now.

We want to know when a button is pressed. We want to know when this function returns True.

2.   I’ve put what the function returns into a variable called phatStatus so that I can check phat status in my if statement.

Images

Figure 1.16   Force stop.

VARIABLES

A variable is somewhere we can store information. A great example of a variable is your score in a game. The score can be increased and decreased, we can see the score, and if you’re a big cheat, you can change the score altogether. The same goes with variables! Variables can also be different data types such as text and numbers.

There are several ways you could write this code. You could leave out phatStatus altogether and use:

if touch.touched("Any"):

The line if phatStatus: could be rewritten as if phatStatus is True:. I would suggest that you, as a beginner, use whatever makes the most sense to you.

Now you have a Raspberry Pi Zombie Detector. What message will you display instead of “Button pressed”?

Images

Figure 1.17   Checking the variable.

You could direct the zombie to only press button A; see if the zombie can follow instructions. Then use the code

touch.touched("A")

to check only the A button.

Debug

When you run the preceding code, it does work, but I get a lot of messages at once from a single button press. I think this isn’t how the program should work.

Put in a small pause after printing the message “button pressed.” This is just enough time for me to press my finger down and let go for a correct zombie test.

Images

Figure 1.18   Pause.

Expert Level

Here’s a challenge for you to level up your skills. Until a human presses a button, show a cross. If the suspect is a human, show a tick. Then clear the screen for the next suspect. We’re changing a lot about the project, so let’s create a new algorithm:

Images

Hint: Use the code from Mission 2 to draw graphics in Python. See the answer on this book’s website (savetheworld.mcunderwood.org).

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

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