So im trying to code a switch to perform a keyboard function here is my code
#include <Keyboard.h>
void setup() {
pinMode(2, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
//if the button is pressed
if(digitalRead(2)==LOW){
Keyboard.write("a");
delay(200);
}
}
When i flip the switch it is not sending typing the key on my keyboard
My switch has 2 legs one is connected to my ground and the other is connected to my "2" spot on my Pro Micro
Hi ya,
I use the same library in one of my projects.
I think the problem is that only some Arduino boards can act as HIDs (Human Interface Devices) - you're probably going to need something like an Arduino Leonardo or one of it's smaller varients.
I use one of these:
Well i did some research and mine takes a usb to power im pretty sure it is a leonardo just the micro one
Probably not; the pro micro isn't HID compatible as far as I know.
You could double-check by looking for it as an HID device in Windows Device Manager - but I suspect you'll only see it as a COM port.
ok i will check thank you for your help
1 Like
I have been trying to find online turtorials and they are using the same board as me theirs is just red
Hard to say; Most of the boards look very very similar. To be honest, the difference between a HID compliant one is possibly just different firmware (I'm aware of a procedure to upgrade a nonHID compatible board but I really don't want to post it because you'll possibly brick your device if you tried it and it didn't work).
Pro micro uses the atmega32u4, same as a Leonardo, so it should work.
Replace Keyboard.write("A") with Keyboard.write('A'), you want to use a single quote.
I tried and im still getting nothing could i somehow upload a photo of my setup?
I think the first thing we need to clarify is "does the device appear in the Human Interface Devices" section of Device Manager? Perhaps connect / disconnect it whilst watching to see if anything changes in there. If it's not appearing there then nothing else is going to work.
ok let me check really fast
The device IS a HID device
Excellent - looks like you and David were right, and I was off the track; apologies for that. Have you tried his suggestion also?
All good, I did attempt the quote thing and it still is not working.
A picture would definitely help, a common mistake is thinking that pin 2 is physically the second pin on the row of connector pins, instead of the actual pin labeled 2 on the official version of the board.
Sparkfun Pro Micro
You could also try bypassing the switch in the code so it just prints regardless as a way of working out if the problem is in the switch portion of the code.
If it helps, here's my code for an 8 digit random number generator when you close a switch - you could try that if you like - it should pop an 8 digit random number into something like Notepad when the switch is closed.
/* This program inserts a random 8 digit number at the current cursor position when the switch is pressed.*/
#include "Keyboard.h" // Include keyboard control library
// Declare constants
int const seedPin = 1; //
int const switchPin = 2; //
// Declare Variables
uint8_t seedBitValue = 0; //
uint8_t seedByteValue = 0; //
uint32_t seedWordValue = 0; //
uint8_t randomNumberLength = 8; //
// SETUP CODE
void setup() // Setup code starts here (runs once)
{
pinMode(switchPin, INPUT); // Setup port for switch
Keyboard.begin(); // Initialise keyboard
for (uint8_t wordShift = 0; wordShift < 4; wordShift++) // 4 bytes in a 32 bit word
{
for (uint8_t byteShift = 0; byteShift < 8; byteShift++) // 8 bits in a byte
{
seedBitValue = (analogRead(seedPin) & 0x01); // Flip the coin
delay(1); // Delay a single millisecond to allow the pin to fluctuate
seedByteValue = seedByteValue | ((seedBitValue & 0x01) << byteShift); // Build a stack of eight flipped coins
seedBitValue = 0; // Clear out the previous coin value
}
seedWordValue = seedWordValue | (uint32_t)seedByteValue << (8 * wordShift); // Build a stack of four sets of 8 coins (shifting right creates a larger number so cast to 32bit)
seedByteValue = 0; // Clear out the previous stack value
}
randomSeed(seedWordValue); // Seed the random number generator
} // End of setup code
// MAIN CODE
void loop()
{
while (digitalRead(switchPin) == 1) // Only do whilst switch is closed
{
for (uint8_t loopCount; loopCount < randomNumberLength; loopCount++) // Do it 8 times
{
Keyboard.print(random(0, 10)); // Output a random number
}
delay(1000); // Wait 1 second before restarting loop to give time for switch release
}
} // End of Main Code Loop
You will get endless trouble if you do not solder on the headers.
1 Like
Ok i will make sure to do that i just set it up today and wanted to test it out