1
9
10
11#include <Keyboard.h>
12#include<Wire.h>
13
14
15int THRESHOLD = 4000;
16
17
18int brake = 5;
19int nitro = 6;
20int pedal = 4;
21
22
23int left_led = 8;
24int right_led = 9;
25
26int x;
27int y;
28int z;
29
30const int MPU_addr=0x68;
31
32void indicator_blink()
33{
34
35 int i = 0;
36
37 for(i=0; i<5; i++){
38 digitalWrite(left_led, HIGH);
39 digitalWrite(right_led, HIGH);
40 delay(100);
41 digitalWrite(left_led, LOW);
42 digitalWrite(right_led, LOW);
43 delay(100);
44 }
45}
46
47void setup(){
48
49 pinMode(brake, INPUT);
50 pinMode(nitro, INPUT);
51 pinMode(pedal, INPUT);
52
53 pinMode(left_led, OUTPUT);
54 pinMode(right_led, OUTPUT);
55
56 Serial.begin(9600);
57
58 Wire.begin();
59 Wire.beginTransmission(MPU_addr);
60 Wire.write(0x6B);
61 Wire.write(0);
62 Wire.endTransmission(true);
63
64 indicator_blink();
65}
66
67void loop()
68{
69
70 Wire.beginTransmission(MPU_addr);
71 Wire.write(0x3B);
72 Wire.endTransmission(false);
73 Wire.requestFrom(MPU_addr, 14, true);
74
75 x = Wire.read()<<8|Wire.read();
76 y = Wire.read()<<8|Wire.read();
77 z = Wire.read()<<8|Wire.read();
78
79
80 if (digitalRead(brake)){
81 Keyboard.release(KEY_UP_ARROW);
82 Keyboard.press(KEY_DOWN_ARROW);
83
84 }
85
86 if (digitalRead(nitro)){
87 delay(10);
88 Keyboard.press('z');
89 if (digitalRead(nitro)){
90 Keyboard.press('A');
91 }
92 }
93
94 else if (digitalRead(pedal)){
95 Keyboard.release(KEY_DOWN_ARROW);
96 Keyboard.press(KEY_UP_ARROW);
97
98
99 if(y < -THRESHOLD){
100 Serial.print(" Left ");
101 Keyboard.release(KEY_RIGHT_ARROW);
102 Keyboard.press(KEY_LEFT_ARROW);
103 digitalWrite(left_led, HIGH);
104 }
105
106 else if(y > THRESHOLD){
107 Serial.print(" Right ");
108 Keyboard.release(KEY_LEFT_ARROW);
109 Keyboard.press(KEY_RIGHT_ARROW);
110 digitalWrite(right_led, HIGH);
111
112 }
113 else{
114 Keyboard.releaseAll();
115 }
116 }
117
118
119 delay(10);
120 digitalWrite(left_led, LOW);
121 digitalWrite(right_led, LOW);
122}
Anonymous user
4 years ago
I'd like to ask if this pcb and code can be used in steam for gyro controls? I was thinking of using the pcb design and make a similar code for gyro aiming controls. Thanks