bonjour,
j'ai un carte arduino micro pro connectée à mon pc. via les pin rx et tx elle est reliée à une carte arduino mega 2556 (sur pin 18 et 19).
Le but est de créer un joystick plutôt boite à boutons pour jouer sur simulateur de vol (DCS World pour ceux qui connaissent).
Il n'y a que des interrupteurs double ou triple position (on/off , on/off/on) et je souhaite aussi avoir de encodeur rotatifs KY-040
Les 2 programmes fonctionnent avec des boutons simples. . pour le moment mon code est
MEGA
MICRO PRO
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 // ===================================================== // BOUTONS MEGA (50 boutons) // ===================================================== #define BUTTON1 2 #define BUTTON2 3 #define BUTTON3 4 #define BUTTON4 5 #define BUTTON5 6 #define BUTTON6 7 #define BUTTON7 8 #define BUTTON8 9 #define BUTTON9 10 #define BUTTON10 11 #define BUTTON11 12 #define BUTTON12 13 #define BUTTON13 22 #define BUTTON14 23 #define BUTTON15 24 #define BUTTON16 25 #define BUTTON17 26 #define BUTTON18 27 // 28 = ENCODEUR A // 30 = ENCODEUR B #define BUTTON19 29 #define BUTTON20 31 #define BUTTON21 32 #define BUTTON22 33 #define BUTTON23 34 #define BUTTON24 35 #define BUTTON25 36 #define BUTTON26 37 #define BUTTON27 38 #define BUTTON28 39 #define BUTTON29 40 #define BUTTON30 41 #define BUTTON31 42 #define BUTTON32 43 #define BUTTON33 44 #define BUTTON34 45 #define BUTTON35 46 #define BUTTON36 47 #define BUTTON37 48 #define BUTTON38 49 #define BUTTON39 50 #define BUTTON40 51 #define BUTTON41 52 #define BUTTON42 53 #define BUTTON43 A0 #define BUTTON44 A1 #define BUTTON45 A2 #define BUTTON46 A3 #define BUTTON47 A4 #define BUTTON48 A5 #define BUTTON49 A6 #define BUTTON50 A7 // ===================================================== // TABLEAU DES PINS BOUTONS // ===================================================== const byte NUM_BUTTONS = 50; const byte buttonPins[NUM_BUTTONS] = { BUTTON1, BUTTON2, BUTTON3, BUTTON4, BUTTON5, BUTTON6, BUTTON7, BUTTON8, BUTTON9, BUTTON10, BUTTON11, BUTTON12, BUTTON13, BUTTON14, BUTTON15, BUTTON16, BUTTON17, BUTTON18, BUTTON19, BUTTON20, BUTTON21, BUTTON22, BUTTON23, BUTTON24, BUTTON25, BUTTON26, BUTTON27, BUTTON28, BUTTON29, BUTTON30, BUTTON31, BUTTON32, BUTTON33, BUTTON34, BUTTON35, BUTTON36, BUTTON37, BUTTON38, BUTTON39, BUTTON40, BUTTON41, BUTTON42, BUTTON43, BUTTON44, BUTTON45, BUTTON46, BUTTON47, BUTTON48, BUTTON49, BUTTON50 }; // ===================================================== // ENCODEUR KY-040 (méthode simple) // ===================================================== #define ENCODER_A 28 #define ENCODER_B 30 int lastCLK; // ===================================================== // DONNÉES DES BOUTONS // ===================================================== byte buttons[7] = {0}; // ===================================================== // SETUP // ===================================================== void setup() { Serial.begin(115200); // Debug Serial1.begin(115200); // Vers Pro Micro // Configuration des boutons for (byte i = 0; i < NUM_BUTTONS; i++) { pinMode(buttonPins[i], INPUT_PULLUP); } // Encodeur pinMode(ENCODER_A, INPUT_PULLUP); pinMode(ENCODER_B, INPUT_PULLUP); lastCLK = digitalRead(ENCODER_A); Serial.println("=== MEGA OK ==="); } // ===================================================== // ENCODEUR SIMPLE // ===================================================== byte readEncoderSimple() { int currentCLK = digitalRead(ENCODER_A); if (currentCLK != lastCLK) { int currentDT = digitalRead(ENCODER_B); lastCLK = currentCLK; if (currentDT != currentCLK) { return 1; // droite } else { return 2; // gauche } } return 0; } // ===================================================== // LOOP // ===================================================== void loop() { // --------------------------------------------------- // Lecture des boutons // --------------------------------------------------- for (byte i = 0; i < 7; i++) buttons[i] = 0; for (byte i = 0; i < NUM_BUTTONS; i++) { if (!digitalRead(buttonPins[i])) { buttons[i / 8] |= (1 << (i % 8)); } } // --------------------------------------------------- // Lecture encodeur // --------------------------------------------------- byte encoderEvent = readEncoderSimple(); if (encoderEvent != 0) { Serial.print("ENCODEUR = "); Serial.println(encoderEvent); } // --------------------------------------------------- // Envoi du paquet // --------------------------------------------------- Serial1.write(0xAA); Serial1.write(buttons, 7); Serial1.write(encoderEvent); delay(1); }
sur le moniteur série de la MEGA, j'ai bien les bonnes infos. Je vois bien la LED TX qui s'affiche à chaque changement de cran du rotateur.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310 #include "Joystick.h" // ===================================================== // BOUTONS LOCAUX // ===================================================== #define joyButton1 2 #define joyButton2 3 #define joyButton3 4 #define joyButton4 5 #define joyButton5 6 #define joyButton6 7 #define joyButton7 8 #define joyButton8 9 #define joyButton9 A2 #define joyButton10 A1 #define joyButton11 A0 #define joyButton12 15 // ===================================================== // AXES // ===================================================== #define joyRZ A3 #define joyThrottle A3 // ===================================================== // TABLEAU DES BOUTONS LOCAUX // ===================================================== const byte unoButtonPins[12] = { joyButton1, joyButton2, joyButton3, joyButton4, joyButton5, joyButton6, joyButton7, joyButton8, joyButton9, joyButton10, joyButton11, joyButton12 }; // ===================================================== // JOYSTICK USB // ===================================================== // // 12 boutons locaux // 50 boutons Mega // 2 boutons encodeur // // TOTAL = 64 boutons // // 0 à 11 = boutons locaux // 12 à 61 = boutons Mega // 62 = encodeur droite // 63 = encodeur gauche // ===================================================== Joystick_ Joystick( 0x15, JOYSTICK_TYPE_JOYSTICK, 32, 0, true, true, false, false, false, true, false, true, false, false, false ); // ===================================================== // DONNÉES REÇUES DE LA MEGA // ===================================================== // 50 boutons = 7 octets byte megaButtons[7] = { 0 }; // ===================================================== // ÉVÉNEMENT ENCODEUR // ===================================================== // // 0 = aucun mouvement // 1 = rotation droite // 2 = rotation gauche // ===================================================== byte megaEncoderEvent = 0; // ===================================================== // RÉCEPTION DES DONNÉES DE LA MEGA // ===================================================== // // Trame reçue : // // 0xAA // + 7 octets boutons // + 1 octet encodeur // // TOTAL = 9 octets // ===================================================== void receiveMegaData() { static byte state = 0; while (Serial1.available()) { byte data = Serial1.read(); // ------------------------------------------------- // ATTENTE DU HEADER // ------------------------------------------------- if (state == 0) { if (data == 0xAA) { state = 1; } } // ------------------------------------------------- // RÉCEPTION DES 7 OCTETS DE BOUTONS // ------------------------------------------------- else if (state >= 1 && state <= 7) { megaButtons[state - 1] = data; state++; } // ------------------------------------------------- // RÉCEPTION DE L'ÉVÉNEMENT ENCODEUR // ------------------------------------------------- else if (state == 8) { megaEncoderEvent = data; state = 0; } } } // ===================================================== // SETUP // ===================================================== void setup() { // --------------------------------------------------- // CONFIGURATION DES BOUTONS LOCAUX // --------------------------------------------------- for (byte i = 0; i < 12; i++) { pinMode( unoButtonPins[i], INPUT_PULLUP ); } // --------------------------------------------------- // LIAISON SÉRIE AVEC LA MEGA // --------------------------------------------------- Serial1.begin(115200); // --------------------------------------------------- // INITIALISATION DU JOYSTICK USB // --------------------------------------------------- Joystick.begin(); } // ===================================================== // LOOP // ===================================================== void loop() { // --------------------------------------------------- // RÉCEPTION DES DONNÉES DE LA MEGA // --------------------------------------------------- receiveMegaData(); // --------------------------------------------------- // AXES // --------------------------------------------------- Joystick.setRzAxis( analogRead(joyRZ) ); Joystick.setThrottle( analogRead(joyThrottle) ); // --------------------------------------------------- // BOUTONS LOCAUX // --------------------------------------------------- // // Boutons joystick 0 à 11 // --------------------------------------------------- for (byte i = 0; i < 12; i++) { Joystick.setButton( i, !digitalRead(unoButtonPins[i]) ); } // --------------------------------------------------- // BOUTONS DE LA MEGA // --------------------------------------------------- // // Boutons joystick 12 à 61 // --------------------------------------------------- for (byte i = 0; i < 50; i++) { byte byteNumber = i / 8; byte bitNumber = i % 8; bool buttonState = megaButtons[byteNumber] & (1 << bitNumber); Joystick.setButton( 12 + i, buttonState ); } // --------------------------------------------------- // ENCODEUR ROTATIF // --------------------------------------------------- // // Bouton 62 = rotation droite // Bouton 63 = rotation gauche // // Chaque rotation génère une courte impulsion. // --------------------------------------------------- if (megaEncoderEvent == 1) { Joystick.setButton(30, true); delay(20); Joystick.setButton(30, false); megaEncoderEvent = 0; } else if (megaEncoderEvent == 2) { Joystick.setButton(31, true); delay(20); Joystick.setButton(31, false); megaEncoderEvent = 0; } // --------------------------------------------------- // PETITE PAUSE // --------------------------------------------------- delay(5); }
sur le moniteur série de la MICRO, rien. Il semblerait que ça ne code pas comme il faut de la mega vers micro.
Merci par avance pour votre aide.





Répondre avec citation





Partager