0% found this document useful (0 votes)
12 views2 pages

Arduino Sonar System with Servo Control

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Arduino Sonar System with Servo Control

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <Wire.

h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

#define TRIG_PIN 9
#define ECHO_PIN 10
#define GREEN_LED 2
#define RED_LED 3
#define BUZZER 4
#define SERVO_PIN 6

LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust if needed


Servo myServo;

int pos = 0;
int direction = 1; // 1 = forward, -1 = backward

void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(BUZZER, OUTPUT);

[Link](SERVO_PIN);
[Link](pos);

[Link](16,2);
[Link]();
[Link](0, 0);
[Link](" Sonar System ");
delay(2000);
[Link]();
}

long readDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}

void loop() {
long distance = readDistance();

if (distance > 20 || distance == 0) {


// No object detected
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
digitalWrite(BUZZER, LOW);

[Link](0, 0);
[Link](" Area is Empty ");
[Link](0, 1);
[Link](" ");

// Move servo faster (delay reduced)


[Link](pos);
delay(10);
pos += direction;

if (pos >= 180 || pos <= 0) {


direction = -direction;
}
} else {
// Object detected
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, HIGH);
digitalWrite(BUZZER, HIGH);

[Link](0, 0);
[Link](" Warning ");
[Link](0, 1);
[Link](" Foreign Body ");

// Hold servo in place


[Link](pos);
}
}

You might also like