how to stop PID calculations? / tell the program the PID calculation completed

hello, im trying to make a line following robot, the robot is quite long so the robot meets the junction, i want it to move an extra 11.5cm, and then turn 90degrees, so that it can turn into the right lane better, im using arduino PID Library by Brett Beauregar http://playground.arduino.cc//Code/PIDLibrary and after referring to examples available, i managed to write the two subfunctions offsetFront(); and offsetRotateRIght(); as below…

//PID Variables
#define SETPOINT_FRONT 1660    //need 166 encoder counts to move forward 11.5cm
#define SETPOINT_ROTATE 2190    //need 219 encoder counts to rotate the robot 90degrees
#define SAMPLE_TIME 1 //Time interval in ms for PID sampling, i set it low only this will make it get to final position
long encoder1 = (abs(newLeft)+abs(newRight))/2;  //get the encoder counts

    positionInput1 = 0;    //Initialize the variables we're linked to, this is for making the robot move forward 11.5cm
    positionPID1.SetMode(AUTOMATIC);    //Turn pid on, Automatic mode
    positionPID1.SetOutputLimits(100,120);    //Clamp PWM values between 100 & 120
    positionPID1.SetSampleTime(SAMPLE_TIME);    //Set PID Sampling Time

    positionInput4 = 0;    //Initialize PID4 variables we're linked to, this is to make robot rotate 90degrees
    positionPID4.SetMode(AUTOMATIC);    //Turn pid on, Automatic mode
    positionPID4.SetOutputLimits(100,120);    //Clamp PWM values between 100 & 120
    positionPID4.SetSampleTime(SAMPLE_TIME);    //Set PID Sampling Time   


// this subfunction will make the robot go forward 11.5cm
int offsetFront(){ 
    positionSetPoint1 = SETPOINT_FRONT;    //Distance to be travelled
    positionInput1 = map(encoder1, 0,360,0,3600); 
    positionPID1.Compute();    //Decides whether to launch PID algorithm

    if(abs (positionInput1 - positionSetPoint1)<50){    //Sets the PID parameters
        positionPID1.SetTunings(consKp1, consKi1, consKd1);}
    else{
        positionPID1.SetTunings(aggKp1, aggKi1, aggKd1);}
    if(positionInput1 < positionSetPoint1){
        goFront(positionOutput1);} 
    else if (positionInput1 > positionSetPoint1){
        goBack(positionOutput1);}
    else{
        tempStop();}}

//this is to make the robot rotate right 90degrees
int offsetRotateRight(){    
    positionSetPoint4 = SETPOINT_ROTATE;
    positionInput4 = map(encoder1, -360,360,-3600,3600);
    positionPID4.Compute();  //Decides whether to launch PID algorithm

    if (abs( positionInput4 - positionSetPoint4 ) < 50 ){    //Sets the PID parameters
        positionPID4.SetTunings(consKp4, consKi4, consKd4);}
    else{
        positionPID4.SetTunings(aggKp4, aggKi4, aggKd4);}
    if(positionInput4 < positionSetPoint4){
        goRight(positionOutput4);}
    else if (positionInput4 > positionSetPoint4){
        goLeft(positionOutput4);}
    else{
        tempStop();}}

when i write program with only subfunction call like this, the robot moves forward 11.5cm and reach steady values

void loop(){
    timer.run();    //timer will get encoder counts at intervals
    offsetFront();}

when i write like this, the robot rotates 90degrees, good

void loop(){
    timer.run();    //timer will get encoder counts at intervals
offsetRotateRight();}

but i cant seem to implement this into my line following robot

void loop(){
    timer.run();
    int a = digitalRead(IR1);  //when IR sensors in line, gives a High to these variables
    int b = digitalRead(IR2);
    int c = digitalRead(IR3);
    int d = digitalRead(IR4);
  
    if (a==HIGH && b==HIGH && c==HIGH && d==HIGH){  //meets junction, all IR in black line, all HIGH
    offsetFront();   //so i want it to move forward 11.5cm
    offsetRotateRight();  //and rotate right 90degrees
    }

the two motors will rotate randomly and its not what i want
i implement different PID to offsetFront() and offsetRotateRight, how could this be happen?
seems like the way arduino reads the void loop() is causing this problem…
is there a way to write program, and asks arduino to DO only offsetFront(), and only after finish doing it, comes out, and continues to offsetRotateRight? being figuring this for weeks…but in vail =(

any kind help would be very appreciated…

what i think is arduino reads offsetFront();
and before it finishes doing stuffs inside offsetFront(), it continues to read offsetRotateRight(),
then before it finishes stuffs inside offsetRotateRight() it goes back to read offsetFront() again,

i think this caused the random movement of the two motors because without finishing offsetFront(); then continues to do offsetRotateRight(), causes instability in the PID in offsetFront() making it unable to reach its steady state values ...

i must keep arduino to finish doing offsetFront() before reading other subfunctions, or else i cant implement the PID library at all...

tried many ways trying to make the arduino to finish doing the stuffs in offsetFront() first before it continues to do offsetRotateRight(), like wrapping offsetFront() in for loops, or even changing void offsetFront() to int void offsetFront() to return 0 after it finishes

and i still failed... if you need to see the whole code i can post here...

j6433:
if you need to see the whole code i can post here...

Yes please. Trying to debug snippets is usually futile.

PeterH:
Yes please. Trying to debug snippets is usually futile.

okay…here is it

do take a look at the end of void loop (), i wasnt getting multiple PID to work, even if i put only 1 PID (that is offsetFront() ) into the line following, i cant get it to work when it meets the junction, the wheels turning randomly cant reach the final values, i need some kind of turning off the PID after it knows that it has reach the values i want, or even, keep the arduino to finish doing 1 PID first, turning it off, then only do the next thing…

    if (a==HIGH && b==HIGH && c==HIGH && d==HIGH){  //Junction
    offsetFront();
    
    //offsetBack();
    //offsetRotateRight();
    //offsetRotateLeft();
    }

simple timer library from GitHub - infomaniac50/SimpleTimer: This is (yet another) simple library to launch timed actions.
encoder library from http://www.pjrc.com/teensy/td_libs_Encoder.html
brett’s pid library from GitHub - br3ttb/Arduino-PID-Library
i havent given them credits as i’m not finished with my project yet…

Fork_Lift_Program.ino (14 KB)