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