#include #define TX 0 #define RX 1 SoftwareSerial serial(RX,TX); #define echoPin 7 #define trigPin 8 const int ledPin = 3; long duration; long distance; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(ledPin,OUTPUT); serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor serial.println("with Arduino UNO R3"); } void loop() { // Clears the trigPin condition digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin HIGH (ACTIVE) for 10 microseconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance = duration * 0.034 / 2; if (distance < 10) serial.println("good"); }