Skip to content

Instantly share code, notes, and snippets.

@Alex4386
Created October 19, 2018 10:08
Show Gist options
  • Save Alex4386/639130199fbf31b3776184167f9a8a47 to your computer and use it in GitHub Desktop.
Save Alex4386/639130199fbf31b3776184167f9a8a47 to your computer and use it in GitHub Desktop.
#include <Keyboard.h>
int KEY_0='a';
int KEY_1='w';
int KEY_2='s';
int KEY_3='d';
int wasPressed[4] = {0,0,0,0};
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
Keyboard.begin();
}
void loop() {
/*
* 왼쪽에 케이블을 낸다고 했을 때 기준임.
*/
int a = analogRead(0);
int b = 1023 - analogRead(1);
Serial.print("Joystick X:");
Serial.print(a);
Serial.print(" / Joystick Y:");
Serial.println(b);
digitalWrite(13, LOW);
int key[4] = {0,0,0,0}; //왼쪽, 위, 아래, 오른쪽 / Vim이랑 같음
if (a > 800) {
key[3] = 1;
} else if ( a < 300 ) {
key[0] = 1;
}
if (b > 800) {
key[1] = 1;
} else if ( b < 300) {
key[2] = 1;
}
//이제 케이스바이 케이스로 입력해야지
if (key[0] == 1) {
if (wasPressed[0] != 1) {
Keyboard.press(KEY_0);
}
} else {
if (wasPressed[0] != 0) {
Keyboard.release(KEY_0);
}
}
if (key[1] == 1) {
if (wasPressed[1] != 1) {
Keyboard.press(KEY_1);
}
} else {
if (wasPressed[1] != 0) {
Keyboard.release(KEY_1);
}
}
if (key[2] == 1) {
if (wasPressed[2] != 1) {
Keyboard.press(KEY_2);
}
} else {
if (wasPressed[2] != 0) {
Keyboard.release(KEY_2);
}
}
if (key[3] == 1) {
if (wasPressed[3] != 1) {
Keyboard.press(KEY_3);
}
} else {
if (wasPressed[3] != 0) {
Keyboard.release(KEY_3);
}
}
int ledcount = 0;
for (int i = 0; i< 4; i++) {
wasPressed[i] = key[i];
if(key[i] == 1) {
ledcount++;
}
}
if (ledcount > 0) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment