Problems with HC-sr04

I want to built a Line tracking robot which will stop when it is 5cm from a wall and then go back.
The HC-sr04 code works just fine alone but when use it along with others, all the numbers turn wrong???

The HC-sr04:

void setup() {
  Serial.begin(9600);
  pinMode(0, INPUT);
  pinMode(1, OUTPUT);
}

void loop() {
  digitalWrite(1, LOW);
  delayMicroseconds(2);
  digitalWrite(1, HIGH);
  delayMicroseconds(10);
  int d = pulseIn(0, HIGH);
  d = d/58;
  Serial.println(d);
}

The robot code:

#include <Servo.h>
Servo myservo;
int pos = 0;
int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
void setup()
{
  Serial.begin(9600);
  pinMode(0, INPUT);      //HC-sr04 Echo
  pinMode(1, OUTPUT);   //HC-sr04 Trig
  pinMode(3, OUTPUT);   //2 LED
  pinMode(M1, OUTPUT); //Micro DC Geared Motor with Back Shaft
  pinMode(M2, OUTPUT); //Micro DC Geared Motor with Back Shaft
  myservo.attach(2);      //Hitec HS311 Standard Servo
  myservo.write(150);
}
void loop()
{
  digitalWrite(1, LOW);
  delayMicroseconds(2);
  digitalWrite(1, HIGH);
  delayMicroseconds(10);
  int d = pulseIn(0, HIGH);
  d = d/58;
  Serial.println(d);
    if (d > 2 && (digitalRead(10) == 0 || digitalRead(11) == 0))
    {
      digitalWrite(M1, HIGH);
      digitalWrite(M2, HIGH);
      analogWrite(E1, 100);
      analogWrite(E2, 100);
    }
    if (d > 2 && digitalRead(12) == 0)
    {
      digitalWrite(M1, HIGH);
      digitalWrite(M2, HIGH);
      analogWrite(E1, 100);
      analogWrite(E2, 80);
    }
    if (d > 2 && digitalRead(13) == 0)
    {
      digitalWrite(M1, HIGH);
      digitalWrite(M2, HIGH);
      analogWrite(E1, 100);
      analogWrite(E2, 60);
    }
    if (d > 2 && digitalRead(9) == 0)
    {
      digitalWrite(M1, HIGH);
      digitalWrite(M2, HIGH);
      analogWrite(E1, 80);
      analogWrite(E2, 100);
    }
    if (d > 2 && digitalRead(8) == 0)
    {
      digitalWrite(M1, HIGH);
      digitalWrite(M2, HIGH);
      analogWrite(E1, 60);
      analogWrite(E2, 100);
    }
    if (d > 1 && d <= 2  )
    {
      analogWrite(E1, 0);
      analogWrite(E2, 0);
      digitalWrite(3, HIGH);
      for (pos = 150; pos > 80; pos -= 10) myservo.write(pos);
      delay(2000);
      digitalWrite(3, LOW);
      digitalWrite(M1, LOW);
      digitalWrite(M2, LOW);
      analogWrite(E1, 110);
      analogWrite(E2, 100);
      delay(3000);
      analogWrite(E1, 0);
      analogWrite(E2, 0);
      delay(999999);
    }
}

I use Arduino Uno and all the components from DFRobot except the HC-sr04:
_ The Screw Shield V2 is installed directly onto the Arduino Uno and a 2A Motor Shield on top.
_ 6 Line Tracking Sensor are connected to digital pins 8 - 13 on the screw shield.
_ The Screw Shield VCC GND are connected to the 5V GND pin on the 2A Motor Shield.
_ 2 Micro DC Geared Motor with Back Shaft are connected to the 2A Motor Shield exactly like the description in DFRobot Manual.
_ A Hitec HS311 Standard Servo is connected to pin 2 on the screw shield.
_ On pin 3 of the screw shield are just 2 LED with 2 small resistors to protect the LEDs.
_ About the HC-sr04: Trig - pin 1, Echo - pin 0, VCC - 5V.
_ I use Wild Scorpion 7.4V 2200mAh 35C: the red wire is connected to VIN on the 2A Motor Shield and the outside black wire is connected to GND.

You can find all the information about the DFRobot components here:
_ Screw Shield V2 For Arduino: Gravity: Screw Shield V2 for Arduino - DFRobot
_ 2A Motor Shield For Arduino: http://www.dfrobot.com/index.php?route=product/product&keyword=2a%20m&product_id=69[](http://"http://\"http://\\"http://\\"http://\\"http://\"http://\"http://www.dfrobot.com/index.php?route=product/product&keyword=2a""")
_ Line Tracking Sensor for Arduino: Gravity: Digital Line Tracking (Following) Sensor For Arduino - DFRobot
_ Micro DC Geared Motor with Back Shaft: TT Geared Motor with Back Shaft (180rpm 6v L Shape) - DFRobot
_ Hitec HS311 Standard Servo: Hitec HS311 Standard Servo - DFRobot

I'm sorry for all the trouble. This is the first time I've ever make something like this so I don't even have a slightest clue about writing a wiring diagram.
Thank you for your help.

but when use it along with others,

What are "others"?
If you want informed help, take a few moments to describe your setup as completely as possible, and include a wiring diagram.

tuanlam169:
I want to built a Line tracking robot which will stop when it is 5cm from a wall and then go back.
The HC-sr04 code works just fine alone but when use it along with others, all the numbers turn wrong???

OK as i can see from your code you are using two servo motors, two dc motors and the hc-sr04. Well i suppose its something wrong with the wiring.. A photo would help..

What is the power source of the robot? Is it separate from the Arduino's power source? If not it should be..

Please check your wiring... :slight_smile: :slight_smile:

Are you using a Uno? If so, you should not use pins 0 and 1 for the rangefinder as they are the hardware serial pins (Serial.print).