Subjects

Maker

Project Type

Interactive Games

Required Tutorials

Rotation servo & Joystick

Estimated Time

Medium (3-6 hours)

Using the Hummingbird Joystick
Description

Learn how to use the joystick to control your Hummingbird.

Free Teacher Materials

Get Access

Project Map

Learning Goals

Student will learn to use the Hummingbird Joystick to create a game.

Materials

Tips

Steps

Sensors enable you to create a robot that interacts with people or its environment. You can make robots that respond to light, sound, etc. To expand your creative options even further, there is now a joystick that you can use with the Hummingbird. You can use it to create your own games or control a robot!


Connecting to the Joystick

The joystick is a combination of three sensors. First, the entire joystick can be pressed as button. This wire carrying this information is blue and labelled ‘B’ on the joystick board. Another sensor measures horizontal movement of the joystick (H: yellow wire), and the third sensor measures vertical movement of the joystick (V: green wire). The joystick also has wires for power (+: red wire) and ground (-: black wire).

Connecting the joystick to the Hummingbird requires three sensor ports. Power (+) and ground (-) should be connected to the ‘+’ and ‘-’ terminals of one sensor port. In the picture below, they are connected to sensor port 1. The other three wires from the joystick should be connected to the ‘S’ terminals of three different sensor ports. In the picture below, the button is connected to sensor port 1, the horizontal sensor to sensor port 2, and the vertical sensor to sensor port 3.

Controlling a Sprite in Snap!

To find the value of each of the joystick sensors in Snap!, you can use the Hummingbird Sensor Block at the bottom of the Sensing menu. Select ‘Other (V)’ from the drop down menu and make sure to select the correct sensor port for the button (port 1), the horizontal sensor (port 2), or the vertical sensor (port 3).

 

The voltage for each sensor varies between 0 and 3 V. The voltage for the button is close to 3 V when the button is pressed, and close to 0 V otherwise. The script below uses a threshold of 1.5 V to determine whether or not the button is being pressed. When it is, the sprite on the screen says, “Button pressed.”

The joystick can be used to move a sprite in Snap! to make a game. The horizontal and vertical position of the joystick can control the horizontal and vertical position of the sprite on the screen, respectively. To control the horizontal position of the sprite, the 0 to 3 V range of the horizontal sensor must be converted to the -240 to 240 range of the stage x-axis.

First, we subtract the value of the horizontal sensor from 1.5. The range for this block is -1.5 to 1.5.

Next, we multiply the difference by 160 to convert the -1.5 to 1.5 range to -240 to 240. This scaled value can be used to control the x-position of the sprite. In this script, the scaled value is stored in the variable xposition.

A similar process can be used to control the y-position of the sprite. The order of the subtraction is different to make up and down movement of the joystick correspond properly to up and down movement of the sprite.

Once you can use the joystick to control a sprite, you can create whatever game you want. One example is given in the Joystick Game code (see Teacher Materials). In this game, the joystick controls an octopus sprite. The horizontal and vertical positions of the joystick control the position of the sprite on the screen, and the button makes the octopus shoot bubbles. The goal of the game is to score points by avoiding the shark and shooting it with bubbles.

Controlling a Robot

The joystick can also be used to control Hummingbird outputs, such as rotation servos or LEDs. In this example, we chose to motorize an Etch-A-Sketch and control it with a joystick. Two motors were connected to the Hummingbird Duo board. Each motor had a gear attached to it. These gears were coupled to gears glued to the knobs on a small Etch-A-Sketch. Although this example was made with Hummingbird Duo motors, you can achieve a similar result using Hummingbird Bit rotation servos.

The script below can be used to control an Etch-A-Sketch robot. This code looks very similar to the script used to control a sprite with the joystick. The main differences are the scaling factors used to convert the range of the joystick to an appropriate range of rotation servo speed. In the script below, the scaling factors are both equal to 30. This script maps the 0 to 3 V range of the horizontal and vertical joystick sensors to a motor speed range of -45 to 45. What scaling factor would you use to get a motor speed range of -100 to 100?

Standards Alignment

COMPUTER SCIENCE TEACHERS’ ASSOCIATION (CSTA)

CSTA Standards are split into different grade levels: 3-5, 6-8, and 9-10. Working with the Hummingbird Robotics Kit meets multiple standards across these grade-level delegations.
Visit this page for a more detailed explanation of how working with the Hummingbird Robotics Kit applies to meeting CSTA Standards.

Back to Top