Hang when build code programming

i dont know how to do. i already try other code it can be built.
But my code cant be built.

/*
 HC-SR04 Ping distance sensor]
 VCC to arduino 5v GND to arduino GND
 Echo to Arduino pin 13 Trig to Arduino pin 12
 More info at: http://goo.gl/kJ8Gl
 */

#define trigPin 13
#define echoPin 12

#define trigPin2 11
#define echoPin2 10

#define trigPin3 9
#define echoPin3 8

#define distance
#define distance2
#define distance3

void setup() {
  //ultrasonicsensor
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);

  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);

}

void loop() {
  read_ultrasonicsensor();
  if (distance >= 2000 || distance <= 0)
  {
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" mm");
  }
  delay(50);
}

void read_ultrasonicsensor()
{
  //ultrasonic sensor 1
  //long duration, distance;

  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(trigPin, HIGH);
  //delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 2.91;
}

void read_ultrasonicsensor()
{
  //ultrasonic sensor 2
  long duration2, distance2;

  digitalWrite(trigPin2, LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(trigPin2, HIGH);
  //delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2 / 2) / 2.91;
}
 
void read_ultrasonicsensor()
{
  //ultrasonic sensor 3
  long duration3, distance3;

  digitalWrite(trigPin3, LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(trigPin3, HIGH);
  //delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3 = (duration3 / 2) / 2.91;
}

Try using the newPing library, and the newPing example code in the examples.

NewPing.zip (12 KB)

Just make some adjustments to the code to fit 3 sensors, and put them all in the void loop() void.

So what you are saying is "It doesn't work, so find the problem for me?". Yaaa, no. Try giving us more than 21 words to describe your problem.

Or just read "How To Use This Forum" thread.

i have try using new example. It can be build.
But now i try to do read three sonar sensor.
and it cant be build.

First, I dont know about the others here, but this make me uneasy.

#define distance
#define distance2
#define distance3

I know it is valid, but I just don't like it.

Second, you have 3 functions with the exact same name, read_ultrasonicsensor(). Maybe try giving them unique names, even if it is with one additional number like read_ultrasonicsensor**_1**(), read_ultrasonicsensor**_2**() and read_ultrasonicsensor**_3**().

Or better yet, you can use arrays for your Ping pins and have just one function that accepts a number.
This number will correspond to the array's index, thus allowing all three sensors to work.

Let me know what you do.

PS. I just read the other comments and I too suggest you use the NewPing library.

It doesn't even look like you included any libraries. I don't know how it would know what distance meant without a library

Dannyboy001:
It doesn't even look like you included any libraries. I don't know how it would know what distance meant without a library

They get defaulted to 0.
See here.

#define Test

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
delay(1);
Serial.println(int(Test));
}

void loop() {
  // put your main code here, to run repeatedly:
}

HazardsMind:
First, I dont know about the others here, but this make me uneasy.I know it is valid, but I just don't like it.

Second, you have 3 functions with the exact same name, read_ultrasonicsensor(). Maybe try giving them unique names, even if it is with one additional number like read_ultrasonicsensor**_1**(), read_ultrasonicsensor**_2**() and read_ultrasonicsensor**_3**().

Or better yet, you can use arrays for your Ping pins and have just one function that accepts a number.
This number will correspond to the array's index, thus allowing all three sensors to work.

Let me know what you do.

PS. I just read the other comments and I too suggest you use the NewPing library.

Now i am doing programing to read all 3 sonar sensor. Then distance1, distance2, distance3 will get from equation. And that distance will using to differentiate all 3 sonar sensor so that my autonomous robot will adjust to the middle.

I have problem when i compiling sketch. It not showing any error but keep hang in the half when compiling for a long time. I think that will be problem from my programming. Not the software because, I try other example to compile and it can be compile.

Here the new edited. But it didnt work up. I have blank idea how to correct my program

/*
 HC-SR04 Ping distance sensor]
 VCC to arduino 5v GND to arduino GND
 Echo to Arduino pin 13 Trig to Arduino pin 12
 More info at: http://goo.gl/kJ8Gl
 */

#define trigPin 13
#define echoPin 12

#define trigPin2 11
#define echoPin2 10

#define trigPin3 9
#define echoPin3 8

long duration, distance;
long duration2, distance2;
long duration3, distance3;
void setup() {
  //ultrasonicsensor
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);

  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);

}

void loop() {
  read_ultrasonicsensor();
  if (distance >= 2000 || distance <= 0)
  {
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" mm");
  }
  delay(50);
}

void read_ultrasonicsensor()
{
  //ultrasonic sensor 1
  //long duration, distance;

  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(trigPin, HIGH);
  //delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 2.91;
}

void read_ultrasonicsensor2()
{
  //ultrasonic sensor 2
  //long duration2, distance2;

  digitalWrite(trigPin2, LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(trigPin2, HIGH);
  //delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2 / 2) / 2.91;
}
 
void read_ultrasonicsensor3()
{
  //ultrasonic sensor 3
  //long duration3, distance3;

  digitalWrite(trigPin3, LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(trigPin3, HIGH);
  //delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3 = (duration3 / 2) / 2.91;
}

Arrays.

/*
 HC-SR04 Ping distance sensor]
 VCC to arduino 5v GND to arduino GND
 Echo to Arduino pin 13 Trig to Arduino pin 12
 More info at: http://goo.gl/kJ8Gl
 */

const byte TrigPins[3] = {13, 11, 9};
const byte EchoPins[3] = {12, 10, 8};

void setup()
{
  //ultrasonicsensor
  Serial.begin (9600);
  for (byte i = 0; i < 3; i++)
  {
    pinMode(TrigPins[i], OUTPUT);
    pinMode(EchoPins[i], INPUT);
  }
}

void loop()
{
  static long distance[3];
  static byte i = 0;

  distance[i] = read_ultrasonicsensor(i);

  Serial.print(i);
  Serial.print(F(": "));

  if (distance[i] >= 2000 || distance[i] <= 0)
  {
    Serial.println(F("Out of range"));
  }
  else
  {
    Serial.print(distance[i]);
    Serial.println(F(" mm"));
  }
  delay(50);

  i++;
  if (i >= 3)
    i = 0;
}

long read_ultrasonicsensor(byte i)
{
  long distance;

  digitalWrite(TrigPins[i], LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(TrigPins[i], HIGH);
  //delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(TrigPins[i], LOW);
  distance = (pulseIn(EchoPins[i], HIGH) / 2) / 2.91;
}

If you want better performance, use the NewPing library.

HazardsMind:
Arrays.

/*

HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
More info at: http://goo.gl/kJ8Gl
*/

const byte TrigPins[3] = {13, 11, 9};
const byte EchoPins[3] = {12, 10, 8};

void setup()
{
  //ultrasonicsensor
  Serial.begin (9600);
  for (byte i = 0; i < 3; i++)
  {
    pinMode(TrigPins[i], OUTPUT);
    pinMode(EchoPins[i], INPUT);
  }
}

void loop()
{
  static long distance[3];
  static byte i = 0;

distance[i] = read_ultrasonicsensor(i);

Serial.print(i);
  Serial.print(F(": "));

if (distance[i] >= 2000 || distance[i] <= 0)
  {
    Serial.println(F("Out of range"));
  }
  else
  {
    Serial.print(distance[i]);
    Serial.println(F(" mm"));
  }
  delay(50);

i++;
  if (i >= 3)
    i = 0;
}

long read_ultrasonicsensor(byte i)
{
  long distance;

digitalWrite(TrigPins[i], LOW);  // Added this line
  delayMicroseconds(2); // Added this line

digitalWrite(TrigPins[i], HIGH);
  //delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(TrigPins[i], LOW);
  distance = (pulseIn(EchoPins[i], HIGH) / 2) / 2.91;
}




If you want better performance, use the **NewPing** library.

Ok thank hazardmind.. i will try and looking forward for to use NewPing library. also Your edited program show some idea to me