Hollow Shaft Encoder on Stepper Motor Shaft

I attached a hollow shaft encoder to my stepper motor and I am trying to get feedback. I tried using the code below, but it is too sensitive that as soon as I plug my stepper motor into power, I get incrementing values for "counter". The needed accuracy is 1 degree. How can I modify my code to make it work?

volatile long temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder
    
void setup() {
  Serial.begin (9600);
  pinMode(2, INPUT_PULLUP); // internal pullup input pin 2 
  
  pinMode(3, INPUT_PULLUP); // internalเป็น pullup input pin 3
   //Setting up interrupt
  //A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
  attachInterrupt(0, ai0, RISING);
   
  //B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
  attachInterrupt(1, ai1, RISING);
  }
   
  void loop() {
  // Send the value of counter
  if( counter != temp ){
  Serial.println (counter);
  temp = counter;
  }
  }
   
  void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if(digitalRead(3)==LOW) {
  counter++;
  }else{
  counter--;
  }
  }
   
  void ai1() {
  // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
  // Check with pin 2 to determine the direction
  if(digitalRead(2)==LOW) {
  counter--;
  }else{
  counter++;
  }
  }

Is your motor a 200 step per revolution motor? If so it will not do 1 degree steps (1.8 degree). Is your encoder's pulses per revolution an even multiple of 200? If not, it may not be usable with your motor.
Post some datasheet links or brand name / part numbers.

More fundamentally, why do you need an encoder with a stepper motor? - just keep count of the steps the motor is instructed to move.

If the motor is missing steps then it is likely that a more powerful motor is needed.

...R

  //A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
  attachInterrupt(0, ai0, RISING);
   
  //B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
  attachInterrupt(1, ai1, RISING);

Immediately indicates the error.

You cannot correctly handle a quadrature encoder by only registering rising edges.

You have to use CHANGE for the interrupt so all transitions are counted correctly, otherwise vibration
or oscillation will count endlessly rather than switching back and forth by 1 count as would be correct.

If you use a stepperdriver which can do microstepping you can increase the number of steps per revolution.

If your motor has 200 fullsteps per revolution an A4988-stepperdriver can be configurated to do
halfsteps = 400 steps per rev 360 / 400 = 0,9 degrees
quartersteps = 800 steps / rev = 360 / 800 = 0,45 degrees
1/8-steps = 1600 steps / rev = 360 / 1600 = 0,225 degrees
1/16-steps = 3200 steps per rev = 360 / 3200 = 0,1125 degrees.

"normal" stepper-motors are used without any feedback. They have to be dimensioned that they never get an overloading torque otherwise they will loose steps.

If this is the case you replace the stepper-motor with another stepper-motor which has more torque

The A4988 or the DRV8825 stepperdriver just needs a step and direction-input = 2 IO-pins.

If you need high RPMs (more than 1000-2000 rpm) real servo-motors are better suited.
Real servo-motor need a much higher sophisticated driver which has a combined PID-controller for speed and acceleration
best thing with servo-motors is to by one with integrated controller. This servo-controller just needs two inputs steps and direction.

If you post your real application a much more precise reommendation can be given

best regards Stefan