Research & Learning
Other Class pages
Mouse Control With Joystick
In this lab, you'll build an alternative computer mouse using an Arduino Leonardo using a joystick to move the mouse left, right, up and down. You'll use the joystick's select button to replace the mouse button as well. You'll see how to scale the analog outputs of the joystick to a reasonable range using the
A joystick is typically made up of two potentiometers, one for the X axis and one for the Y axis. The potentiometers are mounted so that when the joystick is at rest in the center, the potentiometers are at the middle of their range. Some joysticks like the Thumb Joystsick used here also have a pushbutton that you can press by pushing down on the stick. (Note: SparkFun and Parallax have equivalent models as well.)
(:toc Table of Contents:)
For this lab you will need the following parts:
Click on any image for a larger view
NOTE: The sketches contained in this lab will cause the Arduino Leonardo to take control of your mouse. Make sure they're working properly before you add the mouse commands. The example doesn't introduce the mouse commands until the end of the lab. Instead, messages are printed to the serial monitor to tell you what should happen. When you've run this and seen the serial messages occurring when you think they should, then you can add the mouse commands safely.
The sketches here will work on an Uno until you add the mouse commands. So you can test this on an Uno simply by commenting out any line that says Mouse.begin() or Mouse.move().
Prepare the breadboard
Connect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V and any of the ground connections:
Add a pushbutton
Attach a pushbutton to digital pin 2. Connect one side of the pushbutton to 5 volts, and the other side of the pushbutton to a 10-kilohm resistor. Connect the other end of the resistor to ground. Connect the junction where the pushbutton and the resistor meet to digital pin 2. (For more on this digital input circuit,see the Digital Input Lab)
Add a thumb joystick
Add a thumb joystick, attaching the Xout to analog input 0, the Yout to analog input 1, and the select button to digital input 3.
Program the module to read the pushbutton
Follow the same steps as you did in the first Mouse Control lab to read when the pushbutton on pin 2 is pressed. Your code should only print out a message when the button changes state. Similarly, set up a global variable to track whether or not you're controlling the mouse, called mouseIsActive. Each time the pushbutton on pin 2 is pressed, change the state of this variable from false to true, just like you did in the first mouse control lab.
Program the Leonardo to read the Joystick
Add code to the main loop to read the joystick X and Y outputs and print them
Map the X and Y output readings
The Mouse.move() command has three parameters: the horizontal movement, the vertical movement, and the scroll wheel movement. All movements are relative, so
In order to do this, you need to scale the X and Y outputs from the default range that they return (0 to 1023) to a range from -5 to 5. You can do this using the
So, if your input range is 0 to 1023, and your output range is -5 to 5, you might map like this:
Add code to the main loop to map the X and Y outputs to a range from -5 to 5. Print the mapped values instead of the original sensor values.
NOTE: If your joystick defaults to -1 at rest on one axis or both, try adding 1 to the result of the map command. Try different output ranges and see what they do.
When you run this sketch, you should see the Mouse Control State message once every time you press the first pushbutton, and thevalues of the X and Y axes of the joystick, mapped to a range of -5 to 5. You still need to add in the select button on the joystick, however.
Add code to listen for the Joystick Select Button
The joystick select button is a digital input, but it's wired differently than the buttons you saw in the Digital Lab or the Mouse Control With Pushbuttons Lab. It's wired to connect to ground when you press it. To read it, then, you'd still use
The Arduino has built-in pullup resistors that you can use on the digital inputs. When you set the pin to be an input using the
The advantage of connecting your switch to ground instead of 5V is that you can use the internal pullup resistor instead of having to add an external resistor. The disadvantage is that your switch logic is inverted: HIGH means the switch is open, and LOW means it is closed. Most of the time you can wire your switches either way, but when you have a switch that's already wired to ground like the select button in the joystick, you have to use the internal pullups and inverted logic.
Previously, you wired the select button to digital input 3. To read the select button in this sketch, add a pinMode command to the setup that makes it an INPUT_PULLUP. Then in the main loop, check if the mosueIsActive variable is true. If it is, then use
The select button's behavior should be like the mouse control pushbutton's behavior: you only want something to happen when it changes. When the select button changes from off to on, you want it to perform a mouse click. When it changes from on to off, you want it to perform a mouse release. So you need to check for when the select button changes, just like you do the other pushbutton.
To check for the select button to change, set up a global variable called
This block of code will look a lot like the code you used to track the state change of the pushbutton on pin 2.
When you run this code, you should see the words "mouse pressed" once when you press the select button, and "mouse released once when you release the select button. If it prints continually, you have an error.
When you've got that working, you're ready to take control of the mouse.
Add commands to control the mouse
NOTE: The sketches contained in this lab will cause the Arduino Leonardo to take control of your mouse. Make sure they're working properly before you add these commands. If your sketch causes your mouse to misbehave, upload a blank sketch (the default when you choose File -> New) to your Leonardo and you can start again from the beginning.
The Mouse.begin() command is called in the setup. It initializes mouse control from your Leonardo.
Modify the setup by adding the command Mouse.begin(). Then, in the loop where check if
That's the whole sketch. When you run this, press the mouse control pushbutton, then move the joystick and press the select button. You should have full mouse control.
In this sketch, you can see the value of scaling an analog sensor's readings to match the output range you need. Since the joystick potentiometers are at rest in the middle of their range, scaling them from -5 to 5 gives you easy control of the mouse movement, which is relative. You can also see how reading the state change of a digital input is important. You're doing it for two different buttons in this sketch.