Input Calibration

Hi all,
So I'm trying to make some code that'll measure and read out the values of a joystick in a neutral position for a calibration. My main issue is that I'd really like these measurements to be global variables. However, with the code below, Serial.println(xMeasure) and Serial.println(yMeasure) return 0. When I move int xMeasure = calibrate(xPin); and int yMeasure = calibrate(yPin); inside the loop, everything works perfectly, but then they run every time loop runs.
I'm using a Parallax 2-axis joystick with range 0-1000 for both potentiometer axes.

Thanks for any help,
Chris

 const int yPin = A0;
const int xPin = A1;

int xMeasure = calibrate(xPin);
int yMeasure = calibrate(yPin);
  
void setup() {
  Serial.begin(9600); 
}

void loop() {

  Serial.println(xMeasure);
  Serial.println(yMeasure);

}

int calibrate(int pin) {
  
  const int testNum = 25;
  int measure = 0;
  int i = 0;
  
  pinMode(pin,INPUT);  
  do{
    measure += analogRead(pin); 
    i++;  
  }while (i < testNum);
  measure = measure/testNum;
  return measure;  
}

Because you can't call a function to initialize a global variable. Put:

xMeasure = calibrate(xPin);
yMeasure = calibrate(yPin);

into your setup().

Regards,
Ray L.

RayLivingston:
Because you can't call a function to initialize a global variable. Put:

You can!

int q()
{
  return 7;
}

int p = q();

void setup() {
 Serial.begin(115200);
}

void loop() {
 Serial.println(p);
 delay(1000);
}

This prints the digit 7 every second.

The problem I see is that calibrate() is not defined until after it is used! I'm surprised the OP's code actually compiles.

Edit: Interesting - the forward reference seems to be allowed in Arduino IDE 1.6.5 without the need for a prototype to be defined.

Edit 1: Using avr-gcc in AVR Eclipse does not allow by default that forward reference. Is there a compilation switch that Arduino uses to allow it?

Edit: Interesting - the forward reference seems to be allowed in Arduino IDE 1.6.5 without the need for a prototype to be defined.

Look at the cpp file created from the ino file. You'll be signing a different tune.

Figuring out forward references is one of the nice pieces of assistance that is provided by the Arduino IDE.

...R

Robin2:
Figuring out forward references is one of the nice pieces of assistance that is provided by the Arduino IDE.

...R

I learn something new every day. I have just found and read Redirecting .