analogRead always returns 1023 - Arduino Mega 2560
I had a similar problem when I ported my code from an Arduino Due to an Arduino Mega. I was able to solve this problem by adding the following call in the setup function.

analogReference(DEFAULT);


It seems that in some other Arduinos, the analog reference will be selected automatically, but at least the Arduino Mega needs it.