I have quickly put together the following sketch to see what values I get from A0 connected to the slider of a 10kOhm pot in series with a 10kOhm reistor between 5v and gound:
#define potPIN A0
int potVal;
int potLastVal;
void setup() {
// put your setup code here, to run once:
pinMode(potPIN, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
potVal = analogRead(potPIN);
delay(50);
if (potVal != potLastVal) {
Serial.println(potVal);
delay(1000);
}
potLastVal = potVal;
Serial.print("potLastVal = ");
Serial.println(potLastVal);
}
Two things from the output on the serial monitor are not clear to me:
Sometimes, a reading is repeated, two, three or even five times. Why would this be?
It isn't. One small difference could be that when the pot is down at zero, it consistently reads zero each time. But in other positions, the reading might be slightly different, by perhaps 1 or 2, each time it is read. That's normal with analog inputs.
As @alto777 said, if you read your code carefully, line by line, and imagine in your head what would actually happen as each line executes, you'll see that it is behaving as expected. Code is not magic, it's just a list of instructions that get executed in the order you write them in. Try it!
Set your pot value approximately in the middle and then check that the print out on the Serial Moniotr agrees with the following Flow Chart. If yes, then your sketch is beahaving the way you have asked it to behave. If you want that the sketch should do something else, then change your codes accordingly. In that case, the Flow Chart might be helpful for you to visualize what is going on during the excution of the codes of your sketch.
The resistive element in the pot MUST be connected to the end pins someway. Many times it is a metal rivet, so the slider is resting on the metal rivet, not the resistive element. Sometime you need to disassemble a pot and see how it is constructed.