Tải bản đầy đủ (.pdf) (10 trang)

Practical Arduino Cool Projects for Open Source Hardware- P9 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (341.21 KB, 10 trang )

CHAPTER 4  VIRTUAL USB KEYBOARD
As you can see from the sequence sent when BUTTON_MSG is asserted you aren’t limited to
sending one keypress event per input: you can also send sequences of characters.
Finally, the sketch defines its own delay function that can be used inside setup() while timer0 is
disabled.

void delayMs(unsigned int ms)
{
for (int i = 0; i < ms; i++) {
delayMicroseconds(1000);
}
}

One thing to note about the example sketch is that it doesn’t introduce any delays inside the main
program loop. It’s critical that the loop executes quickly so just about the only thing you can do inside
the loop is read digital inputs: if you try to do anything that will slow down the program, the host
computer may fail to get a response at a critical time and decide the device is misbehaving. If that
happens the host will de-enumerate the device and your fake keyboard will stop working.
Once you’ve compiled the sketch and uploaded it to your Arduino, open a new document in a text
editor or word processor on the host computer so it’s all ready for your Arduino to begin typing into it.
Then disconnect the USB lead from the normal USB socket on the Arduino board and plug it instead
into the additional USB socket on the prototyping shield. This will cause the Arduino to power down and
then power up again using power from the USB connection coming in via the shield. It will also attempt
to enumerate itself with the host computer as a Human Interface Device (HID) so if you’re curious about
what happens you could open the system log on your computer and watch it while plugging the USB
cable into the shield.
If all goes well your Arduino will now behave like an extra keyboard plugged into your computer, so
try pressing one of the buttons or connecting one of the inputs to ground to trigger a keypress event. You
should see corresponding characters appear in the text document on your computer—and, as far as it
knows, that’s just you typing the letters on a regular keyboard!
For a more complete list of available characters supported by the library, have a look in the library


header file (hardware/libraries/UsbKeyboard/UsbKeyboard.h) using a text editor.
For even more information have a look at the USB HID to PS/2 scan code translation table
document published by Microsoft at download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-
843a-923143f3456c/translate.pdf. Note that although the letters are defined using uppercase names
such as KEY_J for the letter j, the character sent through will actually be the lowercase version unless you
apply a modifier to the key. The modifier is sent through as a second parameter to the sendKeyStroke()
function, so to send an uppercase J you would call the following:

sendKeyStroke(KEY_J, MOD_SHIFT_LEFT);

The example program includes a couple of modifiers so you can see it in action.
Just for fun, we mapped the four buttons to characters used to control the game Frozen Bubble (left,
right, fire, and centre) so we could use an Arduino as a custom game controller (see Figure 4-5).

59
CHAPTER 4  VIRTUAL USB KEYBOARD

Figure 4-5. Using the virtual USB keyboard as a custom game controller
We also connected a Nintendo DS touch screen to another Arduino as described in the Touch
Control Panel project in Chapter 8 and ran a program that defined different regions on the screen for
left, right, and fire. We then linked the two Arduinos together with the digital outputs from the touch
screen Arduino connected to digital inputs on the USB keyboard Arduino to give touch screen control of
Frozen Bubble. There are videos of both systems in action on the Practical Arduino site.
60
CHAPTER 4  VIRTUAL USB KEYBOARD
61
Variations
Chording Keyboard
A chording keyboard is a special type of input device that has a very small number of buttons compared
to the number of characters it can send: rather than having one button for each letter of the alphabet like

a normal keyboard, a chording keyboard allows you to press a combination (“chord”) of keys at once to
select the letter you want. A five-button chording keyboard can be comfortably held in a one-handed
grip and allow the operator to type a huge number of different characters by simultaneously pressing
several keys, with the added advantage that it doesn’t need to be placed on a surface to be used: you can
even walk around with a chording keyboard in one hand and type while on the move.

C H A P T E R 5

  

PS/2 Keyboard or Mouse Input
Arduino isn’t limited to taking input from sensors: you can even connect a full-size PS/2 keyboard just as
if it were a “real” computer and type away! Connecting a keyboard to an Arduino may sound a bit odd
(after all, it’s just a little microcontroller), but keep in mind that an Arduino actually has more processing
power and memory than a complete desktop machine of not-so-many years ago. Adding some
peripherals that you would normally associate with a full-size computer opens up some interesting
possibilities.
Perhaps PS/2 seems a bit dated and you’d rather use a modern USB keyboard with your Arduino.
After all, even finding a PS/2 keyboard can be difficult these days, and you might have to go dumpster
diving or hunting through that pile of crusty old hardware in the company storeroom to find one. Using
a USB keyboard might sound like the obvious solution, but unfortunately an Arduino just isn’t up to the
job of acting as a USB host natively—it’s simply not fast enough. All is not lost, however, because many
USB keyboards are designed for backward compatibility and come with a little purple USB-to-PS/2
adapter that allows them to be plugged into a PS/2 port. Keyboards that come with those adapters are
designed to detect when they are plugged into a PS/2 port rather than a USB port and automatically
switch modes to become a PS/2 device, so this project should work just as well with a modern USB
keyboard connected via an adapter as it would with a genuine antique PS/2 keyboard.
Or if you want to go even more retro you can use this same circuit with an old-style AT keyboard,
because even though they use a different connector, they have the same electrical interface as a PS/2
keyboard.

Connecting a keyboard or mouse to an Arduino opens up a wide range of possibilities in terms of
non-human input, too. A hacked keyboard can provide you with 100+ digital inputs using only a few
Arduino I/O pins, and the X/Y encoders in a mouse are perfect for tracking movement of a robot.
The required parts are pictured in Figure 5-1, and the schematic is in Figure 5-2.
Parts Required
1 Arduino Duemilanove, Arduino Pro, Seeeduino, or equivalent
1 Prototyping shield
1 PS/2 extension cable or 6-pin mini-DIN socket
1 PS/2 keyboard or 1 PS/2 mouse
Hookup wire
Source code available from www.practicalarduino.com/projects/ps2-keyboard-or-mouse.

63
CHAPTER 5  PS/2 KEYBOARD OR MOUSE INPUT

Figure 5-1. Parts required for PS/2 keyboard or mouse connection to Arduino
Instructions
This project can be built to provide connections for just a keyboard, just a mouse, or both. We’ll step you
through the process of assembling connections for both but if you want to only fit the parts for one or
the other, that’s perfectly fine.
There are two options for providing the connections: either fitting a PS/2-style socket (generally
referred to as a “6-pin mini-DIN”) to the prototyping shield, or using a PS/2 extension cable and cutting
off one end so you can attach it to the shield as a fly-lead.
Note that if you want to go really retro you can even use an early AT-style keyboard fitted with a 5-
pin DIN connector. The difference between AT and PS/2 connectors is purely mechanical: both types of
keyboard use the exact same electrical specifications, so if you happen to have an old AT keyboard lying
around it should work just as well provided you use the correct socket or extension cable. You might find
they pull more current, though, so stick to a more modern PS/2 keyboard or a USB keyboard with a PS/2
adapter if possible.
64

CHAPTER 5  PS/2 KEYBOARD OR MOUSE INPUT

Figure 5-2. Schematic of PS/2 keyboard or mouse connection to Arduino
PS/2 Connections
PS/2 devices need connections for +5V power, ground, data, and clock, as shown in Figure 5-3.
Table 5-1 gives the specifications.
65
CHAPTER 5  PS/2 KEYBOARD OR MOUSE INPUT


Figure 5-3. PS/2 pinout in 6-pin mini-DIN plug and socket
Table 5-1. PS/2 pinout
Pin Purpose
1 DATA
2 unused
3 Ground
4 +5V
5 CLOCK
6 unused
Recycled 6-Pin Mini-DIN Sockets
Using a PCB-mount 6-pin mini-DIN socket will make your shield neater, but they have become
surprisingly hard to find. Some suppliers, such as SparkFun, have them available along with breakout
boards, but if you’re not averse to a bit of scavenging you might also be able to obtain them from an old
computer.
If you have an old computer motherboard lying around and some luck, patience, a hacksaw, and a
solder sucker, you might be able to remove the combination keyboard/mouse socket and repurpose it.
The shield shown in Figure 5-4 uses a double connector cut from a motherboard using a hacksaw. The
section of PCB attached to the socket was trimmed tightly against the edges of the socket case and left in
place, with the edges cleaned up so there are no slivers of metal to short things out. Removing the PCB
entirely would be very difficult and there’s not much reason to do so anyway, since all of the socket

connections are isolated from each other. It also conveniently links pin 3 (ground) on each socket to the
socket case, which is then attached to the shield’s ground connection on the other side, out of sight.

66
CHAPTER 5  PS/2 KEYBOARD OR MOUSE INPUT

Figure 5-4. Recycled PS/2 keyboard and mouse connector mounted on a prototyping shield
A small rectangle of PCB material was glued to the prototyping shield with two-part epoxy glue as a
spacer, and the socket assembly was then glued on top of that. The result is an extremely strong
mechanical mount for the socket that was then linked to the shield using short jumper leads.
6-Pin Mini-DIN Panel Sockets
If you don’t want to sacrifice a motherboard it may be necessary to use panel-mount sockets instead,
which unfortunately can be quite tricky to mount on a shield.
The only new 6-pin mini-DIN sockets we could get locally were panel-mount sockets with long
mounting tabs on the sides, so we cut the tabs cut off and placed the sockets sideways on the shield with
the edges overlapping the PCB. Make sure you mount the socket right near one edge of the shield so
there’s plenty of clearance for the lead to plug in without fouling the shield or the Arduino, and keep
them far enough apart that if you want to plug in both a keyboard and a mouse at the same time the
plugs won’t foul each other. That’s even more of an issue if you use USB-PS/2 adapters, of course, and
you may need to mount them on opposite ends of the board or mount them sideways so the wide part of
the adapters sit vertically.
We then inserted some bare single-core hookup wire (stripped from a bit of leftover Ethernet cable)
beside one of the sockets, soldered it to the shield, and threaded it over the socket and down the other
side. We pulled it tight using pliers and looped it around a couple of times before soldering it all in place,
then put dobs of two-part epoxy on top to provide lots of mechanical support. The result is very strongly
mounted sockets that definitely won’t be ripped off the shield.
67
CHAPTER 5  PS/2 KEYBOARD OR MOUSE INPUT
On our board we fitted two sockets: one for a keyboard, one for a mouse. If you can get sockets that
are color-coded green for a mouse and purple for a keyboard, that’s perfect, but if not just use whatever

is available and mark them so you know which is which. We used plain sockets and put colored markers
on the shield using pieces cut from a sticker intended to go on the back of a computer case.
The photograph in Figure 5-5 shows our board with two sockets glued in place. On this particular
board the sockets are mounted at the same end as the USB port (the left end when looking at an Arduino
with the labeling oriented correctly) and wouldn’t clear the large USB socket on a typical Arduino. In this
case, though, it’s being assembled to fit an Arduino Pro from SparkFun, so there’ll be plenty of room to
clear the 6-pin USB header.


Figure 5-5. Panel mount 6-pin mini-DIN sockets attached to a shield with looped wire and epoxy
Each socket needs to be connected to ground, +5V, and a pair of Arduino digital I/O lines. Start by
using short lengths of hookup wire to connect the power lines.
At this point you should fit the shield on your Arduino, power it up, and use a multimeter to
measure that you have ground and +5V on the appropriate holes in the socket. Most keyboards are
pretty robust but it’s better not to feed them power the wrong way if you can help it.
The keyboard socket needs to be wired up with the CLOCK pin connected to digital I/O line 3, and
the DATA pin connected to digital I/O pin 4. You can actually change the DATA pin to something other
than 4 if you prefer, but the CLOCK pin absolutely must be connected to line 3 because the software
we’ll be using in a moment needs to use the interrupt on that pin to manage communications with the
keyboard.
Likewise, use short lengths of hookup wire from the mouse socket to connect the CLOCK pin to
digital I/O line 6, and the DATA pin to digital I/O line 5. The mouse driver library is less fussy, so both
these pins can be remapped to something else if you need those particular pins for another device in
your project.
68

×