user49010: I have a function for manually jogging a stepper motor using the AccelStepper library. At the beginning of the function I would like the motor in question to be selected until told to stop.
Here is what I imagine it would look like:
AccelStepper X_Axis(AccelStepper::DRIVER, 3, 2);
AccelStepper Y_Axis(AccelStepper::DRIVER, 5, 4);
AccelStepper Z_Axis(AccelStepper::DRIVER, 7, 6);

void ManualJog(int Dir, int Spd, int axis)
{
  int i;
  if (Dir == 0){i = 1;}else{i =- 1;}
  setSpeed(i * Spd);
  switch(axis)
    {
      case 1:
      axis = X_Axis;
      break;

      case 2:
      axis = Y_Axis;

      case 3:
      axis = Z_Axis
    }
  while (!Serial.available()){

      axis.runSpeed();
    }
  }

The axis variable should map to whichever axis is called and so control the desired stepper.
How can I achieve this?
Note: I submitted this to StackOverflow with no response. Any help would be appreciated

user4143: To do it in the form you have, you would want to use a pointer.
void ManualJog(int Dir, int Spd, int axis)
{
  int i;
  AccellStepper *thisAxis = NULL;

  if (Dir == 0) {
    i = 1;
  } else {
    i =- 1;
  }
  setSpeed(i * Spd);
  switch(axis)
  {
    case 1:
      thisAxis = &X_Axis;
      break;

    case 2:
      thisAxis = &Y_Axis;
      break;

    case 3:
      thisAxis = &Z_Axis
      break
  }

  if (thisAxis != NULL) {
    while (!Serial.available()) {
      thisAxis->runSpeed();
    }
  }
}

Briefly: the & before the name of a variable in assignment means "Give me the address of this variable".  A * before a variable when declaring that variable means "Make this variable hold the address of another variable of this type".  You then have the address variable hold the address of your chosen axel, and then "dereference" that with the "poiner dereference operator" -> (instead of using .).
A check is added to see if thisAxis has actually been assigned anything or not, although you could use the default: case in your switch to abort on an invalid axis number instead.
Depending on how the rest of your program works, though, it may be possible to not use the "axis" integer at all, but pass a reference to the correct stepper to the function:
void ManualJog(int Dir, int Spd, AccelStepper &axis) {
  int i;
  if (Dir == 0) {
    i = 1;
  } else {
    i =- 1;
  }
  setSpeed(i * Spd);
  while (!Serial.available()) {
    axis.runSpeed();
  }
}

In this example the & before the axis variable in the function prototype means "Pass me the address of the variable", and a new variable is created located in the same memory space. This is the pass by reference operator in C++.

49010: <p>I have a function for manually jogging a stepper motor using the AccelStepper library. At the beginning of the function I would like the motor in question to be selected until told to stop.</p>

<p>Here is what I imagine it would look like:</p>

<pre><code>AccelStepper X_Axis(AccelStepper::DRIVER, 3, 2);
AccelStepper Y_Axis(AccelStepper::DRIVER, 5, 4);
AccelStepper Z_Axis(AccelStepper::DRIVER, 7, 6);

void ManualJog(int Dir, int Spd, int axis)
{
  int i;
  if (Dir == 0){i = 1;}else{i =- 1;}
  setSpeed(i * Spd);
  switch(axis)
    {
      case 1:
      axis = X_Axis;
      break;

      case 2:
      axis = Y_Axis;

      case 3:
      axis = Z_Axis
    }
  while (!Serial.available()){

      axis.runSpeed();
    }
  }
</code></pre>

<p>The axis variable should map to whichever axis is called and so control the desired stepper.</p>

<p>How can I achieve this?</p>

<p>Note: I submitted this to StackOverflow with no response. Any help would be appreciated</p>
 4143: <p>To do it in the form you have, you would want to use a <em>pointer</em>.</p>

<pre><code>void ManualJog(int Dir, int Spd, int axis)
{
  int i;
  AccellStepper *thisAxis = NULL;

  if (Dir == 0) {
    i = 1;
  } else {
    i =- 1;
  }
  setSpeed(i * Spd);
  switch(axis)
  {
    case 1:
      thisAxis = &amp;X_Axis;
      break;

    case 2:
      thisAxis = &amp;Y_Axis;
      break;

    case 3:
      thisAxis = &amp;Z_Axis
      break
  }

  if (thisAxis != NULL) {
    while (!Serial.available()) {
      thisAxis-&gt;runSpeed();
    }
  }
}
</code></pre>

<p>Briefly: the <code>&amp;</code> before the name of a variable in assignment means "Give me the address of this variable".  A <code>*</code> before a variable when declaring that variable means "Make this variable hold the address of another variable of this type".  You then have the address variable hold the address of your chosen axel, and then "dereference" that with the "poiner dereference operator" <code>-&gt;</code> (instead of using <code>.</code>).</p>

<p>A check is added to see if <code>thisAxis</code> has actually been assigned anything or not, although you could use the <code>default:</code> case in your switch to abort on an invalid axis number instead.</p>

<p>Depending on how the rest of your program works, though, it may be possible to not use the "axis" integer at all, but pass a reference to the correct stepper to the function:</p>

<pre><code>void ManualJog(int Dir, int Spd, AccelStepper &amp;axis) {
  int i;
  if (Dir == 0) {
    i = 1;
  } else {
    i =- 1;
  }
  setSpeed(i * Spd);
  while (!Serial.available()) {
    axis.runSpeed();
  }
}
</code></pre>

<p>In this example the <code>&amp;</code> before the <code>axis</code> variable in the function prototype means "Pass me the address of the variable", and a new variable is created located in the same memory space. This is the <em>pass by reference</em> operator in C++.</p>
