Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
59 views1 page

Code

The document contains an Arduino sketch that utilizes a servo motor and ultrasonic sensor to measure distance. It continuously checks the distance and adjusts the servo's position based on whether the measured distance is less than 30 cm. If the distance is below 30 cm, the servo moves to 90 degrees; otherwise, it returns to 0 degrees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views1 page

Code

The document contains an Arduino sketch that utilizes a servo motor and ultrasonic sensor to measure distance. It continuously checks the distance and adjusts the servo's position based on whether the measured distance is less than 30 cm. If the distance is below 30 cm, the servo moves to 90 degrees; otherwise, it returns to 0 degrees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// Eazytronic.

com
#include <Servo.h> // servo library
Servo s1;
const int trigPin = 2;
const int echoPin = 3;
long duration;
int distanceCm, distanceInch;
void setup()
{

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

s1.attach(4); // Servo Motor

}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;
Serial.println("Distance: ");
Serial.println(distanceCm);
delay(50);

if(distanceCm <30)
{

s1.write(90);
delay(1000);
}

else

{
s1.write(0);
delay(10);

You might also like