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

0% found this document useful (0 votes)
1 views3 pages

Program Code

Uploaded by

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

Program Code

Uploaded by

pmshrikvno.1atl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Program Code

#include <Servo.h> //INCLUDES SERVO LIBRARY

Servo servo;

int trig = 3;
Pin Assignment
int echo = 4;

int servoPin = 8;

long duration, distance, average; Variable type Definition


long aver[3];

void setup() {

Serial.begin(9600);

servo.attach(servoPin);

pinMode(trig, OUTPUT);

pinMode(echo, INPUT);

servo.write(0); //CLOSES CAP ON STARTING

delay(100);

servo.detach();

void measure() {

digitalWrite(trig, LOW);

delayMicroseconds(5);

digitalWrite(trig, HIGH);

delayMicroseconds(15);

digitalWrite(trig, LOW);

pinMode(echo, INPUT);

duration = pulseIn(echo, HIGH);

distance = (duration / 2) / 29.1; //CALCULATES DISTANCE

}
void loop() {

Serial.println(distance); //CAN BE DISABLED

for (int i = 0; i <= 2; i++) { //CALCULATES AVERAGE DISTANCE

measure();

aver[i] = distance;

delay(10);

distance = (aver[0] + aver[1] + aver[2]) / 3;

if ( distance <= 10 ) { //CHANGE AS PER AS NEED

servo.attach(servoPin);

delay(1);

servo.write(0);

delay(1000); //CHANGE AS PER AS NEED

servo.write(90);

delay(1500); //CHANGE AS PER AS NEED

servo.detach();

You might also like