Hello there, im trying to figure out how to check all three operand at the same time given that these is a obstacle avoiding robot that have all 3 ultrasonic sensor at all three direction. and i wanted to include the scenario where all three statement are true which is "(Mdistance < maxFrontDistance)" ,"(Ldistance < maxLeftDistance)" and "(Rdistance < maxRightDistance) " so that i can execute the task such as moveBackward(). i have look it up and someone suggested using the boolean expression of "&&" can anyone help me , bellow is my current coding.
void loop()
{
SpeedControl = analogRead(A0);
MotorSpeed = map(SpeedControl, 0, 1023, 0, 255);
checkFrontDistance();
if (Mdistance < maxFrontDistance) {
checkLeftDistance();
delay(20);
checkRightDistance();
delay(20);
if (Ldistance < Rdistance)
turnRight();
else if (Ldistance> Rdistance) {
turnLeft();
}
}
else{
moveForward();
}
checkLeftDistance();
if (Ldistance < maxLeftDistance) {
checkLeftDistance();
delay(20);
checkRightDistance();
delay(20);
if (Ldistance > Rdistance)
moveForward();
else if (Ldistance < Rdistance) {
turnRight();
}
}
if (Rdistance < maxRightDistance) {
checkRightDistance();
delay(20);
checkLeftDistance();
delay(20);
if (Rdistance >Ldistance)
moveForward();
else if (Rdistance < Ldistance) {
turnLeft();
}
}
}
if (Mdistance < maxFrontDistance && Ldistance < maxLeftDistance && Rdistance < maxRightDistance)
{
//execute this code if all 3 conditions are true
}
Will it contradict with the my existing coding, because i had run a stimulation and it does not execute the coding
Williamsccz:
Will it contradict with the my existing coding, because i had run a stimulation and it does not execute the coding
That depends on where you put the test and the code that it executes if true
Please post the sketch with the new code in it
Williamsccz:
Hello there, im trying to figure out how to check all three operand at the same time given that these is a obstacle avoiding robot that have all 3 ultrasonic sensor at all three direction. and i wanted to include the scenario where all three statement are true which is "(Mdistance < maxFrontDistance)" ,"(Ldistance < maxLeftDistance)" and "(Rdistance < maxRightDistance) " so that i can execute the task such as moveBackward(). i have look it up and someone suggested using the boolean expression of "&&" can anyone help me , bellow is my current coding.
void loop()
{
SpeedControl = analogRead(A0);
MotorSpeed = map(SpeedControl, 0, 1023, 0, 255);
checkFrontDistance();
if (Mdistance >= maxFrontDistance)
{
// No obstacle in front
moveForward();
}
else
{
checkLeftDistance();
checkRightDistance();
// Obstacle in front, check the sides
if (Ldistance < maxLeftDistance && Rdistance < maxRightDistance)
{
// Obstacles in front AND both sides
moveBackward();
}
else
{
// Obstacle in front but not on both sides
// Turn toward the side with the further obstacle.
if (Ldistance < Rdistance)
turnRight();
else
turnLeft();
}
}
}
Works for me thank you so much
johnwasser:
void loop()
{
SpeedControl = analogRead(A0);
MotorSpeed = map(SpeedControl, 0, 1023, 0, 255);
checkFrontDistance();
if (Mdistance >= maxFrontDistance)
{
// No obstacle in front
moveForward();
}
else
{
checkLeftDistance();
checkRightDistance();
// Obstacle in front, check the sides
if (Ldistance < maxLeftDistance && Rdistance < maxRightDistance)
{
// Obstacles in front AND both sides
moveBackward();
}
else
{
// Obstacle in front but not on both sides
// Turn toward the side with the further obstacle.
if (Ldistance < Rdistance)
turnRight();
else
turnLeft();
}
}
}
Thank you to you too good sir!
UKHeliBob:
if (Mdistance < maxFrontDistance && Ldistance < maxLeftDistance && Rdistance < maxRightDistance)
{
//execute this code if all 3 conditions are true
}