Calling functions within functions

I am calling functions inside other functions. I am receiving input like:

S383
W235
S399
W235
S415
W236
S431
W234
S447
W235
S463
W234

My main problem is I need to use both values for S and W to calculate before moving to next value. So, once call processSteer for 'S' I get 'targetPos' then I process 'W' for processWheel and get 'currentPos'. Now I am trying to get both of those values together into 'calculateError' function to use those values for calculations. Not sure of the process to get currentPos and targetPos together to calculate further.

/*****STEERING*******/
int processSteer (const unsigned int value)
{
  // do something with steer
  Serial.print ("Steer = ");
  Serial.println (value); 
  int steeringInput = constrain(value, 400, 1023); //345 and 795 were min and max of steering wheel pot
  Serial.print("SteeringInput: ");
  Serial.println(steeringInput);
  
   int targetPos = map(steeringInput, 400, 1023, -100, 100);
   Serial.print("targetPos: ");
   Serial.println(targetPos);
   //calculateError(targetPos);
   return targetPos;

} // end of processSteer

int processWheel(const unsigned int value)
{
  Serial.print("Wheel = ");
  Serial.println(value);
  
  int wheelInput = constrain(value, 150, 480); //150 and 480 were min and max of wheel position pot
  Serial.print("WheelInput: ");
  Serial.println(wheelInput);
  
  int currentPos = map(wheelInput, 150, 480, -100,100);
  Serial.print("currentPos: ");
  Serial.println(currentPos);
  //calculateError(currentPos);
  return currentPos;
}

void calculateError(.....)
{
 int error = targetPos - currentPos;
 Serial.print("Error: ");
 Serial.println(error);
 
 setSteer(error);
}

int handlePreviousState ()
{
  switch (state)
  {
  case GOT_B:
    processBrake (currentValue);
    break;
  case GOT_G:
    processGas (currentValue);
    break;
  case GOT_S: //processes first part of Sxxx,xxx
    processSteer (currentValue);
    break;
  case GOT_W:
    processWheel(currentValue);
    break;
  }  // end of switch  

  currentValue = 0; 
}  // end of handlePreviousState

void processIncomingByte (const byte c)
{
  if (isdigit (c))
  {
    currentValue *= 10;
    currentValue += c - '0';
  }  // end of digit

  else 
  {

    // The end of the number signals a state change
    handlePreviousState ();

    // set the new state, if we recognize it
    switch (c)
    {
    case 'B':
      state = GOT_B;
      break;
    case 'G':
      state = GOT_G;
      break;
    case 'S':
      state = GOT_S;
      break;
    case 'W':
      state = GOT_W;
      break;
    default:
      state = NONE;
      break;
    }  // end of switch on incoming byte
  } // end of not digit  

} // end of processIncomingByte

void loop ()
{
  if (Serial.available ())
    processIncomingByte (Serial.read ());

  //if error is stopping motor, try to re-enable the motor
  

}  // end of loop

When you declare a function such as

int foo(int a, int b)
{
return a+b;
}

you get the value out by declaring an int, and using the return value

int fooans;

fooans = foo(4, 5);

So in your case, you need an int to hold the return value of processWheel(), and the other function, then give those ints to the CalculateError() function.

At least I think that is what you are asking.

You could use global variables - not that nice, but definitely working. Then you could declare static variables in handlePreviousState() which will be persistent between calls. They are like global variables but only accessible in the scope where they are declared. Or you could ...

Hmm, somehow I do not see what's bothering you :~

void calculateError(int targetPos, int currentPos)
{
 int error = targetPos - currentPos;
 Serial.print("Error: ");
 Serial.println(error);
 
 setSteer(error);
}

int handlePreviousState ()
{
  static int targetPos= 0;
  static int currentPos= 0;
  switch (state)
  {
  case GOT_B:
    processBrake (currentValue);
    break;
  case GOT_G:
    processGas (currentValue);
    break;
  case GOT_S: //processes first part of Sxxx,xxx
    targetPos= processSteer (currentValue);
    break;
  case GOT_W:
    currentPos= processWheel(currentValue);
    calculateError(targetPos, currentPos);
    break;
  }  // end of switch  

  currentValue = 0; 
}  // end of handlePreviousState

This assumes you always send a W after a S. targetPos and currentPos are only usable in the scope where they are declared.

Yes, a W will be processed after an S. This compiles, I will be able to test it soon. Thank you.

I had did the process of assigning targetPos and currentPos to the processWheel and processSteer like you did but did not assign it static int.

Got it working