Analog variables?

In the code below I created a global variable to an analogRead.

 int potValue  = analogRead(potPin);

If I try to call potValue at anytime in the code I get a return of “0” which I assume is what it will always read during boot. The only way I can get a read on the analog pin is to call analogRead within each function and even within each “if” or “while” statement.

Calling analogRead is not a problem for me, I just want to know if there is a way to create a variable that will be easier to use?

//shifter buttons
const byte parkPin = 13;

const byte extendPin = 6;
const byte retractPin = 7;


//shifter positon through feedback
const byte potPin = A0;
int potValue  = analogRead(potPin);

void setup() {
  // put your setup code here, to run once:
  pinMode (parkPin, INPUT_PULLUP);

  pinMode (extendPin, OUTPUT);
  pinMode (retractPin, OUTPUT);

  Serial.begin (9600);
}

void loop() {
  // put your main code here, to run repeatedly:

  potValue = analogRead(potPin);
  Serial.println(potValue);
  Serial.println("loop");

  if (digitalRead(parkPin) == LOW)
    Park();
  else {
    digitalWrite (extendPin, HIGH);
    digitalWrite (retractPin, HIGH);
  }
}

void Park() { //hookup motion sensor to verify no motion?
  while (analogRead(potPin) > 655) {
    digitalWrite (extendPin, LOW);
    Serial.println("extend");
    Serial.println(potValue);
  }
  while (analogRead(potPin) < 655) {
    digitalWrite (retractPin, LOW);
    Serial.println("retract");
    Serial.println(potValue);
  }
  digitalWrite (extendPin, HIGH);
  digitalWrite (retractPin, HIGH);
}

Having this before setup() does nothing for you int potValue = analogRead(potPin); Just have int potValue; that's all you need.

In the code below I created a global variable to an analogRead.

No, you created a global variable, and assigned it a completely useless value. The analogRead() function is not useful when the variable memory is allocated and initialized.

That statement does NOT bind the value of the variable to the function.

If I try to call potValue at anytime in the code

You don't call variables. You call functions. You reference variables.

The only way I can get a read on the analog pin is to call analogRead within each function and even within each "if" or "while" statement.

That's as it should be.

I just want to know if there is a way to create a variable that will be easier to use?

If you mean that you want a variable that always contains the latest value read from the analog pin, no, that is not possible with C or C++.

As intimated, what you want is a function named potValue() that will read the pot and return the value.

If you have a lot of places where you read the pot, it can save code space by creating the function. It also makes it easier to maintain the code. For example, if you want to scale every pot read you only have to change it in one place:

int potRead(void)
{
    return analogRead(potPin);
}

Thanks, I learned something today! I'll play around with the potRead function, it may be useful as the code grows.

PaulS: If you mean that you want a variable that always contains the latest value read from the analog pin, no, that is not possible with C or C++.

Not quite true. You can fake that kind of effect by creating a class and implementing operator unsigned int() to call and return an analogRead() result.

This is quite an advanced technique though.

Jiggy-Ninja: Not quite true. You can fake that kind of effect by creating a class and implementing operator unsigned int() to call and return an analogRead() result.

This is quite an advanced technique though.

I'd shoot you if you did that, and expected me to help you debug your code.

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg.

Bjarne Stroustrup

You could also fake it with a pre-processor define:

#define potValue analogRead(potPin)

Except by convention it should be in all caps: POT_VALUE.

But that's the "shooting in the foot"-part... If you do that, you might expect potValue to be potValue. But

potValue == potValue;

Might as well evaluate false although you easy would assume it's always true.

So with C++, (my advice) don't use macro's unless you really have no other way.

Good point, the Macro should be named GET_POT_VALUE, or something.