Wire + Serial + Rotary Encoder crashes serial communication

Hey everyone,

I have a board with 3 PCF8574 for expanding Arduino ports, which I access through Wire protocol. I also have a rotary encoder connected to pins 2 and 4 (not the expanded ones). The project is working fine, but when I move the rotary encoder, some time after, my Serial connection simply crashes. I have to close the Serial Monitor and open it again for it to work. The weird part is that if I don't use the Wire interface or don't use the rotary encoder, everything works fine. When both are working together, it also works, but after 5 seconds messing with the rotary encoder, the serial connection crashes.

Anyone has an idea on what is going on? The source code is quite large, that's why I didn't posted it, but if you want, I'll be happy to provide.

Regards,
Fergo.

my Serial connection simply crashes.

This is highly unlikely. "My Arduino crashes, ending serial communications" is much more likely.

but if you want, I'll be happy to provide.

You only need to do that if you want help solving the problem.

Fergo:
Hey everyone,

I have a board with 3 PCF8574 for expanding Arduino ports, which I access through Wire protocol. I also have a rotary encoder connected to pins 2 and 4 (not the expanded ones). The project is working fine, but when I move the rotary encoder, some time after, my Serial connection simply crashes. I have to close the Serial Monitor and open it again for it to work. The weird part is that if I don't use the Wire interface or don't use the rotary encoder, everything works fine. When both are working together, it also works, but after 5 seconds messing with the rotary encoder, the serial connection crashes.

What is the last thing that gets printed? Ohhh wait, we need to see the code for that information to make sense. Then... I don't know. When you close and open the serial monitor, you reset the Arduino... so it starts fresh.
How did you implement the encoder readings?

Fergo:
Anyone has an idea on what is going on? The source code is quite large, that's why I didn't posted it, but if you want, I'll be happy to provide.

Regards,
Fergo.

Two people might have already found the problem if you posted the code...

Same problem here is code. Its from the examples page here Arduino Playground - RotaryEncoders

#define encoder0PinA 2
#define encoder0PinB 7

volatile unsigned int encoder0Pos = 0;

void setup() {

pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pullup resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pullup resistor

attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
Serial.begin (9600);

}

void loop(){
// do some stuff here
}

void doEncoder() {
/* If pinA and pinB are both high or both low, it is spinning

  • forward. If they're different, it's going backward.
    */

if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
encoder0Pos++;
} else {
encoder0Pos--;
}

Serial.println (encoder0Pos, DEC);
}

I've also changed the interrupt to RISING and did a if digitalRead(encoder0PinB) == HIGH with the same results.

It goes good for a little bit, 5-20 seconds then freezes.

If sorry if I am not supposed to resurrect this old thread.

void doEncoder() {
/* If pinA and pinB are both high or both low, it is spinning

  • forward. If they're different, it's going backward.
    */

if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
encoder0Pos++;
} else {
encoder0Pos--;
}

Serial.println (encoder0Pos, DEC);
}

Really not a good idea in an interrupt handler...

Yep

I put the print in the main loop and all is good.

Thanks