Again, as above - I dont use
Again, as above - I dont use arduino’s, but here are a few comments that may help -
As stated above you have to fix your distances inches/centimeters (<15cm is a very short distance)
Your comments state ‘get pin time in microseconds’ but you call the sonar.ping_cm() function, confusing time and distance
The code tells the robot to turn left even if it should go straight ahead in this part>
case ‘s’:
leftward();
delay(1000);
break;
Various comments say things like set to 100% then set a value to 191 (i make that 75%, but mabey your pwm is inverted or something I am missing), others say 37%, but have the same values. I dont want to preach, but it helps everyone (even you!!!) if you can update your comments when you make changes to the code, the comments should either be accurate, or as a minimum show the intent, but accurate is better.
My datasheet for the SR-04 states a minimum time of 65ms between pings, you have a comment about 29ms (perhaps you have a different sensor)
You should use brackets (parentheses) these ( ) in you if statements to show intent, for example:
(rightscanval>leftscanval && rightscanval>centerscanval)
You may know operator precedence rules off by heart, but I dont - but im guessing you mean
((rightscanval>leftscanval) && (rightscanval>centerscanval))
just they 4 brackets make the code easier to read when you ask for help, as your intent is clearly shown. This way if you do have a problem with precedence it can be easily spotted.
This is very similar to my pic code for avoidance, and I would love to help you sort your robot, but we do need some more info.
G