Problem using DC Motors in conjunction with sonar sensors

Hi, I’m making a rover to navigate an open space and I’m using HRC404 sensors to detect walls and obstacles. Something is going wrong when I’m calling my motor function as it seems to mess up the sensor readings, which work up to that point. After the motor function is called, the sensor readings will constantly stay 0 and not update at all.

void left_motor_forward()
{
  digitalWrite(LEFT_MOTORB,HIGH);
  digitalWrite(LEFT_MOTORA,LOW);
}
void right_motor_forward()
{
  digitalWrite(RIGHT_MOTORB,LOW);
  digitalWrite(RIGHT_MOTORA,HIGH);         
}
void both_motors_forward()
{
  left_motor_forward();
  right_motor_forward();
}

void navigation()
{

DistanceL=sensor1();
DistanceR=sensor2();
DistanceF=sensor3();
// Everything is Clear
    if (DistanceF >= 4 && DistanceL >= 4 && DistanceR >= 4)
    {
        x=4;
        Serial.println(x);
        //k=1024;
        both_motors_forward();
        delay(20);
        Serial.println(x);
        Serial.print("k:");
        Serial.println(k);
        
    }
}

void loop()
{   
   if(digitalRead(11)==LOW)
   {
    delay(500);
    i=1;
      while (i==1)
      {
      x=1;
      Serial.print("x:");
      Serial.println(x);
      navigation();
      x=3;
      Serial.print("x:");
      Serial.println(x);
        if(digitalRead(11)==LOW)
           {         
            x=0;
            i=0;
           }
         Serial.print("i:");
         Serial.println(i);
      }
    Serial.print("k:");
    Serial.println(k);
    delay(10000);
    }
    return;
  }

I tried to select the relevant parts and attached the entire sketch. I’m using an IR sensor for the change on pin 11. The excessive print statements are to confirm exactly where the code is going wrong but I’m still not sure what the problem is and how to correct it. Any help would be appreciated.

rovertotal.ino (7.19 KB)

First impression based on the description is that the motors disrupt the power supply for the sonars. Have you monitored the sonar supply while activating the motors to see? Can you post a schematic of your project? Include all components and power supplies.

You did not post all of your sketch. Where are the functions that read the sonars?

I attached the rest of the code in the opening post. However I'll put it in this post as well.

void sensorsetup() 
{
  Serial.begin(9600);
}

uint8_t sensor1() 
{
  delay(50);                    
  DistanceL=sonar1.convert_in(sonar1.ping_median(5));
  Serial.print("Forward: ");
  Serial.print(DistanceL); 
  Serial.print("in");
  Serial.print('\t');
    return DistanceL;
}

uint8_t sensor2() 
{
  delay(50);
  DistanceR=sonar2.convert_in(sonar2.ping_median(5));
  Serial.print("Left: ");
  Serial.print(DistanceR); 
  Serial.print("in");
  Serial.print('\t');
 return DistanceR;
}

uint8_t sensor3()
{
  delay(50);
  DistanceF=sonar3.convert_in(sonar3.ping_median(5));
  Serial.print("Right:");
  Serial.print(DistanceF); 
  Serial.println("in");
    return DistanceF;
}

Your hunch was correct. It seems the power to the sensors was getting disrupted and I was able to fix it. Thank you, this was frustrating me.

Why do you have 3 nearly identical functions, for measuring distance? Learn about passing arguments to functions.

Why does each function return a global variable's value? Why do you then overwrite the global variable with the same value that it had?