Reading the values on A0

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:

  1. Sometimes, a reading is repeated, two, three or even five times. Why would this be?

184
potLastVal = 184
potLastVal = 184
185
potLastVal = 185
184
potLastVal = 184
potLastVal = 184
potLastVal = 184
potLastVal = 184
potLastVal = 184
potLastVal = 184
183
potLastVal = 183
potLastVal = 183
potLastVal = 183
184
potLastVal = 184
182
potLastVal = 182
183
potLastVal = 183
potLastVal = 183
184
potLastVal = 184
182
potLastVal = 182
184
potLastVal = 184

  1. When the pot is turned fully clockwise the value of zero is shown without the rest of the loop functioning:

potLastVal = 0
potLastVal = 0
potLastVal = 0
potLastVal = 0
potLastVal = 0
potLastVal = 0
potLastVal = 0.............

Again, why is this?

Why would it not be repeated? Your code does not prevent it from being repeated.

What makes you think the rest of the loop is not functioning? Looks to me like it's functioning fine.

Put your finger on the code and step through it just like your Arduino does.

As @PaulRB observes, your code is doing… exactly what you wrote, even if it was not what you meant.

a7

Why would it not be repeated? Your code does not prevent it from being repeated.

I cannot improve the code to stop it being repeated if I don't know whiy it is repeating. Which is why I am asking for help in identifying the cause.

What makes you think the rest of the loop is not functioning? Looks to me like it's functioning fine.

The let me re-phrase my question and ask why the value 0 is being treated by the code differently than all the values between 1 and 508?

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!

@steveinaustria

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.

Why wouldn't it? If you are not moving the pot then that just means the value is stable. Bouncing 1 or 2 steps is normal.

The rest of the loop is functioning or it wouldn't be printing potLastVal. If it is stable at 0 then that is all it will print.

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.

I think you meant to write:

  void loop() {
    // put your main code here, to run repeatedly:
    potVal = analogRead(potPIN);
    delay(50);
    if (potVal != potLastVal) {
      Serial.print("potLastVal = ");
      Serial.println(potLastVal);
      potLastVal = potVal;
      Serial.println(potVal);
      delay(1000);
    }
  }

[/quote]