I am debating weather to just stop or actually detach the servos. It depends if I can get the robot to drive towards the target and stop. In some of the tests, it will stop and continue checking the distance but some stray reading will cause the robot to turn then it can't find its way back to the target. In the code below, I removed the debug statements to clean it up for posting.
I am including the functions and the loop that should be calling the functions at the end of this. I am also including my ping() function just in case, but the debug shows it is working as designed. The only functions that are working (according to my debug before and after every function and function call) are
if ((chkDistanceFwd>dangerThresh) && (rightDistance=leftDistance)) //if target distance is greater than programmed default, go straight
{
forward();
}
and
else if ((chkDistanceFwd<=dangerThresh) || (rightDistance<=dangerThresh) || (leftDistance<=dangerThresh)) //if target is <= minimum programmed distance. STOP
{
bothWheelStop();
}
If I understand && (both must be true) and || (either can be true) correctly, the rest of the driving function calls from the 'if' statements should work but they are not.
void ping()
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
chkDistanceFwd = microsecondsToCentimeters(duration); //ping stright ahead
delay(100); //wait this many milliseconds WAS 500
panMotor.write(70); //turn ping sensor right, 0 for full right
delay(200); //wait this many milliseconds WAS 500
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
rightDistance = microsecondsToCentimeters(duration);
delay(100); //wait this many milliseconds WAS 500
panMotor.write(110); //turn ping sensor left, 180 for full right
delay(300); //wait this many milliseconds WAS 500
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
leftDistance = microsecondsToCentimeters(duration);
panMotor.write(pos); //return to center, "90 by default"
delay(200); //wait this many milliseconds
}
void forward()
{
rightMotor.writeMicroseconds(RForward); //move forward
leftMotor.writeMicroseconds(LForward); //move forward
}
void bothWheelStop()
{
rightMotor.writeMicroseconds(1390);
leftMotor.writeMicroseconds(1400);
}
void reverse() //if they are equally obstructed
{
rightMotor.writeMicroseconds(RBackwardSlow); //rotate right drive servo backward
leftMotor.writeMicroseconds(LBackwardSlow); //rotate left drive servo forward
delay(500); //run motors this many milliseconds
}
void turnLeft() //if left is less obstructed
{
rightMotor.writeMicroseconds(RForwardSlow); //rotate right drive servo forward "skid steer"
leftMotor.writeMicroseconds(LBackwardSlow); //rotate left drive servo bacward "skid steer"
delay(200); //run motors this many milliseconds
}
void turnRight() //if right is less obstructed
{
rightMotor.writeMicroseconds(RBackwardSlow); //rotate right drive servo backward "skid steer"
leftMotor.writeMicroseconds(LForwardSlow); //rotate left drive servo forward "skid steer"
delay(200); //run motors this many milliseconds
}
void loop()
{
ping();
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches); //Prints to Serial Monitor
Serial.print("in, "); //Prints to Serial Monitor
Serial.print(cm); //Prints to Serial Monitor
Serial.print("cm"); //Prints to Serial Monitor
Serial.println(); //Prints to Serial Monitor
Serial.print(chkDistanceFwd);
Serial.print(" chkDistanceFwd "); //displays value of chkDistanceFwd to validate operation
Serial.print(dangerThresh);
Serial.print(" dangerThresh "); //displays value of dangerThresh to validate operation
Serial.print (rightDistance);
Serial.print(" rightDistance ");
Serial.print (leftDistance);
Serial.print(" leftDistance ");
if ((chkDistanceFwd>dangerThresh) && (rightDistance=leftDistance)) //if target distance is greater than programmed default, go straight
{
forward();
}
else if ((chkDistanceFwd>dangerThresh) && (rightDistance<leftDistance)) //if target distance is greater than programmed default AND if target is closer on the RIGHT side...turn RIGHT to face target
{
turnRight();
}
else if ((chkDistanceFwd>dangerThresh) && (leftDistance<rightDistance)) //if target distance is greater than programmed default AND if target is closer on the LEFT side...turn LEFT to face target
{
turnLeft();
}
else if ((chkDistanceFwd<=dangerThresh) || (rightDistance<=dangerThresh) || (leftDistance<=dangerThresh)) //if target is <= minimum programmed distance. STOP
{
bothWheelStop();
}
else if ((chkDistanceFwd<=dangerReverseThresh) || (rightDistance<=dangerReverseThresh) || (leftDistance<=dangerReverseThresh)) //if target is <= programmed danger distance, BACKWARDS
{
reverse();
}
}