arduino making noises

i'm trying to connect 2 motors and a ultrasonic module to my arduino uno.
when i supply power the arduino makes a beeping noise and nothing works.
if i remove the ultra sonic and 1 motor the second motor will work.

i'm powering the porject with a 9v battery connected to a power extension module, and i tried 4x1.5v batteries.

my motors are 3v.

anyone knows what is that noise and how can i fix it or how should i power it?

here is schematic and my code:

#include "SR04.h"
#define TRIG_PIN 13
#define ECHO_PIN 12
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);

#define ENABLE1 5
#define DIR1A 3
#define DIR1B 4

#define ENABLE2 11
#define DIR2A 9
#define DIR2B 10

int i;
 
void setup() {
  //---set pin direction
  pinMode(ENABLE1,OUTPUT);
  pinMode(DIR1A,OUTPUT);
  pinMode(DIR1B,OUTPUT);
  pinMode(ENABLE2,OUTPUT);
  pinMode(DIR2A,OUTPUT);
  pinMode(DIR2B,OUTPUT);\
  Serial.begin(9600);

  analogWrite(ENABLE1,70); // enable on
  analogWrite(ENABLE2,70); // enable on
 
  digitalWrite(DIR1A,HIGH); //one way
  digitalWrite(DIR1B,LOW);
  digitalWrite(DIR2A,HIGH); //one way
  digitalWrite(DIR2B,LOW);
}

void loop() {
    if (sr04.Distance() <= 8) {
      digitalWrite(DIR2A,LOW);  //reverse
      digitalWrite(DIR2B,HIGH);
      
      delay(1000);

      digitalWrite(DIR2A,HIGH); //one way
      digitalWrite(DIR2B,LOW);
    }
}

You should probably also have delay(1000) after the second pair of digitalWrite()s in this

void loop() {
    if (sr04.Distance() <= 8) {
      digitalWrite(DIR2A,LOW);  //reverse
      digitalWrite(DIR2B,HIGH);
      
      delay(1000);

      digitalWrite(DIR2A,HIGH); //one way
      digitalWrite(DIR2B,LOW);
    }
}

otherwise it will go instantly from the second pair back to the first pair.

...R

You should not be powering motors from the Arduino 5V rail, its a logic supply rail and
doesn't handle the current involved or tolerate the spikes and dropouts. You may be
endangering the computer USB port too.

Separate power supply for motors.

Robin2:
You should probably also have delay(1000) after the second pair of digitalWrite()s in this

void loop() {

if (sr04.Distance() <= 8) {
      digitalWrite(DIR2A,LOW);  //reverse
      digitalWrite(DIR2B,HIGH);
     
      delay(1000);

digitalWrite(DIR2A,HIGH); //one way
      digitalWrite(DIR2B,LOW);
    }
}



otherwise it will go instantly from the second pair back to the first pair.

...R

the code should rotate the "car" and then continue moving forward, that why after the first delay the car should be facing no obstacle so there is no need in more delay

i forgot to add this in the schem but as i wrote i'm using a Power Supply Module. with this module i'm powering the arduino as well, should i move the arduino power to the second rail? (it's not connected to the computer)

Robin2:
You should probably also have delay(1000) after the second pair of digitalWrite()s in this

void loop() {

if (sr04.Distance() <= 8) {
      digitalWrite(DIR2A,LOW);  //reverse
      digitalWrite(DIR2B,HIGH);
     
      delay(1000);

digitalWrite(DIR2A,HIGH); //one way
      digitalWrite(DIR2B,LOW);
    }
}



otherwise it will go instantly from the second pair back to the first pair.

...R

What you have to ensure is that the Arduino 5V rail is not contaminated by being used for the motor.
Otherwise it is likely to crash the Arduino on every dropout, or even cause damage.

hidonut:
the code should rotate the "car" and then continue moving forward, that why after the first delay the car should be facing no obstacle so there is no need in more delay

Then I think your code is not written to do what you want.

Take a pencil and paper and pretend your brain is the Arduino and work through the code line by line writing down the results for each line. Do that for 4 or 5 iterations of loop().

...R

hidonut:
here is schematic

I'm not one of those who are against Fritzings on principle, sometime's they're ok. But that one made my eyes bleed.

neiklot:
I'm not one of those who are against Fritzings on principle, sometime's they're ok. But that one made my eyes bleed.

A candidate for the Wall of Shame ?

...R