I didn't get far into reading your program before I became confused as to which distance related to which direction, left, right or front so I am afraid that I stopped trying to understand the details at that point.
Can I suggest that you rename your distance variables to reflect the direction that they relate to in order to make the logic clearer. It would also help to have the motors named as left and right rather than A and B and for their directions to be named forward and reverse rather than CW and CCW.
Also, whilst it is nothing to do with the logic of the program, I noticed that you have 3 separate functions to read distances when it could be done with a single function to which you passed a parameter indicating the sensor to be read. You are also passing max and min ranges to the function when there is no need as they are global variables.