Rotary Encoder "malfunctioning"? Help.

Hello everyone,
So, im currently trying to make a button box to work with X-Plane11, and, in order to get familiarezed with arduino, wiring and coding the components i’ve been doing little sections of it to better understand how things work (since i am a beginner at all this) and then put all these little parts togheter.

This time i tried to make a single rotary encoder to interface with the SIM, but it did not work as i expected, the pushbutton function worked just fine, as well as the counter clockwise rotation, however, the clockwise rotation was really bad.

Basically, when i turn the knob clockwise either nothing happens, or it sees it as a counter clockwise rotation.
can anyone help me figure out what is going on, please?

Here is the code that i made:

#include <Joystick.h>

Joystick_ Painel(JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_JOYSTICK,
3,
false, // y axis
false, // x axis
false, // z axis
false, // rx axis
false, // ry axis
false, // rz axis
false, // rudder
false, // throttle
false, // accelerator
false, // brake
false); // steering wheel

#define CLK 9
#define DT 8
#define SW 7

int currentStateCLK;
int previousStateCLK;

void setup() {

pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);

previousStateCLK = digitalRead(CLK);

Painel.begin();
}

void loop() {
Check_Rotation();
Check_Push();
}

void Check_Rotation(){

currentStateCLK = digitalRead(CLK);
if(currentStateCLK != previousStateCLK){
if(digitalRead(DT) != currentStateCLK){
Painel.setButton(1,1);
delay(50);
Painel.setButton(1,0);
}else{
Painel.setButton(0,1);
delay(50);
Painel.setButton(0,0);
}
previousStateCLK = currentStateCLK;
}
}

void Check_Push(){

if (digitalRead(SW) == LOW){
Painel.setButton(2, 1);
}else{
Painel.setButton(2,0);
}
}

If you post your code as described in the how to use this forum sticky, more forum members will read it.

Please post a schematic in CAD or a picture of a hand drawn circuit in jpg or png. PLEASE- not a pretty Fritzing picture.

Have you tried running the example sketch for a rotary encoder?

It would be helpful if you included all of the code, what is there looks good however the setup() is entirely missing. You say parts of the code are working and other parts are not. Since I am not there please explain how you know this. Consider not using Delay(), it blocks any code from operating until its time has expired. A few comments would help. With all the widgets you appear to have connected a schematic, not a frizzy thing, would be a big help.

Hello Everyone, thank you so much for the replies. Here is the properly sent code. I also added a few comments. Please let me know if something is not clear. I will make more replies with the rest of the information (schematics + a video of the encoder running on the sim) so you guys can see exactly what i mean.

#include <Joystick.h>                                                           

Joystick_ Painel(JOYSTICK_DEFAULT_REPORT_ID,                        //Declaring a joystick entity with only 3 buttons, all the rest disabled.
  JOYSTICK_TYPE_JOYSTICK,                                           //Button 0 = CCW rotation, Button 1 = CW rotation, Button 2 = Push button.
  3, //number of buttons                                
  0, //number of hat switches                                   
  //Set as many axis to "true" as you have potentiometers for
  false, // y axis
  false, // x axis
  false, // z axis
  false, // rx axis
  false, // ry axis
  false, // rz axis
  false, // rudder
  false, // throttle
  false, // accelerator
  false, // brake
  false); // steering wheel


#define CLK 9      //CLK pin of the encoder connected to arduino pin 9
#define DT 8       //DT pin of the encoder to arduino pin 8
#define SW 7       //SW pin of the encoder to arduino pin 7

int currentStateCLK;      //Integers to store the states of the CLK pin.
int previousStateCLK;

  void setup() {

  pinMode(CLK, INPUT);          
  pinMode(DT, INPUT);
  pinMode(SW, INPUT_PULLUP);

  previousStateCLK = digitalRead(CLK);

  Painel.begin();        //Initializing the joystick called "Painel"
  }

  void loop() {
    Check_Rotation();   //check if the encoder has been rotated.
    Check_Push();       //check if the pushbutton has been pressed.
  }

    void Check_Rotation(){

        currentStateCLK = digitalRead(CLK);
          if(currentStateCLK != previousStateCLK){              //If the CLK state has changed
            if(digitalRead(DT) != currentStateCLK){            //check if CLK state is different from DT state
              Painel.setButton(1,1);                           //if it is, it means a CW rotation (set button 1 to state 1, which means pressed)
              //delay(50);
              Painel.setButton(1,0);                           //then set button 1 to state 0 (which means released);
            }else{
              Painel.setButton(0,1);                         //If the DT state is equal to CLK state it means a CCW rotation
              //delay(50);                                   //if so, button 0 is set to state 1
              Painel.setButton(0,0);                         //and then setted again to state 0 (released)
            }
        previousStateCLK = currentStateCLK;
          }
    }
    
    
    void Check_Push(){ 
  
      if (digitalRead(SW) == LOW){      //if the SW pin is LOW the button has been pressed.
        Painel.setButton(2, 1);         //if so, button number 2 of the joystick is set to state 1 (pressed)
      }else{
        Painel.setButton(2,0);          //if SW is not LOW, button number 2 is set to state 0 (released).
      }
    }

Here is the schematic of the wiring, i am sorry for the drawing but i have no CAD software available.
Also, here is the video of the encoder running on the sim: encoder problem - YouTube

(please ignore my computer trying to takeoff hahahah).
(look at the numbers on the upper right corner,those are supposed to change with the rotation)

Answering the other question, yes, i ran a little sketch first, that just printed on the serial monitor CW/CCW depending on the rotation and a counter that increases if the knob is turned CW and decreases if the knob is turned CCW. there were no problems, both rotations were detected.

Here is the code:

#define CLK 4     //CLK connected to pin 4 on the arduino
#define DT  5     //DT connected to pin 5 on the arduino
#define SW  3     //SW connected to pin 3 on the arduino
                      
int counter = 0;          //define an counter
int currentStateCLK;      //store the CLK states
int previousStateCLK;
String Encoder_direction = "";  //Define the direction as a string to print on the serial

void setup() {

  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  pinMode(SW, INPUT_PULLUP);
  
  Serial.begin(9600);

  previousStateCLK = digitalRead(CLK);

}

void loop() {
  Check_Rotation();
  Check_Button_Press();
}

void Check_Rotation(){ 
    
    currentStateCLK = digitalRead(CLK);

  if (currentStateCLK != previousStateCLK){ //if the states are different, the encoder has been rotated
    if(digitalRead(DT) != currentStateCLK){ //if DT state is different than CLK state it means a CW rotation
      counter ++;                           //increase counter
      Encoder_direction = "CW";             //store the direction
    }else{    
      counter --;                   
      Encoder_direction = "CCW";
    }
      Serial.print("Direction: ");
      Serial.print(Encoder_direction);
      Serial.print("| Counter: ");
      Serial.println(counter);

      previousStateCLK = currentStateCLK;
  }
}

void Check_Button_Press(){
  
  if (digitalRead(SW) == LOW){                //SW LOW, means that the button has been pressed, so that's what we're checking here
      Serial.println("Button Pressed");
    }else{ 
  }
}

Thank you to everybody in advance and please, ask more questions if there is anything else you guys need to help clarify the situation.

Your image doesn't match the code:

#define CLK 4     //CLK connected to pin 4 on the arduino
#define DT  5     //DT connected to pin 5 on the arduino
#define SW  3     //SW connected to pin 3 on the arduino

Otherwise, nothing jumps out at me.

Are there pullups on the encoder board for DT and CLK. I think those need pullups, too. I use the internal pullups.

Hi, I think you need a pullup resistors on those cheap encoders and a 0.1uF cap from each CLK, DT, SW to gnd.

Tom.... :)

Thank you so much for the answers.
So, the back of the encoder looks like this, i see only 2 resistors, is it suposed to have only 2?
I’ll do a research on how to connect it with a capacitor and other resistors, thank you so much.