-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris_joystick.ino
65 lines (60 loc) · 1.47 KB
/
tetris_joystick.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <Keyboard.h>
#define joyX A0
#define joyY A1
#define button 3
void setup() {
Serial.begin(9600);
pinMode(button, INPUT);
}
bool last_y, last_x, last_button, last_space;
int delay_y = 50, delay_x = 50, delay_button = 100;
void loop() {
int xValue = analogRead(A0);
int yValue = analogRead(A1);
Serial.print(xValue);
Serial.print("\t");
Serial.print(yValue);
Serial.print("\t");
Serial.println(digitalRead(button));
if (yValue > 900 && last_y) {
Keyboard.press(0xD7); // right
Serial.println("right");
last_y = 0;
delay(delay_y);
}
else if (yValue < 100 && last_y) {
Keyboard.press(0xD8); // left
Serial.println("left");
last_y = 0;
}
else if (yValue >= 460 && yValue <= 560) {
last_y = 1;
Keyboard.release(0xD8);
Keyboard.release(0xD7);
}
if (xValue > 900 && last_x) {
Keyboard.write(0xDA); // up(rotate clockwise)
Serial.println("up");
last_x = 0;
delay(delay_x);
}
else if (xValue < 100 && last_x) {
Keyboard.press(0xD9); // down
Serial.println("down");
last_x = 0;
}
else if (xValue >= 460 && xValue <= 560) {
last_x = 1;
Keyboard.release(0xD9);
Keyboard.release(0xDA);
}
if (digitalRead(button) == HIGH && last_button) {
Keyboard.write(0x81); // shift(swap hold piece)
Serial.println("shift");
last_button = 0;
delay(delay_button);
}
else {
last_button = 1;
}
}