bonjour,

je suis en train de réaliser une boite à bouton pour mapper des touches pour un simulateur de vol (DCS).

Je travail à partir d'une Arduino Mega

Je l'ai flashée et j'utilise un code déjà fourni qui fonctionne bien pour des switch 2 ou 3 positions.

Ce que je voudrai, c'est mettre des encodeurs rotatifs.

Pour le moment, quand je tourne dans un sens ou dans l'autre, comme les 2 boutons s'activent l'un après l'autre, l'effet s'annule et je n'ai pas de rotation ou c'est très aléatoire.
Auriez-vous une idée de comment faire?

Voici mon code

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
// This version of MegaJoy sketch was created originally
// by Alan Chatham and the UnoJoy team.
//
// Modified by Deusimar Rodrigues dos Santos Junior,
// also known as Shadow Nightmares, in Christmas 2018.
//
// Using parts of a code created by Matthew Heironimus,
// written in 20-11-2015
//--------------------------------------------------------------------
 
#include "MegaJoy.h"
#include <Encoder.h>
Encoder myEnc(30, 31);
 
 
void setup(){
  setupPins();
  setupMegaJoy();
}
 
void loop(){
  // Always be getting fresh data
  megaJoyControllerData_t controllerData = getControllerData();
  setControllerData(controllerData);
}
 
void setupPins(void){
  // Set all the digital pins as inputs
  // with the pull-up enabled, except for the 
  // two serial line pins
  for (int i = 4; i <= 54; i++){
    pinMode(i, INPUT);
    digitalWrite(i, HIGH);
  }
}
 
megaJoyControllerData_t getControllerData(void){
 
  // Set up a place for our controller data
  //  Use the getBlankDataForController() function, since
  //  just declaring a fresh dataForController_t tends
  //  to get you one filled with junk from other, random
  //  values that were in those memory locations before
  megaJoyControllerData_t controllerData = getBlankDataForMegaController();
  // Since our buttons are all held high and
  //  pulled low when pressed, we use the "!"
  //  operator to invert the readings from the pins
  for (int i = 4; i < 54; i++){
    controllerData.buttonArray[(i - 4) / 8] |= (!digitalRead(i)) << ((i - 4) % 8);
  }
 
  // Set the analog sticks
  //  Since analogRead(pin) returns a 10 bit value,
  //  we need to perform a bit shift operation to
  //  lose the 2 least significant bits and get an
  //  8 bit number that we can use
  long newPosition = myEnc.read();
  long oldPosition  = -999;
  if (newPosition != oldPosition) {
    oldPosition = newPosition; }
  controllerData.analogAxisArray[0] = map(newPosition, -1500, 1500, 0, 1023);
  controllerData.analogAxisArray[1] = 512;
  controllerData.analogAxisArray[2] = map(analogRead(A0), 536, 480, 0, 1023);
  controllerData.analogAxisArray[3] = map(analogRead(A1), 1003, 670, 0, 1023); 
  controllerData.analogAxisArray[4] = map(analogRead(A2), 462, 340, 0, 1023);
  controllerData.analogAxisArray[5] = 0; 
  controllerData.analogAxisArray[6] = 0; 
  controllerData.analogAxisArray[7] = 0; 
  controllerData.analogAxisArray[8] = 0; 
  controllerData.analogAxisArray[9] = 0; 
  controllerData.analogAxisArray[10] = 0; 
  controllerData.analogAxisArray[11] = 0;
  // And return the data!
  return controllerData;
}