AttachInterrupt does not work with out Port Monitoring.

Hello everyone!
Arduino Uno and optical encoder does not work correct with out Port Monitoring.
Here is a code:

volatile unsigned int 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++;
}
}

If I just load the code all work fine, after reboot AttachInterrupt does not work, until I open Port Monitoring.

If I add some other things in code, they work fine to.

1 month ago this code work fine and was no problem with it. Try to use another arduino Uno try different power supply: 5v, 12v, USB?, not helps.

Take another code for encoder to:
//these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;

volatile int lastEncoded = 0;
volatile long encoderValue = 0;

long lastencoderValue = 0;

int lastMSB = 0;
int lastLSB = 0;

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

pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);

digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on

//call updateEncoder() when any high/low changed seen
//on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);

}

void loop(){
//Do stuff here

Serial.println(encoderValue);
delay(1000); //just here to slow down the output, and show it will work even during a delay
}

void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit

int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;

lastEncoded = encoded; //store this value for next time
}

The same situation

Please read How to use this forum - please read. - Installation & Troubleshooting - Arduino Forum, specifically point #7 about posting code. Next edit your posts and add [code] and [/code] around the code.

What is "port monitoring"?

Regarding the first code, you set the interrupts to react on a rising edge. In the ISR, you check if the signal is LOW; although not impossible after a rising edge, the signal will probably be HIGH unless your signal has very short pulses. Which brings the question which optical decoder you are using?