Bad contacts in wiring? Incorrectly wired?
Try adding some Serial.println() calls to strategic locations in your code, then run it with Serial Monitor open. That will allow you to see what it happening in your program. Knowing whether the problem is caused by not getting readings from the encoder, or whether it is getting the readings and sending the key presses, will be very helpful in troubleshooting.
Tried that and it seemed like that the code was not going to seperate "left" and "right" functions at all.
The main question is that how the sketch works in one board but not in the other two?
It's not clear to me what you mean by that.We could probably answer that question if you provide a detailed description of what you observed from the Serial output and what change you made to the code to fix it.
The point was that if sketch works in one board then it should work in all identical boards. Right?
Clearly not. We have no way of knowing if each is truly identical. It might be that you have updated some software after the time you uploaded to the first board and that changed the behavior. It might be that you have a floating pin that just happens to be steered the right way on one board but not the others.Look, we don't have your board in front of us. We are trying to help you remotely and so the only information we have to go on is the very little you have provided. The more relevant information you can give us, the more likely it is we can help you. Presumably, you came here hoping to get help with your problem. So when someone who is trying to help you asks for information, why not just provide the information?