Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <Keyboard.h>
int joystickIn = A0;
int shuttleIn = A1;
int joystick = 0;
int shuttle = 0;
int topButton = 12;
int topState = 0;
int shuttleA = 0;
int shuttleD = 0;
int joystickW = 0;
int joystickS = 0;
int topPreviousState = 0;
int previousW;
int previousA;
int previousS;
int previousD;
void setup()
{
Serial.begin(9600);
Keyboard.begin();
pinMode(topButton, INPUT_PULLUP);
}
void loop() {
joystick = analogRead(joystickIn);
shuttle = analogRead(shuttleIn);
topState = digitalRead(topButton);
if (topState != topPreviousState && topState == LOW ) {
Keyboard.press(' ');
delay(100);
Keyboard.release(' ');
Serial.println("SPACE");
}
topPreviousState = topState;
if (joystick > 640 && joystick < 800) { //640 to 800
shuttleD = 1;
}
else if (joystick < 400 && joystick > 300) { //400 to 300
shuttleA = 1;
}
else {
shuttleA = 0;
shuttleD = 0;
}
if (shuttle > 580 && shuttle < 700) { //580 to 700
joystickW = 1;
}
else if (shuttle < 470 && shuttle > 300) { //470 to 300
joystickS = 1;
}
else {
joystickW = 0;
joystickS = 0;
}
if (shuttleD != previousD && shuttleD == 1 ) {
Keyboard.press('d');
delay(100);
Keyboard.release('d');
Serial.println("d");
}
previousD = shuttleD;
if (shuttleA != previousA && shuttleA == 1 ) {
Keyboard.press('a');
delay(100);
Keyboard.release('a');
Serial.println("a");
}
previousA = shuttleA;
if (joystickW != previousW && joystickW == 1 ) {
Keyboard.press('w');
delay(100);
Keyboard.release('w');
Serial.println("w");
}
previousW = joystickW;
if (joystickS != previousS && joystickS == 1 ) {
Keyboard.press('s');
delay(100);
Keyboard.release('s');
Serial.println("s");
}
previousS = joystickS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment