How to use Interrupt Service Routine with a rotary encoder module

the code is not working as expected before rotating the rotary encoder it prints anticlockwise and when turned it prints clockwise and increments +1 even the rotary encoder is rotated anticlockwise...

<

#include <Arduino.h>

#define signalA 4
#define signalB 5


volatile bool prevState;//Declaring Current state globally;
volatile long int reader=0;//intialising reader to zero;
volatile bool roatryEncSig1 = LOW;
volatile bool roatryEncSig2 = LOW;
bool turnedCW = false;
bool turnedCCW = false;
volatile bool prevState ;

void IRAM_ATTR Roatar();
//void IRAM_ATTR Roatar1();


void setup(){
Serial.begin(9600);


pinMode(signalA,INPUT_PULLUP);
pinMode(signalB,INPUT_PULLUP);
 attachInterrupt(digitalPinToInterrupt(signalB), Roatar,CHANGE);
 attachInterrupt(digitalPinToInterrupt(signalA), Roatar,CHANGE);

prevState = digitalRead(signalA);
}


void loop(){
   Serial.println(reader);
   if (turnedCW==true){
    delayMicroseconds(5000);
                Serial.print("ClockWise  ");
                digitalWrite(LED_BUILTIN,LOW);}
                                                else{ delayMicroseconds(5000);
                                                    Serial.print("AntiClockWise  ");
                                                    digitalWrite(LED_BUILTIN,HIGH);}


}

void Roatar(){
bool currentState = digitalRead(signalA);


    delayMicroseconds(5000);
    if (digitalRead(signalA) != digitalRead(signalB)){
        reader= reader+1;
            turnedCW=true;
       
        }else{
            delayMicroseconds(5000);
            reader = reader-1;
            
            turnedCCW=true;}
          

prevState=currentState;
}
/>

What is your end goal ?
What kind of encoder are you using ?

I think it sort of is. It's not doing what you want it to do but that is something else.

What arduino are you using ?

It looks like you have attached one ISR to both signals.

wouldn't you need to do that if you wanted to recognize each possible encoder step?

the interrupt would read the state of both inputs to determine 1 of 4 encoder states, and based on the previous state update the encoder position. this approach seems a little beyond the OPs current understanding.

a simpler starting point is to simply configure an interrupt on the RISING edge of one input and read the other input to determine the direction

my end goal is to Design a linear actuator that has a rotary encoder as a position sensor. It should be able to travel to a commanded position ( say 20 cm when commanded).

the type of encoder algorithm and encoder depends on the resolution, 20 cm +/- ???

Take a look at This Encoder Library that uses interrupts and the State Table Encoder Method.

actually i am using Esp32s3 and programming from Platfrom IO ( using Arduino Environment in platform IO ) in VScode

I recently employed an encode and had NO ISR. I will look for it later and post back here if the OP has not resolved hgis issue.

i have an application that uses an encoder turned by hand and found that along with the other processing i needed to use an interrupt.

i needed to configure CHANGE interrupts on both inputs on the simple ky-040 encoder i used to track each indent

now where should i have modify in code? should i have to call separately for both pins???

look this over
(compiles but don't have hardware to test with

const byte signalA = 4;
const byte signalB = 5;

volatile long int reader = 0;

void Roatar ()
{
    if (HIGH == digitalRead (signalB))
        reader++;
    else
        reader--;
}

void loop ()
{
    noInterrupts ();
    int cnt = reader;
    interrupts ();

    Serial.println (cnt);
    delay (500);
}

void setup ()
{
    Serial.begin (9600);

    pinMode (signalA,INPUT_PULLUP);
    pinMode (signalB,INPUT_PULLUP);
    attachInterrupt (digitalPinToInterrupt (signalA), Roatar, RISING);
}

it didn’t work

what did it do?

when i flashed the code to esp32s3 then after that what I did was just opening serial monitor and there was printed "Anti-clockwise 0" and if I rotate to clockwise the name anti clockwise changes to clockwise and 0 increments by -1... then after that what i did was pushed the button esp32s3 the "RST" button and now the serial monitor is showing "Anticlockwise 0" and this time I turned to anticlockwise. the name Anticlockwise changed to clockwise and increments by -1....

does the code i posted and are presumably referring to have any print statements that produce "Anticlockwise"?

again, I copied your code and pasted and flashed into esp32s3 same thing happens...may there is a hardware problem.... the code which first posted at the beginning actually that work with my previous rotary encoder. when turned clockwise it will prompt clockwise and if otherwise it will prompt anticlockwise but sometimes when rotate two or three turns it shows wrong direction... but now Iam using another rotary encoder module

Try the SingleEncoder example from the link I provided in Post #7.

All you have to do is change the '2' and '3' in this line:
NewEncoder encoder(2, 3, -20, 20, 0, FULL_PULSE);
To the pin numbers you're using for your ESP32.

If you get two counts per encoder detent, change FULL_PULSE to HALF_PULSE.

The way you are reading the encoder, there is NO reason to have signalB generating an interrupt. You'd get the same result by removing that interrupt. However, you are also using only 1/4 of the resolution of the encoder. If you want full resolution, you need to have BOTH interrupts, but separate interrupt handlers for each.

I don't have the same hardware as user123-name_valid, but I managed to test your code satisfactorily.

I used an Arduino Uno R3, so I changed the pins to 2 and 3.

I fed two 2Hz square waves from a function generator into the two inputs.

With the phase of the second square wave at 90°, the code counted upwards.
With the phase changed to 270°, the code counted downwards.