Object following robot

I’m working on an object following robot using two ultra sonic sensors. The values are compared and the motors are powered accordingly. It follows straight fine but when it comes turning right and left its very buggy. I’m using if else statements as my control but have hit a wall the past two days. My hope is that someone on here can guide me in the right direction. I’m still a beginner. any help would be greatly appreciated.

This is my current if else statement, and the full project is attached.

////////////////////////////STOP when HUMAN is too close to both sensors/////////////////////////
if(durationRight > 0 && durationRight < 3000 && durationRight > 0 && durationLeft < 3000)///////
{rightmotor->run(RELEASE);
leftmotor->run(RELEASE);}

////////////////////////////MOVE FORWARD when HUMAN is equadistant to both sensors////////////////
else if (durationRight > 3000 && durationRight < 10000 && durationLeft > 3000 && durationLeft < 10000)
{rightmotor->run(FORWARD);
leftmotor->run(FORWARD);
delay(1);}

////////////////////////////TURN RIGHT when HUMAN is close to RIGHT sensor////////////////////////
else if(durationLeft - 1000 > durationRight && durationRight > 3000)
{rightmotor->run(RELEASE);
leftmotor->run(FORWARD);
delay(1);}

///////////////////////////TURN LEFT when HUMAN is closer to LEFT sensor//////////////////////////
else if(durationRight - 1000 > durationLeft && durationLeft > 3000)
{rightmotor->run(FORWARD);
leftmotor->run(RELEASE);
delay(1);}

A_Setup.ino (788 Bytes)

B_Loop.ino (2.17 KB)

Capstone_V2.0.ino (1.53 KB)

I should note the else if statements did make it more stable im thinking other components might be needed just not sure were to go from here.

You should describe your problem.

delay(1)

You know, in that millisecond, the Arduino could have executed 16,000 instructions but you chose to put it back in its box, unable to see anything or do anything during that time. When the robot uprising comes, they will be crashing through your door during the first millisecond.

Also, the first if() statement checks that durationRight is greater than zero twice. I don't see how it could have changed during the execution of that line.

Is

durationRight - 1000

correct? If durationRight is declared unsigned and < 1000 it will underflow and became large - possible source of error?