Created
December 18, 2012 23:40
-
-
Save acspike/4333114 to your computer and use it in GitHub Desktop.
An arduino sketch for a foot actuated usb spacebar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Much code borrowed from the following | |
// https://github.com/practicalarduino/VirtualUsbKeyboard | |
// http://playground.arduino.cc/Learning/SoftwareDebounce | |
#include "UsbKeyboard.h" | |
#define BUTTON_PIN 12 | |
#define LED_PIN 13 | |
#define KEY_SPACE 0x2C | |
int counter = 0; // how many times we have seen new value | |
int reading; // the current value read from the input pin | |
int current_state = LOW; // the debounced input value | |
// the following variable is a long because the time, measured in milliseconds, | |
// will quickly become a bigger number than can be stored in an int. | |
long time = 0; // the last time the output pin was sampled | |
int debounce_count = 10; // number of millis/samples to consider before declaring a debounced input | |
void setup() { | |
pinMode(BUTTON_PIN, INPUT); | |
digitalWrite(BUTTON_PIN, HIGH); | |
pinMode(LED_PIN, OUTPUT); | |
digitalWrite(LED_PIN, LOW); | |
// Disable timer0 since it can mess with the USB timing. Note that | |
// this means some functions such as delay() will no longer work. | |
// BREAKS millis() too! | |
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() { | |
UsbKeyboard.update(); | |
reading = digitalRead(BUTTON_PIN); | |
if(reading == current_state && counter > 0) | |
{ | |
counter--; | |
} | |
if(reading != current_state) | |
{ | |
counter++; | |
} | |
// If the Input has shown the same value for long enough let's switch it | |
if(counter >= debounce_count) | |
{ | |
counter = 0; | |
current_state = reading; | |
digitalWrite(LED_PIN, current_state); | |
if (current_state==LOW) { | |
UsbKeyboard.sendKeyStroke(KEY_SPACE); | |
} | |
} | |
delayMicroseconds(1000); | |
} | |
/** | |
* Define our own delay function so that we don't have to rely on | |
* operation of timer0, the interrupt used by the internal delay() | |
*/ | |
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