How cool would it be if you have a USB keyboard and / or mouse to any of the buttons, joysticks, sensors or other electronic gadgets can make? You just input device into a controller for your computer programs or games you want to do about that. What if you could do it with one line of code, Arduino inside the cozy confines? Interested? Well, say hello to my micro friend: SparkFun Pro Micro!
Pro Micro is a tiny, Arduino- compatible microcontroller is centered around a ATmega32U4 - an 8-bit AVR ATmega328 with a very similar one huge admit: 32U4 comes equipped with a full-speed USB transceiver. So now, when you connect to your computer, Arduino, directly over the USB bus (excuse my RAS) are connected. Pro Micro USB device like a USBHID class can program it to work.
You might ask what is HID? It is one of many defined USB device classes. Each USB device is a device class, which defines exactly what is its general purpose. Printers, hubs, speakers and webcams to mention a few - - the class is loaded, but in this tutorial is going to specialize in HID: Human Interface Device. You could be the one holding the hand of a HID device, on the other hand, when you are hovering over another.
So ATmega32U4 takes care of USB hardware hurdle, but we still got a clean firmware. Fortunately for us, Arduino exist, and with the release of 1.0, and it comes with built-in USB core support for ATmega32U4. HID USB support that includes functionality. Unfortunately, some of that stuff tucked HID (or even locked) away from us. The purpose of this tutorial describes how to how to use the Arduino HID library, so you can get the most from your personal micro. So lets pop the hood!
Part 1: A simple HID keyboard
In this section header "easy" because it is what I stressed. And that's a great thing! Basically you have two functions in a USB keyboard to turn on your Pro Micro is:
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')
-
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. An example of that: Keyboard.print("Hello, world");
- The "Hello, world" of the Arduino HID class. This'll send your computer an 'H,' followed by 'e,' followed by an 'l,' followed by … you get the picture. You could also replace the "Hello, world" with either a String variable, or an array of chars.
Keyboard.println(string)
- Just like Keyboard.print() except this adds an [Enter] key to the end of the string.Keyboard.press(byte)
and Keyboard.release(byte)
. They do exactly what you'd expect. One presses a button down, the other releases a button. Make sure you release any buttons you press, otherwise you'll encounter some wiggyness on your computer. That's it. You can just pull out any of the functions, do not need to include any library or something.
After a Keyboard.write () or Keyboard.print () function has been performed by the Micro Pro, your computer will have to decide what to do with it. What is the character of your computer, or with a string of characters, the program is fully dependent on running time. If you open a text editor and active, then it will print out there.
The most straightforward example I can think of a single button to press a single key is tying. (Below, from the copy / paste, sketch or download here), this example uses a quick look. If you have a handy button, pin one end 9, and tie the other to the ground. Or, if you do not have a button, just short of a wire used to ground pin 9.
int buttonPin = 9; // Set a button to any pin
void setup()
{
pinMode(buttonPin, INPUT); // Set the button as an input
digitalWrite(buttonPin, HIGH); // Pull the button high
}
void loop()
{
if (digitalRead(buttonPin) == 0) // if the button goes low
{
Keyboard.write('z'); // send a 'z' to the computer via Keyboard HID
delay(1000); // delay so there aren't a kajillion z's
}
}
9. When the pin is grounded, your computer has a 'Z' character should be taken. Keypress activated when you do, make sure that you have a 'Z' will be able to open with an application. If you want, just leave the Arduino sketch is active, and it is a sketch editor of the 'Z' to type.
I also point out that the delay (1000), an important part of that sketch I want. 1000 arbitrary, but the delay () is not. Try out the delay, and some of your computer zzzzzzzzzzzz) will try to catch. Needless to say, you watch what you are doing on the computer, such as sending, as well as how fast and how often received.
HID USB keypad
When I'm using my laptop, I often find myself to achieve in the absence of a keypad. But with Pro Micro, I can make a keypad!
If you pair with our 12-button keypad Pro Micro, your very own, DIY USB keypad is far from just a few wires. Here, DIY USB keypad is designed for an example.
Pro Micro Feel free to any digital pin keypad connection pins. On a breadboard for me the easiest way to wire it all was.
(Copy and paste from below, or click here to download it) loaded up the keypad code:
No comments:
Post a Comment