Skip to content

Instantly share code, notes, and snippets.

@mikewen
Forked from petrockblog/gist:3051606
Created September 26, 2016 03:01
Show Gist options
  • Save mikewen/98cc31f9560d1ade886f53bf02d6b3fe to your computer and use it in GitHub Desktop.
Save mikewen/98cc31f9560d1ade886f53bf02d6b3fe to your computer and use it in GitHub Desktop.
Exemplary Arduino sketch of a virtual keyboard with the V-USB library
#include "UsbKeyboard.h"
#define PIN_BUTTON 7 // the button is attached to pin 7
int lastState = LOW; // LOW is equivalent to 0
void setup() {
// button setup
pinMode(PIN_BUTTON, INPUT);
// USB setup -----------------
// Disable timer0 since it can mess with the USB timing. Note that
// this means some functions such as delay() will no longer work.
TIMSK0 &= !(1<TOIE0);
// Clear interrupts while performing time-critical operations
cli();
// Force re-enumeration so the host will detect us
usbDeviceDisconnect();
delayMs(250);
usbDeviceConnect();
// Set interrupts again
sei();
}
void loop() {
// update USB device state
UsbKeyboard.update();
// check, if transition to button-pressed has occured
if( (digitalRead(PIN_BUTTON) == HIGH) && (lastState==LOW) ) {
UsbKeyboard.sendKeyStroke(KEY_A); // if so, send the letter 'a' via USB keyboard
lastState = HIGH; // update the state variable
delay(10); // simple de-bouncing
// check, if the button was released
} else if( (digitalRead(PIN_BUTTON) == LOW) && (lastState==HIGH) ) {
lastState = LOW; // if so, update the state variable
delay(10); // simple de-bouncing
}
}
// helper method for V-USB library
void delayMs(unsigned int ms) {
for( int i=0; i<ms; i++ ) {
delayMicroseconds(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment