Scratch Project 2: Burglar Alarm

Description: Make the Hummingbird flash lights and vibrate when someone gets too close

Programming Concepts: using if statements, reading sensor values, displaying graphics

Materials: Hummingbird Controller, Red LED, Yellow LED, Vibration Motor, Distance Sensor

Difficulty: Beginner

Approx. Time: 25 Minutes

Pictures:

Video:

Hummingbird setup

1. Connect the yellow LED to port 1 and the red LED to port 2.

2. Connect the vibration motor to port 1.

3. Connect the distance sensor to port 1.

4. Plug your Hummingbird into a wall socket using the power adaptor.

5. Connect the Hummingbird to your computer to begin programming.

Programming steps

1. Let's start with a "when space key pressed" block to launch our program. Drag the block to the middle of the screen. (It is in the "Events" category.)

2. We need the Hummingbird to repeatedly check the distance sensor. To do this, we will use a loop. Attach a forever loop from the "Control" section to the when key pressed block.

Now let's use the distance sensor to detect when someone is getting close. If they are close, we want to turn the red light on. To perform this check, we will need to use an if statement.

3. Click "Control" and place an if...else... statement inside of the loop.

4. We want the light to turn on if the distance is less than a certain amount. Click "Operators" and insert the "<" block into the slot after the word "if".

5. Now we need to read the distance sensor. Click "More Blocks" and place a distance sensor block to the left of the < sign. Select port 1 from the dropdown menu.

6. The distance sensor indicates how many centimeters away an object is. Click the white box to the right of the < and type 12.

7. Add an LED block (in the "More Blocks" section) to the "if...then" section of the if block. Set its intensity to 100 and port to 2. This will turn on the red LED if someone is closer than 12 centimeters.

8. Now we want to make the yellow "warning light" turn on if someone is within 30 centimeters. Drag another if...else... block into the else section of the initial if statement.

9. Right-click the green "<" block and click "duplicate". Connect the copied blocks to the new if statement.

10. Change the number 12 to 30 in this new set of blocks.

11. Duplicate the LED block and place it in the if section. Then set the LED to port 1 (the yellow LED).

12. To turn off both lights when nothing is within 30 centimeters from the distance sensor, add two LED blocks to the else section that set the intensities of the LEDs on ports 1 and 2 to 0.

13. Run the code by pressing your space bar. The yellow light should turn on when you get closer to the sensor and both lights should turn on when you get even closer. Click the stop button when you are done.

14. Add two vibration motor blocks to make the Hummingbird vibrate when someone is too close. They go in the first if section and the else section. Use intensity 100 for on and 0 for off.

Graphics

1. This takes care of the Hummingbird's outputs, but Scratch also lets us display images on the screen. To take advantage of this, click the "Costumes" tab.

2. Costumes change the appearance of the "sprites" on your screen. The Scratch Cat is the default sprite. Click the paint brush icon to create a new costume. Then delete the other costumes by right-clicking them and selecting "delete".

3. Click the "Convert to vector" button in the bottom-right corner of the screen. This will make it easier to create and edit shapes and text.

4. Make the first screen for when the sensor does not detect anything.

5. Name the costume in the top-left corner of the screen. We chose "nothingDetected" as the name.

6. Click the paint brush and design another costume for when the warning light is on.

7. Finally, make a costume for when the red LED is lit.

8. Now go back to the "Scripts" tab and select "Looks". Then use three "switch costume to" blocks to set the correct screen.

9. Finally, we can use two speak blocks from the "More Blocks" category to alert us when an intruder is detected. Be sure to add a wait 1 second block from the "Control" category after each speak block to give the computer time to finish speaking.

10. Turn up your computer's volume, press the spacebar, and try it out!

Example file

example-file-2-burglar-alarm.sb2