Last active
October 26, 2017 19:00
-
-
Save raek/e8ee0b7e0801ef27c477c4333ec3e066 to your computer and use it in GitHub Desktop.
A-Star 32U4 NES Controller
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
#include <Keyboard.h> | |
const int vccPin = 2; // red | |
const int gndPin = 3; // black | |
const int shiftPin = 4; // yellow | |
const int latchPin = 5; // green | |
const int dataPin = 6; // blue | |
char buttonToKey[8] = { | |
'f', // A | |
'd', // B | |
's', // Select | |
KEY_RETURN, // Start | |
KEY_UP_ARROW, // Up | |
KEY_DOWN_ARROW, // Down | |
KEY_LEFT_ARROW, // Left | |
KEY_RIGHT_ARROW // Right | |
}; | |
static void clockPin(int pin) { | |
digitalWrite(pin, HIGH); | |
digitalWrite(pin, LOW); | |
} | |
void setup() { | |
pinMode(vccPin, OUTPUT); | |
digitalWrite(vccPin, HIGH); | |
pinMode(gndPin, OUTPUT); | |
digitalWrite(gndPin, LOW); | |
pinMode(shiftPin, OUTPUT); | |
digitalWrite(shiftPin, LOW); | |
pinMode(latchPin, OUTPUT); | |
digitalWrite(latchPin, LOW); | |
pinMode(dataPin, INPUT); | |
Serial.begin(9600); | |
Keyboard.begin(); | |
} | |
bool lastState[8]; | |
void loop() { | |
clockPin(latchPin); | |
for (int i = 0; i < 8; i++) { | |
bool state = !digitalRead(dataPin); | |
if (state != lastState[i]) { | |
if (state) { | |
Keyboard.press(buttonToKey[i]); | |
} else { | |
Keyboard.release(buttonToKey[i]); | |
} | |
} | |
lastState[i] = state; | |
clockPin(shiftPin); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment