What is wrong in this code?

// defines pins numbers
const int trigPin1 = 9;
const int echoPin1 = 10;
const int trigPin2 = 9;
const int echoPin2 = 10;
const int trigPin3 = 9;
const int echoPin3 = 10;
const int buzzer = 11;
const int motor = 13;

// defines variables

void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(motor, OUTPUT);

}

void loop() {
int duration, distance;
digitalWrite (trigPin1, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin1, LOW);
duration = pulseIn (echoPin1, HIGH);
distance = (duration/2) / 29.1;

Serial.print(distance);
Serial.print("cm");
Serial.println();

if (distance < 30) { // Change the number for long or short distances.
digitalWrite (buzzer, HIGH);
} else {
digitalWrite (buzzer, LOW);
}

int duration2, distance2;
digitalWrite (trigPin2, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin2, LOW);
duration = pulseIn (echoPin2, HIGH);
distance2 = (duration/2) / 29.1;

Serial.print(distance2);
Serial.print("cm");
Serial.println();

if (distance2 < 20) { // Change the number for long or short distances.
digitalWrite (buzzer, HIGH);
}
else {
digitalWrite (buzzer, LOW);
}

int duration3, distance3;
digitalWrite (trigPin3, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin3, LOW);
duration = pulseIn (echoPin3, HIGH);
distance3 = (duration/2) / 29.1;

Serial.print(distance3);
Serial.print("cm");
Serial.println();

if (distance3 < 10) { // Change the number for long or short distances.
digitalWrite (buzzer, HIGH);
}
else {
digitalWrite (buzzer, LOW);
}

}
// Code ends here.

// Code ends here.

That's all!

The following two functions are everything based on which you can write yourself tons of program lines.

void setup()
{

}

void loop()
{

}

Write a basic (simple and elementary) program; execute it and check that it produces the expected result. If not, correct the program and then add hardware and codes of the next function. This procedure is known as SSS (Small Start Strategy) Strategy.

Example of a basic program: Make ON, wait 1-sec, and then make OFF the built-in LED (L) of the Arduino UNO.

You proceed following SSS strategy; one day you will become a good programmer. A programmer knows the way of writing the steps for the solution of his problem.

Then comes what we call coding, which could be in any language. Learning and practicing few keywords of the Arduino IDE and the simple C are good enough to be a good programmer (my opinion!).

const int trigPin1 = 9;
const int echoPin1 = 10;
const int trigPin2 = 9;
const int echoPin2 = 10;
const int trigPin3 = 9;
const int echoPin3 = 10;

I stopped looking after this

Are you really using each of the 2 pins (9 and 10) for 3 different purposes ?

Does the code compile ?

Thats is one of the simplest ways of finding problems with the code.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

You need to explain what your code is supposed to do and what it is doing.

Can you please tell us your electronics, programming, Arduino, hardware experience?

Thanks.. Tom... :slight_smile:

What makes you think that something is wrong with the code?

What is wrong in this code?

The biggest problem is that there is too much of it. Write a function to measure the distance, using one sensor. Call the function three times, once for each sensor. Do NOT copy/paste the code to make 3 copies of it.