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

Servo: Int Int Int Long Long

This document contains code for an Arduino project that uses an ultrasonic sensor and servo motor to automatically open and close the lid of a dustbin when a hand is detected near it. The code measures the distance to the hand using an ultrasonic sensor, takes the average of 3 measurements, and if the distance is less than 40 cm, it activates a servo motor to open the dustbin lid for 5 seconds before closing it again.

Uploaded by

Akshat Gupta
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)
65 views2 pages

Servo: Int Int Int Long Long

This document contains code for an Arduino project that uses an ultrasonic sensor and servo motor to automatically open and close the lid of a dustbin when a hand is detected near it. The code measures the distance to the hand using an ultrasonic sensor, takes the average of 3 measurements, and if the distance is less than 40 cm, it activates a servo motor to open the dustbin lid for 5 seconds before closing it again.

Uploaded by

Akshat Gupta
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
You are on page 1/ 2

//Electronics is Fun

//www.facebook.com/Electronics is Fun
//www.yotube.com/Electronics is Fun
#include <Servo.h> //servo library
Servo servo;
int trigPin = 2;
int echoPin = 3;
int servoPin = 9;

long duration, dist, average;


long aver[3];

void setup() {
servo.attach(servoPin);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.write(0); //close dustbin cap on power on
delay(1000);
servo.detach();
}

void measure() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(15);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
dist = (duration/2) / 29.1; //obtain distance
}
void loop() {
for (int i=0;i<=2;i++) { //average distance
measure();
aver[i]=dist;
delay(50); //delay between measurements
}
dist=(aver[0]+aver[1]+aver[2])/3; //average distance by 3
measurements

if ( dist<40 ) {
//if hand on the distance 10...50 cm
servo.attach(servoPin);
delay(1);
servo.write(90);
delay(5000); //wait 5 seconds
servo.write(0);
delay(1000);
servo.detach();
}
}

You might also like