Skip to content

Instantly share code, notes, and snippets.

@possan
Created September 9, 2014 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save possan/75c688f28dc03708adc0 to your computer and use it in GitHub Desktop.
Save possan/75c688f28dc03708adc0 to your computer and use it in GitHub Desktop.
Simple arcade stick controller thingy for Arduino, emulating an keyboard, sending arrow key events
const int leftButton = 4;
const int rightButton = 5;
const int upButton = 6;
const int downButton = 3;
const int enterButton = 7;
const int ledPin = 12;
class Debouncer {
private:
int time;
int lastvalue;
int stablemillis;
public:
int stableValue;
void setValue(int value);
};
void Debouncer::setValue(int value) {
if (value != lastvalue) {
lastvalue = value;
stablemillis = millis() + 20;
} else {
if (millis() > stablemillis) {
stableValue = value;
}
}
}
class Keymap {
private:
int key;
int lastvalue;
public:
Keymap(int keyboard_key);
void setStableValue(int value);
};
Keymap::Keymap(int keyboard_key) {
key = keyboard_key;
}
void Keymap::setStableValue(int value) {
if (value != lastvalue) {
lastvalue = value;
if (lastvalue) {
Keyboard.press(key);
digitalWrite(ledPin, HIGH);
} else {
Keyboard.release(key);
digitalWrite(ledPin, LOW);
}
}
}
Debouncer leftState;
Debouncer rightState;
Debouncer upState;
Debouncer downState;
Debouncer enterState;
Keymap leftMap(KEY_LEFT_ARROW);
Keymap rightMap(KEY_RIGHT_ARROW);
Keymap upMap(KEY_UP_ARROW);
Keymap downMap(KEY_DOWN_ARROW);
Keymap enterMap(KEY_RETURN);
void slowblink() {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
}
void fastblink() {
digitalWrite(ledPin, HIGH);
delay(10);
digitalWrite(ledPin, LOW);
}
void setup() {
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
pinMode(enterButton, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
// Mouse.begin();
Keyboard.begin();
slowblink();
}
void loop() {
if (Serial.available() > 0) {
char inChar = Serial.read();
switch (inChar) {
case 'A':
digitalWrite(ledPin, HIGH);
break;
case 'a':
digitalWrite(ledPin, LOW);
break;
case 'B':
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(ledPin, LOW);
break;
}
}
leftState.setValue(digitalRead(leftButton));
rightState.setValue(digitalRead(rightButton));
upState.setValue(digitalRead(upButton));
downState.setValue(digitalRead(downButton));
enterState.setValue(digitalRead(enterButton));
leftMap.setStableValue(leftState.stableValue);
rightMap.setStableValue(rightState.stableValue);
upMap.setStableValue(upState.stableValue);
downMap.setStableValue(downState.stableValue);
enterMap.setStableValue(enterState.stableValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment