So i'm using 5 rotary encoders EC11 to make a autopilot panel for MFS.
I alredy have the push buttons working in another arduino but i can't read and press the rotary encoders in this one. I have the 5 connect to arduino pro micro, but only one work.
the wiring is the midle pin is connect to GND and the other 2 (A and B) are connect to 2 diferent digital pins.
rotary1 - pins 2 and 3
rotary2 - pins 4 and 5
rotary2 - pins 6 and 7
rotary2 - pins 16 and 10
rotary2 - pins 15 and 14
I'm using Joystick library from Matthew Heironimus (GitHub - MHeironimus/ArduinoJoystickLibrary: An Arduino library that adds one or more joysticks to the list of HID devices an Arduino Leonardo or Arduino Micro can support.) as well the "RotaryEncoder" library by Matthias Hertel
the code i already have is:
Bloco de Citação
#include <RotaryEncoder.h>
#include <Joystick.h>
#define rotIntPin0 3
#define rotIntPin1 2
#define rotIntPin2 4
#define rotIntPin3 5
#define rotIntPin4 6
#define rotIntPin5 7
#define rotIntPin6 8
#define rotIntPin7 9
#define rotIntPin8 10
#define rotIntPin9 12
//Define Encoders
RotaryEncoder encoder(rotIntPin0,rotIntPin1);
RotaryEncoder encoder2(rotIntPin3,rotIntPin2);
RotaryEncoder encoder3(rotIntPin4,rotIntPin5);
RotaryEncoder encoder4(rotIntPin6,rotIntPin7);
RotaryEncoder encoder5(rotIntPin9,rotIntPin8);
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
10, 0, // Button Count, Hat Switch Count
false, false, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
void setup() {
Joystick.begin(false);
attachInterrupt(digitalPinToInterrupt(rotIntPin0), getTick1, CHANGE);
attachInterrupt(digitalPinToInterrupt(rotIntPin1), getTick1, CHANGE);
attachInterrupt(digitalPinToInterrupt(rotIntPin2), getTick2, CHANGE);
attachInterrupt(digitalPinToInterrupt(rotIntPin3), getTick2, CHANGE);
attachInterrupt(digitalPinToInterrupt(rotIntPin4), getTick3, CHANGE);
attachInterrupt(digitalPinToInterrupt(rotIntPin5), getTick3, CHANGE);
attachInterrupt(digitalPinToInterrupt(rotIntPin6), getTick4, CHANGE);
attachInterrupt(digitalPinToInterrupt(rotIntPin7), getTick4, CHANGE);
attachInterrupt(digitalPinToInterrupt(rotIntPin8), getTick5, CHANGE);
attachInterrupt(digitalPinToInterrupt(rotIntPin9), getTick5, CHANGE);
}
void getTick1(){
encoder.tick();
}
void getTick2(){
encoder2.tick();
}
void getTick3(){
encoder3.tick();
}
void getTick4(){
encoder4.tick();
}
void getTick5(){
encoder5.tick();
}
void loop() {
static int pos = 0;
static int pos2 = 0;
static int pos3 = 0;
static int pos4 = 0;
static int pos5 = 0;
int newPos = encoder.getPosition();
int newPos2 = encoder2.getPosition();
int newPos3 = encoder3.getPosition();
int newPos4 = encoder4.getPosition();
int newPos5 = encoder5.getPosition();
if (pos != newPos) {
if (newPos < pos) Joystick.pressButton(0);
else if (newPos > pos) Joystick.pressButton(1);
pos = newPos;
}
if (pos2 != newPos2) {
if (newPos2 < pos2) Joystick.pressButton(2);
else if (newPos2 > pos2) Joystick.pressButton(3);
pos2 = newPos2;
}
if (pos3 != newPos3) {
if (newPos3 < pos3) Joystick.pressButton(4);
else if (newPos3 > pos3) Joystick.pressButton(5);
pos3 = newPos3;
}
if (pos4 != newPos4) {
if (newPos4 < pos4) Joystick.pressButton(6);
else if (newPos4 > pos4) Joystick.pressButton(7);
pos4 = newPos4;
}
if (pos5 != newPos5) {
if (newPos5 < pos5) Joystick.pressButton(8);
else if (newPos5 > pos5) Joystick.pressButton(9);
pos5 = newPos5;
}
Joystick.sendState();//send state 1st before we clear the button states
ClearButtons();
}
void ClearButtons()
{
delay(150);
Joystick.releaseButton(0);
Joystick.releaseButton(1);
Joystick.releaseButton(2);
Joystick.releaseButton(3);
Joystick.releaseButton(4);
Joystick.releaseButton(5);
Joystick.releaseButton(6);
Joystick.releaseButton(7);
Joystick.releaseButton(8);
Joystick.releaseButton(9);
}