Research & Learning
Other Class pages
Mouse Control With Pushbuttons
In this lab, you'll build an alternative computer mouse using an Arduino Leonardo using pushbuttons to move the mouse left, right, up and down. You'll see the difference between reading a digital input continually and reading for a change of state.
(: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 four more pushbttons
Repeat the last step, connecting four more pushbuttons to pins 3 through 6.
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.
Activate the other four buttons
The other four pushbuttons are intended to control the mouse movement: the button on pin 3 should move the mouse right; the one on pin 4 should move it left; the one on pin 5 should move it down; and the one on pin 6 should move it up. If the user holds the button down, the mouse should continue to move in the corresponding direction.
To make this happen, you need to read the buttons' states. You don't need to tell when they change from off to on in this case. You just need to know that they are on. But you shouldn't do anything unless the mouseIsActive variable is true.
Add code to the setup to set pins 3 through 6 to input using the pinMode command. Then add code to the loop to read whether the mouseIsActive variable is true. If it is, read the four other pushbuttons. For each one, check if it's high, and if so, print the letter of the direction you're moving, U, R, L, or D.
When you run this sketch, you should see the Mouse Control State message once every time you press the first pushbutton, and the letters R, L, U, or D continuously when you hold down any of the other four pushbuttons. 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.
The Mouse.move() command has three parameters: the horizontal movement, the vertical movement, and the scroll wheel movement. All movements are relative, so
Modify the setup by adding the command Mouse.begin(). Then, in the loop where you print L, R, U, or D add Mouse.move commands to move as needed.
That's the whole sketch. When you run this, press the button to enable or disable mouse control, then press any of the other four buttons, and the mouse will move in the appropriate direction.
You can see from this sketch the difference between reading a digital input to look for the state change (the button on pin 2) and reading a digital input just to see the current state (buttons on pins 3 through 6). When you're designing a response that can repeat infinitely, you can just read the current state. But when you're designing a response that should happen just once, you need to determine when the button changes state.
Next, try the Mouse Control With Joystick lab.