// #define trigpin 13
// #define echopin 12
// void setup() {
// pinMode(trigpin, OUTPUT);
// pinMode(echopin, INPUT);
// }
// // the loop function runs over and over again forever
// void loop() {
// digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
// delay(500); // wait for a second
// digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
// delay(500); // wait for a second
// }
/*
Created by ArduinoGetStarted, https://arduinogetstarted.com
Arduino - Ultrasonic Sensor HC-SR04
Wiring: Ultrasonic Sensor -> Arduino:
- VCC -> 5VDC
- TRIG -> Pin 9
- ECHO -> Pin 8
- GND -> GND
Tutorial is available here: https://arduinogetstarted.com/tutorials/arduino-ultrasonic-sensor
*/
int trigPin = 13; // TRIG pin
// int trigPins2 = 11; // TRIG pin
int echoPins1 = 12; // ECHO pin
int echoPins2 = 11; // ECHO pin
float duration_us_s1,duration_us_s2, distance_cm_s1, distance_cm_s2;
void setup() {
// begin serial port
Serial.begin (9600);
// configure the trigger pin to output mode
pinMode(trigPin, OUTPUT);
// configure the echo pin to input mode
pinMode(echoPins1, INPUT);
pinMode(echoPins2, INPUT);
}
void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// measure duration of pulse from ECHO pin
duration_us_s1 = pulseIn(echoPins1, HIGH);
duration_us_s2 = pulseIn(echoPins2, HIGH);
// calculate the distance
distance_cm_s1 = 0.017 * duration_us_s1;
distance_cm_s2 = 0.017 * duration_us_s2;
if (distance_cm_s1 < 4.00) {
Serial.print("Sensor1 distance: ");
Serial.print(distance_cm_s1);
Serial.print(" Result: ");
Serial.println("Alert!");
} else {
// print the value to Serial Monitor
Serial.print("Sensor1 distance: ");
Serial.print(distance_cm_s1);
Serial.println(" cm");
}
if (distance_cm_s2 < 4.00){
Serial.print("Sensor2 distance: ");
Serial.print(distance_cm_s2);
Serial.print(" Result: ");
Serial.println("Alert!");
} else{
Serial.print("Sensor2 distance: ");
Serial.print(distance_cm_s2);
Serial.println(" cm");
}
delay(500);
}