Hello folks,
I'm setting up a simple project in which it requires me to send an "A" keystroke (later it will be changed to a F7 funcion key) to my PC using the HID keyboard firmware for ATmega16U2 used as a USB bridge for an Arduino UNO.
The project works fine when sending the Serial.write commands through the COM port. I can see the characters coming from the Serial Monitor screen.
However when I short the pins and upload the HID firmware Arduino-keyboard-0.3.hex through Atmel Flip software and then reboot the UNO back, I'm not able to receive the keystrokes though HID keyboard.
The firmware I'm using to test and flash in Arduino IDE is Arduino-usbserial-uno.hex
When I'm flashing the firmware I'm able to see the ATmega16U2 device in Device Manager.
I'm also seeing the HID device when I reboot the UNO running.
Here is the full code. There are some filters going on to only send the keystrokes once every LOW to HIGH pin 2 voltage change.
I used this example as a starting point
/*
Button
Turns on and off a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.
The circuit:
- LED attached from pin 13 to ground
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground
- Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int lastButtonState = LOW; // the previous reading from the input pin
int edgeDetc1 = 0;
int edgeDetc2 = 0;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 500; // the debounce time; increase if the output flickers
void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if ((millis() - lastDebounceTime) > debounceDelay) {
if ((buttonState == HIGH) & (edgeDetc1 == LOW)){
// turn LED on:
digitalWrite(ledPin, HIGH);
edgeDetc1 = buttonState;
triggerKey (0x00, 0x41);
}
if ((buttonState == LOW) & (edgeDetc1 == HIGH)){
// turn LED off:
digitalWrite(ledPin, LOW);
edgeDetc1 = buttonState;
//Serial.println(buttonState);
}
}
lastButtonState = reading;
}
void triggerKey(uint8_t mod, uint8_t chr) {
uint8_t buf[8] = {mod, 0x00, chr, 0x00, 0x00, 0x00, 0x00, 0x00};
// press key
Serial.write(buf, 8);
// emulate key press delay
delay(10);
// release key
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
Serial.write(buf, 8); // Release key
}
Thanks,
Blukrr