Using pointers to variables

Corrosion has hit parts of my programming. I would like pass pointers to 2 variables to a find_min_max function. The function should monitour a pot and discouver the min and max values. The pot is of 10 kOhm, connected to +5 resp. GND each via 470 Ohm. This way the A/D values will not go down to zero, not reach 1023. This is for pot failure detection. Values outside the allowed range will cause special actions.

In Setup the calibration is called for X-, Y- resp. Z-axis. Parameters are Analog channel, ?_max, ?_min.
Reading Arduino Reference regarding pointers was not help enough.

Beginners mistake…. Can someone assist me? Code below.

#include<arduino.h>

//I2C for LCD
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp mylcd; // declare lcd object: auto locate & config exapander chip

// LCD geometry
#define LCD_COLS 20
#define LCD_ROWS 4

boolean state, old_state;
int x_min, y_min, z_min = 1023;
int x_max, y_max, z_max = 0;

boolean centered (int chan)
{
  return (( analogRead(chan) > 512 - 25) && ( analogRead(chan) < 512 + 25));
}

void find_min_max(int A, int *minimum, int *maximum)
{
  int tmp;
  mylcd.print("Max ");
  while (centered(A)) {}; // wait for leaving center

  while (!centered(A)); //As longs pot is in the active range
  {
    tmp = analogRead(A);
//    Serial.print("Max "); Serial.print(A); Serial.print(" ");Serial.println( tmp);
    if ( tmp > maximum) maximum = tmp;
  }
  mylcd.print(*maximum); mylcd.print(" Min ");
  
  while (centered(A)) {}; //Wait while pot is in the center
  while (!centered(A)) //As longs pot is in the active range
  {
    tmp = analogRead(A);
//    Serial.print("Min "); Serial.print(A); Serial.print(" ");Serial.println( tmp);
    if ( tmp < minimum) minimum = tmp;
  }
  mylcd.print(*minimum);
}

//The setup function is called once at startup of the sketch
void setup() {
  Serial.begin(115200);

  //1Hz 90% dutycycle
  pinMode(9, OUTPUT);                               // Set digital pin 9 (D9) to an output
  TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM11);  // Enable the PWM output OC1A on digital pins 9 and invert output
  TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12);     // Set fast PWM and prescaler of 256 on timer 1
  ICR1 = 62499;                                     // Set the PWM frequency to 1Hz: 16MHz/(256 * 1Hz) - 1 = 62499
  OCR1A = 6249;                                     // Set the duty-cycle to 10%: 62499 / 10 = 6249

  pinMode(13, OUTPUT);                               // 1 Hz pulse output on digital pin 9 (D9)

  delay(10);//allow pwm timers to start

  int status;

  status = mylcd.begin(LCD_COLS, LCD_ROWS);
  if (status) // non zero status means it was unsuccesful
  {
    status = -status; // convert negative status value to positive number

    // begin() failed so blink error code using the onboard LED if possible
    hd44780::fatalError(status); // does not return
  }
  mylcd.clear();

  // Print start message to the LCD
  mylcd.print("Started ");
  delay(1000);

  mylcd.clear();
  mylcd.print("Calibrate ");
  delay(2000);
  
//Calibrate X axis
  mylcd.setCursor(0, 1);
  mylcd.print("X C ");
  while (!centered(A0));
  find_min_max(A0, &x_min, &x_max);
  Serial.print("X Max "); Serial.print(x_max); Serial.print(" ");Serial.println( x_min);

//Calibrate Y axis
  mylcd.setCursor(0,2);
  mylcd.print("Y C ");
  while (!centered(A1));
  find_min_max(A1, &y_min, &y_max);
  Serial.print("Y Max "); Serial.print(y_max); Serial.print(" ");Serial.println( y_min);

//Calibrate Z axis
  mylcd.setCursor(0,3);
  mylcd.print("Z C ");
  while (!centered(A2));
  find_min_max(A2, &z_min, &z_max);
  Serial.print("Z Max "); Serial.print(z_max); Serial.print(" ");Serial.println( z_min);

//  mylcd.print("Calibration ready.");

}

// The loop function is called in an endless loop
void loop()
{
  if (( millis() % 1000 ) > 499 )
  {
    state = true;
  }
  else
  {
    state = false;
  }

  if ( old_state != state )
  {
    mylcd.setCursor(0, 1);
    if ( state == HIGH )
    {
      mylcd.print("On ");
      digitalWrite(13, HIGH);
    }
    else
    {
      mylcd.print("Off");
      digitalWrite(13, LOW);
    }
    old_state = state;
  }
}// End of loop

void find_min_max(int A, int &_minimum, int &_maximum)

No need to add pointers with the function call

find_min_max( A, minimum, maximum);

I, also, like to change the name of the variables in the working function such as adding a preceeding underline, to avoid the redefining error.

Calling a function with poiners:

void fGetUTM(  void * parameter )
{
  struct stuUTM pxMessage;
  float tmpUTM_North = 0.0; // holds northing info
  float tmpUTM_West = 0.0; // holds easting info
  int tmpZone = 0; // holds zone info
  for (;;)
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, DO_UTM_TASK_BIT, pdTRUE, pdTRUE, portMAX_DELAY) ;
    if ( xSemaphoreTake( sema_UTM, xSemaphoreTicksToWait ) == pdTRUE )
    {
// the call to a function that takes pointers
      tmpZone = LatLonToUTMXY(xUTM_Posit.Lat, xUTM_Posit.Lon, 0, tmpUTM_North, tmpUTM_West );
      pxMessage.UTM_North = tmpUTM_North;
      pxMessage.UTM_West = tmpUTM_West;
      pxMessage.iZone = tmpZone;
      xQueueOverwrite( xQ_UTM_Message, (void *) &pxMessage );
      xSemaphoreGive( sema_UTM );
    }
  }
  vTaskDelete( NULL );
}

The function that takes pointers as its input variables.

int LatLonToUTMXY (float lat, float lon, int zone, float& x, float& y) {
  if ( (zone < 1) || (zone > 60) )
    zone = FLOOR((lon + 180.0) / 6) + 1;
  
  MapLatLonToXY (DegToRad(lat), DegToRad(lon), UTMCentralMeridian(zone), x, y);
  
  /* Adjust easting and northing for UTM system. */
  x = x * UTMScaleFactor + 500000.0;
  y = y * UTMScaleFactor;
  if (y < 0.0)
    y = y + 10000000.0;
  
  return zone;
}

Thanks!
I joined my first cource in programming 1973 and was programming for 35 years. After a 10 year break I want to get moving again.

Changing the call of the function find_min_max compiles but doesn’t solve the issue.

Also changing the definition of the function using “&” doesn’t compile. My current code:

#include<arduino.h>

//I2C for LCD
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp mylcd; // declare lcd object: auto locate & config exapander chip

// LCD geometry
#define LCD_COLS 20
#define LCD_ROWS 4

boolean state, old_state;
volatile int x_min, y_min, z_min = 1023;
volatile int x_max, y_max, z_max = 0;


//The setup function is called once at startup of the sketch
void setup()
{
  Serial.begin(115200);

  //1Hz 90% dutycycle
  pinMode(9, OUTPUT);                               // Set digital pin 9 (D9) to an output
  TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM11);  // Enable the PWM output OC1A on digital pins 9 and invert output
  TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12);     // Set fast PWM and prescaler of 256 on timer 1
  ICR1 = 62499;                                     // Set the PWM frequency to 1Hz: 16MHz/(256 * 1Hz) - 1 = 62499
  OCR1A = 6249;                                     // Set the duty-cycle to 10%: 62499 / 10 = 6249

  pinMode(13, OUTPUT);                               // 1 Hz pulse output on digital pin 9 (D9)

  delay(10);//allow pwm timers to start

  int status;

  status = mylcd.begin(LCD_COLS, LCD_ROWS);
  if (status) // non zero status means it was unsuccesful
  {
    status = -status; // convert negative status value to positive number

    // begin() failed so blink error code using the onboard LED if possible
    hd44780::fatalError(status); // does not return
  }
  mylcd.clear();

  // Print start message to the LCD
  mylcd.print("Started ");
  delay(1000);

  mylcd.clear();
  mylcd.print("Calibrate ");
  delay(2000);

  //Calibrate X axis
  mylcd.setCursor(0, 1);
  mylcd.print("X C ");
  Serial.print("X "); Serial.println(analogRead(A0));
  while (!centered(A0));
  find_min_max(A0, x_min, x_max);
  Serial.print("X Max "); Serial.print(x_max); Serial.print(" "); Serial.println( x_min);

  //Calibrate Y axis
  mylcd.setCursor(0, 2);
  mylcd.print("Y C ");
  Serial.print("Y "); Serial.println(analogRead(A1));
  while (!centered(A1));
  find_min_max(A1, y_min, y_max);
  Serial.print("Y Max "); Serial.print(y_max); Serial.print(" "); Serial.println( y_min);

  //Calibrate Z axis
  mylcd.setCursor(0, 3);
  mylcd.print("Z C ");
  Serial.print("Z "); Serial.println(analogRead(A2));
  while (!centered(A2));
  find_min_max(A2, z_min, z_max);
  Serial.print("Z Max "); Serial.print(z_max); Serial.print(" "); Serial.println( z_min);

  //  mylcd.print("Calibration ready.");
  Serial.print("Xmax "); Serial.print(x_max); Serial.print(" Xmin "); Serial.println(x_min);
  Serial.print("Ymax "); Serial.print(y_max); Serial.print(" Ymin "); Serial.println(y_min);
  Serial.print("Zmax "); Serial.print(z_max); Serial.print(" Zmin "); Serial.println(z_min);
  Serial.print("X "); Serial.println(analogRead(A0));
  Serial.print("Y "); Serial.println(analogRead(A1));
  Serial.print("Z "); Serial.println(analogRead(A2));

}
boolean centered (int chan)
{
  int tmp;
  tmp = analogRead(chan);
  tmp = analogRead(chan);

  return (( tmp > (512 - 25)) && (( tmp < 512) + 25));
}

void find_min_max(int A, int &minimum, int &maximum)
{
  int tmp;
  Serial.print(A); Serial.print(" "); Serial.print(analogRead(A)); Serial.print(" ");
  Serial.print(*minimum); Serial.print(" "); Serial.println(*maximum);
  while (centered(A)) {}; // wait for leaving center

  mylcd.print("Max ");
  while (!centered(A)) //As longs pot is in the active range
  {
    tmp = analogRead(A);
    tmp = analogRead(A);
 //   Serial.println( tmp);
    if ( tmp > maximum) maximum = tmp;
    //    delay(500);
  }

  while (centered(A)) {}; //Wait while pot is in the center

  mylcd.print(*maximum); mylcd.print(" Min ");
  while (!centered(A)) //As longs pot is in the active range
  {
    tmp = analogRead(A);
    tmp = analogRead(A);
//    Serial.println( tmp);
    if ( tmp < minimum) minimum = tmp;
    //    delay(500);
  }
  mylcd.print(*minimum);
}

// The loop function is called in an endless loop
void loop()
{
  if (( millis() % 1000 ) > 499 )
  {
    state = true;
  }
  else
  {
    state = false;
  }

  if ( old_state != state )
  {
    mylcd.setCursor(0, 1);
    if ( state == HIGH )
    {
      mylcd.print("On ");
      digitalWrite(13, HIGH);
    }
    else
    {
      mylcd.print("Off");
      digitalWrite(13, LOW);
    }
    old_state = state;
    Serial.print("X "); Serial.println(analogRead(A0));
    Serial.print("Y "); Serial.println(analogRead(A1));
    Serial.print("Z "); Serial.println(analogRead(A2));
  }
}// End of loop

Error message is

C:\Users\User\Documents\Arduino\CNC_manual_pots_190129a\CNC_manual_pots_190129a.ino: In function 'void setup()':

CNC_manual_pots_190129a:60: error: invalid initialization of reference of type 'int&' from expression of type 'volatile int'

   find_min_max(A0, x_min, x_max);

                                ^

C:\Users\User\Documents\Arduino\CNC_manual_pots_190129a\CNC_manual_pots_190129a.ino:97:6: note: in passing argument 2 of 'void find_min_max(int, int&, int&)'

 void find_min_max(int A, int &minimum, int &maximum)

      ^

CNC_manual_pots_190129a:68: error: invalid initialization of reference of type 'int&' from expression of type 'volatile int'

   find_min_max(A1, y_min, y_max);

                                ^

C:\Users\User\Documents\Arduino\CNC_manual_pots_190129a\CNC_manual_pots_190129a.ino:97:6: note: in passing argument 2 of 'void find_min_max(int, int&, int&)'

 void find_min_max(int A, int &minimum, int &maximum)

      ^

CNC_manual_pots_190129a:76: error: invalid initialization of reference of type 'int&' from expression of type 'volatile int'

   find_min_max(A2, z_min, z_max);

                                ^

C:\Users\User\Documents\Arduino\CNC_manual_pots_190129a\CNC_manual_pots_190129a.ino:97:6: note: in passing argument 2 of 'void find_min_max(int, int&, int&)'

 void find_min_max(int A, int &minimum, int &maximum)

      ^

C:\Users\User\Documents\Arduino\CNC_manual_pots_190129a\CNC_manual_pots_190129a.ino: In function 'void find_min_max(int, int&, int&)':

CNC_manual_pots_190129a:101: error: invalid type argument of unary '*' (have 'int')

   Serial.print(*minimum); Serial.print(" "); Serial.println(*maximum);

                 ^

CNC_manual_pots_190129a:101: error: invalid type argument of unary '*' (have 'int')

   Serial.print(*minimum); Serial.print(" "); Serial.println(*maximum);

                                                              ^

CNC_manual_pots_190129a:116: error: invalid type argument of unary '*' (have 'int')

   mylcd.print(*maximum); mylcd.print(" Min ");

                ^

CNC_manual_pots_190129a:125: error: invalid type argument of unary '*' (have 'int')

   mylcd.print(*minimum);

                ^

exit status 1
invalid initialization of reference of type 'int&' from expression of type 'volatile int'

Test prints in the function indicates that the sketch doesn’t get hold of the correct data. The x_min, x_max is not reached inside the function find_min_max. I still miss something.

Could You spell the definition of the function and the call of the function. The names minimum and maximum ought to local inside the function f_m_m….

invalid initialization of reference of type 'int&' from expression of type 'volatile int'

is, to me, the gist of the error message.

Instead of: volatile int x_min, y_min, z_min = 1023; volatile int x_max, y_max, z_max = 0;

Use:

int x_min, y_min, z_min = 1023; int x_max, y_max, z_max = 0;

This 'volatile int x_min' is not 'int x_min'. The function void find_min_max(int A, int &minimum, int &maximum) is looking for an input parameter of 'int &minimum' and not 'volatile int &x_min'.

If you are using a DUE, STM32, or ESP32 and you need the resource locking use uMT (DUE) or freeRTOS (STM32 or ESP32 and a SemaphoreMutex. I do not think you need resource locking as your code runs sequential and not adhoc as it could with an ESP32 (multi core) and to a lesser extent the DUE or the STM32.

The first attempt was like You suggest, int x_min….=1023. Trying volatile was a desperat gambling short ago. I use UNOs, mostly pirate copies… No other stuff...

The problem is that the variable sent when calling the function is not picked up by the function. Then of course the function shoul deposite the new values int the variable used in the call.4

I am not sure what you are trying to communicate in your last message. I do know that the example I sent you works.

What error message are you getting now?

Perhaps you can post your code changes for us to look at?

Some questions.

Why are x_min, y_min, z_min, x_max, y_max, and z_max declared volatile? The volatile keyword is used on variables that might be changed by interrupts. I see no interrupt handlers in your code.

Why do you think you need to pass global variables to a function?

If you really must, pointers are the way.

You only need to read the value once before setting both the minimum and maximum variables from
that one value - in fact you need to, otherwise you could end up with a situation where maximum < minimum,
which might cause confusion elsewhere in the code.

  while (!centered(A)) //As longs pot is in the active range
  {
    tmp = analogRead(A);
 //   Serial.println( tmp);
    if ( tmp > maximum) maximum = tmp;
    if ( tmp < minimum) minimum = tmp;
    //    delay(500);
  }

Thanks all of You for Your attention.

Look at the very first code I attached in the very beginning. Focus on the calls to the function "find_min_max" and the function find_min_max.

There are three calls made within Setup. The purpose is to measure and calibrate the swing of three pots for a CNC manual operation steering of the X-, Y- and Z- axis. The calibration is done by turning each pot from end to end and note the analog readings.

For safety reasons the three 10 kOhm pots the end terminals are connected to +5 resp. GND via 470 ohm resistors. This makes the A/D Reading swing between some 43 and 978. If a 5+, or GND, connection is lost the A/D reading will swing to either 0 or 1023. This will be detected as a severe pot failure and the operation of the CNC wil be stopped.

The code in short form:

find_min_max( pointer to x_min, pointer to x_max ) . . find_min_max( pointer to y_min, pointer to y_max ) . . find_min_max( pointer to z_min, pointer to z_max )

void find_min_max( A/D channel, pointed out min, pointed out max)

The function find_min_max( int A/D channel, pointer to min, pointer to max) will affect the min/max values for the pot being measured. Three different pots, one measuring function…

This way of handling controls (the pots) is a construction I made some 25 years ago being the chief designer for a Europen truck manufactoring company so I know very well what I want. The issue is how to send the proper pointer to the function and how to use the pointer inside the function. Pointing at X, Y or Z?

Do You get it? How to pass a pointer and how to use the pointer...

The code in short form:

find_min_max( pointer to x_min, pointer to x_max )

To find the max or min, in short or long form, you're going to need the current reading, as well as the current max and min.

No AWOL. Don't bother about that. I have been programming in real time and close hardware Environment for some 40 years. Now I don't find the proper syntax. I want to send a reference to 2 variables to be picked up and used by the function. I just can't find the right syntax for sending 2 pointers to 2 variables and using the pointers in the function. In the C-language it was commonly used. However I seem to make a mistake in the syntax for sending the pointer as well as picking up the pointer inside the function.

@Idahowalker I tried to pick up knowledge from Your example code in #1 but obviously I made some mistake. It ought be seen in my answer #2.

I want to send a reference to 2 variables

void fnr (int& var0, int& var1)
{
  // body of function here
  int local0 = var0;
...
  var1 = 2;
}

I just can't find the right syntax for sending 2 pointers to 2 variables and using the pointers in the function.

void fnp (int* var0, int* var1)
{
  int local0 = *var0; // fetch what var0 points to
...

  *var1 = 2;  // assign 2 to what var1 points to.
}

Thanks but it doesn’t work here. The int* in the functione definition doesn’t make me get the intended values, neither are the results stored in the x_min, x_max etc…
Focus att the definintion of the function find_min_max and the calls of the function. Then look up evetually interesting details. The code contains a lot of unused alternative code.

Message exceeds the linits. Printouts sent in next message.

Code for the moment:

#include<arduino.h>

//I2C for LCD
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp mylcd; // declare lcd object: auto locate & config exapander chip

// LCD geometry
#define LCD_COLS 20
#define LCD_ROWS 4

boolean state, old_state;
int x_min, y_min, z_min = 1023;
int x_max, y_max, z_max = 0;


//The setup function is called once at startup of the sketch
void setup()
{
  Serial.begin(115200);

  //1Hz 90% dutycycle
  pinMode(9, OUTPUT);                               // Set digital pin 9 (D9) to an output
  TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM11);  // Enable the PWM output OC1A on digital pins 9 and invert output
  TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12);     // Set fast PWM and prescaler of 256 on timer 1
  ICR1 = 62499;                                     // Set the PWM frequency to 1Hz: 16MHz/(256 * 1Hz) - 1 = 62499
  OCR1A = 6249;                                     // Set the duty-cycle to 10%: 62499 / 10 = 6249

  pinMode(13, OUTPUT);                               // 1 Hz pulse output on digital pin 9 (D9)

  delay(10);//allow pwm timers to start

  int status;

  status = mylcd.begin(LCD_COLS, LCD_ROWS);
  if (status) // non zero status means it was unsuccesful
  {
    status = -status; // convert negative status value to positive number

    // begin() failed so blink error code using the onboard LED if possible
    hd44780::fatalError(status); // does not return
  }
  mylcd.clear();

  // Print start message to the LCD
  mylcd.print("Started ");
  delay(1000);

  mylcd.clear();
  mylcd.print("Calibrate ");
  delay(2000);

  //Calibrate X axis
  mylcd.setCursor(0, 1);
  mylcd.print("X C ");
  Serial.print("X "); Serial.println(analogRead(A0));
  while (!centered(A0))
  {
    mylcd.setCursor(12, 0); mylcd.print(analogRead(A0)); mylcd.print("  ");
  }
  mylcd.setCursor(0, 1);
  mylcd.print("X   ");
  find_min_max(A0, 1, x_min, x_max);

  //  x_min = find_min(A0, 1);
  mylcd.setCursor(0, 2);
  mylcd.print("Y C ");
  while (!centered(A0));
  {
    mylcd.setCursor(12, 0); mylcd.print(analogRead(A0)); mylcd.print("  ");
  }
  mylcd.setCursor(0, 2);
  mylcd.print("Y   ");
  //  x_max = find_max(A0, 1);
  Serial.print("X Max "); Serial.print(x_max); Serial.print(" X Min "); Serial.println(x_min);

  //Calibrate Y axis
  mylcd.setCursor(0, 2);
  mylcd.print("Y C ");
  Serial.print("Y "); Serial.println(analogRead(A1));
  while (!centered(A1))
  {
    mylcd.setCursor(12, 0); mylcd.print(analogRead(A1)); mylcd.print("  ");
  }

  mylcd.setCursor(0, 2);
  mylcd.print("Y   ");
  find_min_max(A1, 2, y_min, y_max);
  //  y_min = find_min(A1, 2);
  //  while (!centered(A1));
  //  y_max = find_max(A1, 2);
  Serial.print("Y Max "); Serial.print(y_max); Serial.print(" Y Min "); Serial.println(y_min);

  //Calibrate Z axis
  mylcd.setCursor(0, 3);
  mylcd.print("Z C ");
  Serial.print("Z "); Serial.println(analogRead(A2));
  while (!centered(A2))
  {
    mylcd.setCursor(12, 0); mylcd.print(analogRead(A2)); mylcd.print("  ");
  }
  mylcd.setCursor(0, 3);
  mylcd.print("Z   ");

  find_min_max(A2, 3, z_min, z_max);
  Serial.print("Z Max "); Serial.print(z_max); Serial.print(" Z Min "); Serial.println(z_min);
  
  //  while (!centered(A2));
  //  z_min = find_min(A2, 3);
  //  z_max = find_max(A2, 3);
  mylcd.setCursor(12, 0); mylcd.print("    ");
  Serial.print("Z Max "); Serial.print(z_max); Serial.print(" "); Serial.println( z_min);

  mylcd.setCursor(0, 0);
  mylcd.print("Ready    ");
  Serial.print("Xmax "); Serial.print(x_max); Serial.print(" Xmin "); Serial.println(x_min);
  Serial.print("Ymax "); Serial.print(y_max); Serial.print(" Ymin "); Serial.println(y_min);
  Serial.print("Zmax "); Serial.print(z_max); Serial.print(" Zmin "); Serial.println(z_min);
  Serial.print("X "); Serial.println(analogRead(A0));
  Serial.print("Y "); Serial.println(analogRead(A1));
  Serial.print("Z "); Serial.println(analogRead(A2));

}
boolean centered (int chan)
{
  int tmp;
  tmp = analogRead(chan);
  delay(2);
  tmp = analogRead(chan);
  mylcd.setCursor(19, 0);
  if (( tmp > (512 - 25)) && ( tmp < (512 + 25)))
  {
    mylcd.print("C");
    return (true);
  }
  else
  {
    mylcd.print(" ");
    return (false);
  }
}
void find_min_max(int A, int line, int* minimum, int* maximum)
{
  int tmp;
  int mini = 1023;
  int maxi = 0;

  Serial.print(A); Serial.print(" "); Serial.print(analogRead(A)); Serial.print(" ");
  Serial.print(*minimum); Serial.print(" "); Serial.println(*maximum);
  while (!centered(A))
  {
    mylcd.setCursor(12, 0); mylcd.print(analogRead(A2)); mylcd.print("  ");
  }
  mylcd.setCursor(2, line);
  mylcd.print(" ");

  while (centered(A))
  {
    mylcd.setCursor(12, 0); mylcd.print(analogRead(A)); mylcd.print("  ");
  } // wait for leaving center

  mylcd.setCursor(12, line);
  mylcd.print("Max "); mylcd.print(" ");
  while (!centered(A)) //As longs pot is in the active range
  {
    tmp = analogRead(A);
    mylcd.setCursor(12, 0); mylcd.print(tmp); mylcd.print("  ");
    maxi = max( maxi, tmp);
  }
  mylcd.setCursor(12, line);
  mylcd.print("Max "); mylcd.print(" ");
  mylcd.print(maxi);

  mylcd.setCursor(2, line);
  mylcd.print("C");
  while (centered(A))//Wait while pot is in the center
  {
    mylcd.setCursor(12, 0); mylcd.print(analogRead(A)); mylcd.print("  ");
  }
  mylcd.setCursor(4, line);
  mylcd.print("Min ");

  mylcd.setCursor(2, line);
  mylcd.print("C");
  while (!centered(A)) //As longs pot is in the active range
  {
    //    tmp = analogRead(A);
    tmp = analogRead(A);
    mylcd.setCursor(12, 0); mylcd.print(tmp); mylcd.print("  ");
    if ( tmp < mini) mini = tmp;
  }
  mylcd.setCursor(2, line);
  mylcd.print(" ");
  mylcd.setCursor(4, line);
  mylcd.print("Min ");
  mylcd.print(mini);

  *maximum = maxi;
  *minimum = mini;

}

// The loop function is called in an endless loop
void loop()
{

  if ( centered(A0)) Serial.print("X C ");
  else Serial.print("XNC ");
  Serial.println(analogRead(A0));

  if ( centered(A1)) Serial.print("Y C ");
  else Serial.print("YNC ");
  Serial.println(analogRead(A1));

  if ( centered(A2)) Serial.print("Z C ");
  else Serial.print("ZNC ");
  Serial.println(analogRead(A2));

  delay(1000);

  /*
    if (( millis() % 1000 ) > 499 )
    {
      state = true;
    }
    else
    {
      state = false;
    }

    if ( old_state != state )
    {
      mylcd.setCursor(0, 1);
      if ( state == HIGH )
      {
        mylcd.print("On ");
        digitalWrite(13, HIGH);
      }
      else
      {
        mylcd.print("Off");
        digitalWrite(13, LOW);
      }
      old_state = state;
      Serial.print("X "); Serial.println(analogRead(A0));
      Serial.print("Y "); Serial.println(analogRead(A1));
      Serial.print("Z "); Serial.println(analogRead(A2));
    }
  */
}// End of loop

Serial print outs: my comments, added to print out

X 43                 X pot initial value
14 494 239 239          I/O A0 center value, what min pointer points at, what max pointer points at
X Max 0 X Min 0          Prinout from Setup after return from function find_min_max
Y 43                           X pot initial value
15 492 239 239          I/O A1 center value, what min pointer points at, what max pointer points at
Y Max 0 Y Min 0          Prinout from Setup after return from function find_min_max
Z 977                        Z pot initial value
16 536 -12627 239     I/O A2 center value, what min pointer points at, what max pointer points at
Z 
Ma0 Z Min 1023          Prinout from Setup after return from function find_min_max
Z Max 0 1023             disturbed some way.
Xmax 0 Xmin 0            Ought to be 978 resp 43
Ymax 0 Ymin 0
Zmax 0 Zmin 1023

I have been programming in real time and close hardware Environment for some 40 years

And in all that time, you never came across the concept of the MCVE? :o

What is wrong with this picture?

int x_min, y_min, z_min = 1023;
int x_max, y_max, z_max = 0;
...

void find_min_max(int A, int line, int* minimum, int* maximum)
...
...
 find_min_max(A0, 1, x_min, x_max);
{

Thanks AWOL! I got it working a minute ago due to Your last post, given days ago…...

I don't try to show off but when helpers are out on the wrong track I don't want them to spend more energy on that track. Then i write something about my background. Nowdays I face corrosion and aging.

Reading Your last post... What is MCVE? Never heard about it before.

Whats was wrong with Your Picture....? Nothing! It works since a few minutes ago! Earlier I obviously made a mistake somewhere so the picture didn't work at that time. I don't expect You or anyone else to find a deaply laying tilting mistake.

Thanks for Your last post! Ii made me think "what the hell...." and I tried again. This time being being successful coding properly in every detail.

Should have sent You ++ a number of times. I'll make it happend.

If a 5+, or GND, connection is lost the A/D reading will swing to either 0 or 1023.

That is NOT a valid assumption. Do NOT write code that assumes that that is true.

Nowdays I face corrosion and aging.

Corrosion? Is your body really rusting?

Corrosion…. not doing any C-programming for 12 years do have a certain effect to the small grey cells inside the top of the body…… and now I didn't have any old code to read and find out "how should it be done".

The pot arrangement..... There is one weakness. If the signal to A0, A1 or A2 would be lost. I will deal with that. That construction is used in heavy fly by wire controlled fork lift trucks running all over the world. The biasing resistors where built in in the controlling computer. That never failed when a control (pot) failed. The current arrangement is a protoboard equipped with pots but I had the idea of building a separate box, a remote control. Then any cable might get broken, some day or the other.

What is the impedance for UNO analog inputs? Biasing the construction by some 100k - 1M soldered on the UNO board might tighten the leak. I think it is possible to tamper with INPUT_PULLUP for the analog inputs but what value of the resistor would that give? What do You think?