but the code seems to be infinite loop there, and didn't go back whatever I turn the encoder.
this code beep but cant stop even turn the Encoder.
Are both codes behaving the same? Is this a change from the posting where you said the code was working as desired?
Your Beep() function is blocking with the delay(), and the encoder can not be read during the two and a half seconds of delay().
Are you saying that when you turn the encoder so it goes from 0 to -1 and the beeping starts, you can not make the reverse rotation to get back to position 0 and stop the beeping?
What does your serial print tell you about the encoder position.
The code didn't beep when my posting, the serial.print OK.
now the Serial.print still OK, keep print "!!!!!!!!!!!!!!!!!" when met the Encoder read '-1', and keep doing there, never go back whatever Encoder turns.
You are not running the posted code as the -1!!!!!!! is not possible output.
The issue may be related to the encoder being used, and the detent pattern. My encoder has one change with every detent, and I have to turn it slowly so that every change is read by the non responsive program. On a different encoder with all 4 changes between detents, you may not be picking up the A pin transition.
What encoder do you have? Are there detents? Do you know the AB pattern at the detents?
What do you see with this code if you go between detents?
As I said, I can not confirm your results with my encoder, and I think the issue may be with the detent positions and the ability of your non responsive code to pick up the transitions.
What model of encoder do you have?
What do you see when your run the detent detection code I posted in my last post?
What are you trying to do with your encoder? Have you written a simple program which just counts up and down without the beeping at -1?