Skip to content

Instantly share code, notes, and snippets.

@pknowledge
Created October 28, 2019 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pknowledge/a097505cde80bcb2eff83b79f8f70ad1 to your computer and use it in GitHub Desktop.
Save pknowledge/a097505cde80bcb2eff83b79f8f70ad1 to your computer and use it in GitHub Desktop.
Arduino smart Dustbin
dd///
///
/// NOTICE: We SHOULD supply 5.0 Voltage to SERVO run stably
///
///
#include <Servo.h> //servo library
Servo servo;
int trigPin = 5;
int echoPin = 6;
int servoPin = 7;
long duration, dist;
long avg[3]; //array for average
void setup() {
servo.attach(servoPin);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.write(0); //close cap on power on
delay(100);
servo.detach();
}
void measure() {
digitalWrite(10,HIGH);
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();
avg[i]=dist;
delay(10); //delay between measurements
}
dist=(avg[0]+avg[1]+avg[2])/3;
if ( dist<50 ) {
//Change distance as per your need
servo.attach(servoPin);
delay(1);
servo.write(0);
delay(3000);
servo.write(150);
delay(1000);
servo.detach();
}
}
///
///
/// NOTICE: We SHOULD supply 5.0 Voltage to SERVO run stably
///
///
d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment