// Includes the Servo library
#include <Servo.h>
// Defines Trig and Echo pins of the Ultrasonic Sensor
const int trigPin = 10;
const int echoPin = 11;
// Defines the pin for the red LED and the piezo buzzer
const int LED_PIN = 13;
const int buzzerPin = 9; // Pin for the piezo buzzer
// Variables for the duration and the distance
long duration;
int distance;
Servo myServo; // Creates a servo object for controlling the servo motor
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(LED_PIN, OUTPUT); // Sets the LED pin as Output
pinMode(buzzerPin, OUTPUT); // Sets the buzzerPin as Output
Serial.begin(9600);
myServo.attach(12); // Defines the pin on which the servo motor is attached
}
void loop() {
// Rotates the servo motor from 15 to 165 degrees
for (int i = 15; i <= 165; i++) {
myServo.write(i);
delay(30);
distance = calculateDistance(); // Calls the function for calculating the distance measured by the Ultrasonic sensor for each degree
Serial.print(i); // Sends the current degree to the Serial Monitor
Serial.print(","); // Sends a separator character for later use in the Processing IDE
Serial.print(distance); // Sends the distance value to the Serial Monitor
Serial.print("."); // Sends a separator character
checkLED(distance); // Checks and controls the LED based on the distance
checkBuzzer(distance); // Checks and controls the buzzer based on the distance
}
// Repeats the previous lines from 165 to 15 degrees
for (int i = 165; i > 15; i--) {
myServo.write(i);
delay(30);
distance = calculateDistance();
Serial.print(i);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
checkLED(distance); // Checks and controls the LED based on the distance
checkBuzzer(distance); // Checks and controls the buzzer based on the distance
}
}
// Function for calculating the distance measured by the Ultrasonic sensor
int calculateDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance = duration * 0.034 / 2; // Calculates the distance based on the duration
return distance;
}
// Function to blink the LED based on distance
void checkLED(int distance) {
if (distance <= 10) { // If the object is within 10 cm
digitalWrite(LED_PIN, HIGH); // Turn the LED on
delay(200); // Keep it on for 200 ms (faster blink)
digitalWrite(LED_PIN, LOW); // Turn the LED off
delay(200); // Keep it off for 200 ms (faster blink)
}
else if (distance <= 20) { // If the object is within 20 cm but greater than 10 cm
digitalWrite(LED_PIN, HIGH); // Turn the LED on
delay(500); // Keep it on for 500 ms (slower blink)
digitalWrite(LED_PIN, LOW); // Turn the LED off
delay(500); // Keep it off for 500 ms (slower blink)
}
else {
digitalWrite(LED_PIN, LOW); // Turn the LED off if the distance is greater than 20 cm
}
}
// Function to control the buzzer based on distance
void checkBuzzer(int distance) {
if (distance <= 10) { // If the object is within 10 cm
tone(buzzerPin, 1000); // Play a tone at 1000 Hz
}
else {
noTone(buzzerPin); // Stop the tone if the distance is greater than 10 cm
}
}