
*
Note for Windows users
: The first time you upload a sketch, it may fail
and give you an error. On top of that Windows will pop up that familiar
‘Device driver software was not successfully installed’ notification. Don’t let
this worry you too much. If you get the error, wait about a minute, and try
uploading again.
Hopefully the upload will succeed the second time, but if it continues to fail,
check out the how to enter the bootloader section of the FAQ. Windows
needs to install the same driver we’ve already installed for the Pro Micro’s
bootloader, but it’s unable to get everything set up before the bootloader
exits.
Example 2: HID Mouse and Keyboard
By far, the Pro Micro’s most revolutionary feature (as far as Arduinos go) is
its true USB functionality. The Pro Micro can be programmed to emulate
any USB device you could imagine. You can even program it to act just like
a mouse, keyboard, or other HID-class USB device.
What is
HID
? It’s one of the many defined USB device classes. Every USB
device is assigned a class, which defines what its general purpose is. There
are loads of classes – printers, hubs, speakers, and webcams to mention a
few, but in this example we’ll be emulating HID –
Human Interface Device
.
The ATmega32U4 takes care of the USB-hardware hurdle, but we’ve still
got to clear the firmware one. Time for some example code!
USB Keyboards Made Simple
To emulate a USB keyboard, we’ll be making use of the
Keyboard
class.
Here are some of the functions made available to us by this class:
•
Keyboard.write(char)
- This function will send a single character
over USB. The character passed can be any standard, printable,
ASCII-defined character: 0-9, a-z, A-Z, space, symbols, etc. Here’s
an example line of code:
Keyboard.write('z')
//
This
will
send
a
single
'z'
character
to
your
computer.
•
Keyboard.print(string)
- If you need to perform a series a
Keyboard.write()
’s, consider using just a single
Keyboard.print()
. This works similar to
Serial.print()
– give it a
string of characters and it’ll send that stream of characters over USB.
Keyboard.println(string)
is also defined, if you want a
newline/linefeed to close out your string. An example:
Keyboard.print("Hello,
world");
//
This'll
send
your
computer
an
'H',
followed
by
'e',
followed
by...
Page 18 of 27