Insert a regular ring to you piece Ultrasonic Sensors // defines pins numbers const int trigPin = 9; const int echoPin = 10; const int buzzer = 11; const int left_trigPin = 6; const int left_echoPin = 7; const int right_trigPin = 2; const int right_echoPin = 3; const int center_trigPin = 4; const int center_echoPin = 5; // defines variables long duration; int distance; int safetyDistance; void setup() { pinMode(left_trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(left_echoPin, INPUT); // Sets the echoPin as an Input pinMode(right_trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(right_echoPin, INPUT); // Sets the echoPin as an Input pinMode(center_trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(center_echoPin, INPUT); // Sets the echoPin as an Input pinMode(buzzer, OUTPUT); Serial.begin(9600); // Starts the serial communication } void loop(){ left(); right(); center(); } void left(){ delay(500);// reading will be taken after ....miliseconds Serial.println("\n"); int duration, distance; digitalWrite (left_trigPin, HIGH); delayMicroseconds (10); digitalWrite (left_trigPin, LOW); duration = pulseIn (left_echoPin, HIGH); distance = (duration/2) / 29.1; //distance= duration*0.034/2; if (distance < 30) { // Change the number for long or short distances. Serial.print("Left Distance"); Serial.print(distance);} safetyDistance = distance; if (safetyDistance <= 5){ digitalWrite(buzzer, HIGH); } else{ digitalWrite(buzzer, LOW); } } void right(){ delay(500);// reading will be taken after ....miliseconds Serial.println("\n"); int duration, distance; digitalWrite (right_trigPin, HIGH); delayMicroseconds (10); digitalWrite (right_trigPin, LOW); duration = pulseIn (right_echoPin, HIGH); distance = (duration/2) / 29.1; if (distance <30) { // Change the number for long or short distances. Serial.print("Right Distance"); Serial.print(distance); safetyDistance = distance; if (safetyDistance <= 5){ digitalWrite(buzzer, HIGH); } else{ digitalWrite(buzzer, LOW); } } } void center(){ delay(500);// reading will be taken after ....miliseconds Serial.println("\n"); int duration, distance; digitalWrite (center_trigPin, HIGH); delayMicroseconds (10); digitalWrite (center_trigPin, LOW); duration = pulseIn (center_echoPin, HIGH); distance = (duration/2) / 29.1; if (distance <30) { // Change the number for long or short distances. Serial.print("Center Distance"); Serial.print(distance); } safetyDistance = distance; if (safetyDistance <= 5){ digitalWrite(buzzer, HIGH); } else{ digitalWrite(buzzer, LOW); } }
Insert a regular ring to you piece Ultrasonic Sensors
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int left_trigPin = 6;
const int left_echoPin = 7;
const int right_trigPin = 2;
const int right_echoPin = 3;
const int center_trigPin = 4;
const int center_echoPin = 5;
// defines variables
long duration;
int distance;
int safetyDistance;
void setup() {
pinMode(left_trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(left_echoPin, INPUT); // Sets the echoPin as an Input
pinMode(right_trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(right_echoPin, INPUT); // Sets the echoPin as an Input
pinMode(center_trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(center_echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop(){
left();
right();
center();
}
void left(){
delay(500);// reading will be taken after ....miliseconds
Serial.println("\n");
int duration, distance;
digitalWrite (left_trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (left_trigPin, LOW);
duration = pulseIn (left_echoPin, HIGH);
distance = (duration/2) / 29.1;
//distance= duration*0.034/2;
if (distance < 30) { // Change the number for long or short distances.
Serial.print("Left Distance");
Serial.print(distance);}
safetyDistance = distance;
if (safetyDistance <= 5){
digitalWrite(buzzer, HIGH);
}
else{
digitalWrite(buzzer, LOW);
}
}
void right(){
delay(500);// reading will be taken after ....miliseconds
Serial.println("\n");
int duration, distance;
digitalWrite (right_trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (right_trigPin, LOW);
duration = pulseIn (right_echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <30) { // Change the number for long or short distances.
Serial.print("Right Distance");
Serial.print(distance);
safetyDistance = distance;
if (safetyDistance <= 5){
digitalWrite(buzzer, HIGH);
}
else{
digitalWrite(buzzer, LOW);
}
}
}
void center(){
delay(500);// reading will be taken after ....miliseconds
Serial.println("\n");
int duration, distance;
digitalWrite (center_trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (center_trigPin, LOW);
duration = pulseIn (center_echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <30) { // Change the number for long or short distances.
Serial.print("Center Distance");
Serial.print(distance);
}
safetyDistance = distance;
if (safetyDistance <= 5){
digitalWrite(buzzer, HIGH);
}
else{
digitalWrite(buzzer, LOW);
}
}

Step by step
Solved in 6 steps









