Snap! Burglar Alarm
Make the Hummingbird flash lights and vibrate when someone gets too close!
Objective & Learning Goals
- Use if statements
- Read sensor values
- Display graphics
- Connect the yellow LED to port 1 and the red LED to port 2.
- Connect the vibration motor to port 1.
- Connect the distance sensor to port 1.
- Plug your Hummingbird into a wall socket using the power adaptor.
- Connect the Hummingbird to your computer to begin programming.
- 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.)
- 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.
- Click “Control” and place an if…else… statement inside of the loop.
- 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”.
- Now we need to read the distance sensor. Click “Sensing” and place a distance sensor block (in cm) to the left of the < sign.
- The distance sensor indicates how many centimeters away an object is. Click the white box to the right of the < and type 12.
- 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.
- 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.
- Right-click the green “<” block and click “duplicate”. Connect the copied blocks to the new if statement.
- Change the number 12 to 30 in this new set of blocks.
- Duplicate the LED block and place it in the if section. Then set the LED to port 1 (the yellow LED).
- 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.
- 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.
- 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.
- 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.
- 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.
- Make the first screen for when the sensor does not detect anything.
- Name the costume by right-clicking its icon and selecting “rename”. We chose “nothingDetected” as the name.
- Click the paint brush and design another costume for when the warning light is on.
- Finally, make a costume for when the red LED is lit.
- Now go back to the “Scripts” tab and select “Looks”. Then use three “switch costume to” blocks to set the correct screen.
- 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.
- Turn up your computer’s volume, press the spacebar, and try it out!