Snap! 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
Approx. Time: 25 Minutes
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.
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 "Control" 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" category 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 "Sensing" and place a distance sensor block (in cm) to the left of the < sign.
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 "Looks" category) to the "if" 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 from the "Motion" category 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.
1. This takes care of the Hummingbird's outputs, but Snap! 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 Turtle is the default costume. Click the paint brush icon to create a new costume. You can also upload pictures from your computer.
3. Make the first screen for when the sensor does not detect anything.
4. Name the costume by right-clicking its icon and selecting "rename". We chose "nothingDetected" as the name.
5. Click the paint brush and design another costume for when the warning light is on.
6. Finally, make a costume for when the red LED is lit.
7. Now go back to the "Scripts" tab and select "Looks". Then use three "switch costume to" blocks to set the correct screen.
8. Finally, we can use two speak blocks from the "Sound" 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.
9. Turn up your computer's volume, press the spacebar, and try it out!
This file was created with the Birdbrain Robot Server. Use this utility to convert it to a form that can be used with the Chrome Finch connection app.