Using Sensors with the Hummingbird (Snap!)

The Hummingbird LEDs and motors are output devices. Scripts in Snap! send commands to these devices to make something happen. The Hummingbird sensors, on the other hand, are input devices. They send information to the Snap! program. The Snap! program can use this information to make a decision with an if else block.

All of the sensors are attached to the Hummingbird in the same way. A sensor can be attached to any one of the four ports labeled “SENSORS.” Each sensor has a red wire, a black wire, and a yellow wire. The red wire should be connected to ‘+,’ the black to ‘-,’ and the yellow to ‘S.’

Distance Sensor

Attach a distance sensor to the Hummingbird. Drag the HB Distance CM block into the Scripts area away from the other blocks. Make sure that the port number is set to the port attached to the distance sensor. Click on this block to see the current value of the sensor. Then hold something in front of the sensor and click the block again. Is the value of the distance sensor different?

The distance sensor measures the distance to an object in centimeters. The value of the distance sensor is between about 8 cm and 100 cm. The sensor cannot detect objects very close to the sensor or very far from it. It works best in the range of 20 cm to 60 cm.

One way to use the distance sensor in a program is to use its value to create a Boolean block. You can do this using the two blocks shown below, which are found on the Operators menu.

Create the Boolean block shown below. This block is true if the value of the distance sensor is greater than 30 cm, and false otherwise. The value that the Boolean block uses to make a decision is called the threshold. In this case, the threshold is 30 cm.

What do you think the script below will do? After you have made a hypothesis, test it out.

Exercise: Write a program that blinks a red light and moves a servo motor when an object is close to the distance sensor. When nothing is near the distance sensor, the light should be off and the servo should not move.

Programming Tip: Don’t forget to set the port in the sensor block to the port on the Hummingbird board that you are actually using! If the value of the sensor is not changing as expected, be sure to check that the sensor block is reading the correct port.

Rotary Knob

The rotary knob is very similar to the distance sensor. You can use the Hummingbird Knob block to find the value of this sensor. This value will be between 0 and 100. Always remember to make sure that the Hummingbird Knob block is set to the port attached to the rotary knob.

Attach the knob to the Hummingbird board. Then turn the knob all the way to the left. What is the value of the Hummingbird Knob block? What is the value of the Hummingbird Knob block when the knob is turned all the way to the right?

Exercise: Write a program that moves a sprite to the left when the knob is turned toward the left. When the knob is turned toward the right, the sprite should move to the right. What will your threshold be for this program?

In the previous exercise, you used the rotary knob to decide whether the sprite should move left or right. Instead, you could use the knob to determine how far the sprite should move. Try out the block below for several different positions of the rotary knob.

Exercise: Write a script that makes a sprite move in a square. The position of the rotary knob should determine the size of the square.

Light Sensor

The light sensor is very similar to the other sensors that you have learned about. You can use the Hummingbird Light block to find the value of this sensor. This value will be between 0 and 100. Always remember to make sure that the Hummingbird Light block is set to the appropriate port.

Attach the light sensor to the Hummingbird board. Use the Hummingbird Light block to measure the amount of light in your classroom. Then measure the value of the light sensor when you cover it with your hand. The average of these two values is a good threshold for the light sensor.

Exercise: Write a script that vibrates a motor when the light sensor detects that it is dark. A sprite on the screen should also move back and forth as the motor vibrates.