Calling encoder value from other function to void loop

Hello
i'm a new in this forum

i need some guide here for a better understanding on using several function.

i want to build system that measure the value of encoder. with that value, i want to decide the motor will run again or stop right there and wait for other signal (in my case i'm using push button).

it's works perfectly for my first practice using code from intrutables.

but when i want use that code in other declare function (not in void loop), it act weirdly and don't want count straight forward (when i turn clock wise, the counter not always adding sometimes it decrease and then adding again).

here the code i use right now

thank you for reading
regards

code encoder in others function.txt (1008 Bytes)

 #define outputA 4
 #define outputB 7
 int aState;
 int aLastState;
 int counter ;   
 
 void setup() { 
   pinMode (outputA,INPUT);
   pinMode (outputB,INPUT);
   
   Serial.begin (9600);
   // Reads the initial state of the outputA
   aLastState = digitalRead(outputA);   
 } 
  int encoder(){

   aState = digitalRead(outputA); // Reads the "current" state of the outputA
   // If the previous and the current state of the outputA are different, that means a Pulse has occured
   if (aState != aLastState){     
     // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
     if (digitalRead(outputB) != aState) { 
       counter ++;
     } else {
       counter --;
     }

   } 
   aLastState = aState; // Updates the previous state of the outputA with the current state
 return counter;
 }
 
 void loop() { 

   int encodervalue = encoder();
     Serial.print("Position: ");
     Serial.println(encodervalue);
 }

You may want to think about using a pin change interrupt to count encoder ticks. They can be quite fast and your polling code spends a lot of time not reading the encoder while doing time-consuming serial operations.

Does this behave any differently?

const int ENC_PHA   4
const int ENC_PHB   7

#define TICK_OCCURED    1

unsigned long
    encoderValue;   
 byte
    bFlags;
    
void setup( void ) 
{ 
    pinMode ( ENC_PHA,INPUT );
    pinMode ( ENC_PHB,INPUT );
    attachInterrupt(digitalPinToInterrupt(ENC_PHA), EncPHAInterrupt, FALLING);  
    bFlags = 0;
    Serial.begin (9600);
         
}//setup

//DIR CW (arbitrary assignment)
//  ___     ___     ___     ___     ___     ___
//_| A |___|   |___|   |___|   |___|   |___|   
//    ___     ___     ___     ___     ___     ___
//___| B |___|   |___|   |___|   |___|   |___|   

//DIR CCW
//    ___     ___     ___     ___     ___     ___
//___| A |___|   |___|   |___|   |___|   |___|   
//  ___     ___     ___     ___     ___     ___
//_| B |___|   |___|   |___|   |___|   |___|   
//
void EncPHAInterrupt( void )
{
    if( digitalRead(ENC_PHB) == HIGH )
        encoderValue++;    //based on above dwg, +ve is CW
    else
        encoderValue--;    //based on above dwg, +ve is CCW
    
    bFlags |= TICK_OCCURED;
    
}//EncPHAInterrupt
 
void loop() 
{
    if( bFlags & TICK_OCCURED )
    {
        Serial.print("Position: ");
        Serial.println(encoderValue);
        noInterrupts();
        bFlags &= ~TICK_OCCURED )
        interrupts();
        
    }//if
    
 }//loop

thank you for your response sir

i think i cannot use interupt mode sir because i need to move motor until some value on encoder reached.
my project is automatic storage system.
and i think if i use interrupt mode, it will make my motor run and stop continuously because that interrupt (or maybe i'm wrong??).

but i have some question with your code sir
what this code mean sir?
void EncPHAInterrupt( void ) ?

and this
bFlags |= TICK_OCCURED;

and also this
if( bFlags & TICK_OCCURED )

i need some guidance for this language
thanks for your kindness