//define the sensor pins
#define echoPin1 11
#define trigPin1 12
#define echoPin2 4
#define trigPin2 3
#define echoPin3 10
#define trigPin3 7
#define trigDistance 15
//define the LED pins and buzzer
#define redLED 8
#define greenLED 9
#define blueLED 5
#define buzzer 2
// defines variables
long duration1, duration2, duration3;
int distance1, distance2, distance3;
unsigned long prevMillis; //Timing variable
void readSensors() {
// Clears the trigPin
digitalWrite(trigPin1, LOW);
digitalWrite(trigPin2, LOW);
digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds and read duration
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
duration3 = pulseIn(echoPin3, HIGH);
// Calculating the distance
distance1 = duration1 * 0.034 / 2;
distance2 = duration2 * 0.034 / 2;
distance3 = duration3 * 0.034 / 2;
// Prints the distance on the Serial Monitor every seconds
if (millis() - prevMillis >= 1000){
Serial.println();
Serial.print("Distance 1: ");
Serial.print(distance1);
Serial.println("cm");
Serial.print("Distance 2: ");
Serial.print(distance2);
Serial.println("cm");
Serial.print("Distance 3: ");
Serial.print(distance3);
Serial.println("cm");
prevMillis = millis();
}
}
void setup() {
// Sets the trigPins as an Output
pinMode(trigPin1, OUTPUT);
pinMode(trigPin2, OUTPUT);
pinMode(trigPin3, OUTPUT);
// Sets the echoPins as an Input
pinMode(echoPin1, INPUT);
pinMode(echoPin2, INPUT);
pinMode(echoPin3, INPUT);
// Sets the LEDs and buzzer as an Output
pinMode(redLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(greenLED, OUTPUT);
digitalWrite(redLED, LOW);
// Starts the serial communication
Serial.begin(9600);
}
void loop() {
readSensors();
if (distance1 <= trigDistance){
digitalWrite(redLED, HIGH);
tone(buzzer, 1047, 500);
}else{
digitalWrite(redLED, LOW);
}
if (distance2 <= trigDistance){
digitalWrite(greenLED, HIGH);
tone(buzzer, 1319, 500);
}else{
digitalWrite(greenLED, LOW);
}
if (distance3 <= trigDistance){
digitalWrite(blueLED, HIGH);
tone(buzzer, 1568, 500);
}else{
digitalWrite(blueLED, LOW);
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
ultrasonic2:VCC
ultrasonic2:TRIG
ultrasonic2:ECHO
ultrasonic2:GND
ultrasonic3:VCC
ultrasonic3:TRIG
ultrasonic3:ECHO
ultrasonic3:GND
bz1:1
bz1:2
led3:A
led3:C
led1:A
led1:C
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2
led2:A
led2:C