Skip to content

Instantly share code, notes, and snippets.

@elktros
Created October 3, 2018 08:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save elktros/e9902ce8e2306b89e8fc9a64dec557c8 to your computer and use it in GitHub Desktop.
Save elktros/e9902ce8e2306b89e8fc9a64dec557c8 to your computer and use it in GitHub Desktop.
Code for the project How to Make a Smart Dustbin using Arduino?
#include <Servo.h>
Servo myservo;
int pos = 20;
const int trigPin = 5;
const int echoPin = 6;
const int led = 13;
long duration;
float distance;
void setup()
{
myservo.attach(11);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
myservo.write(pos);
}
void loop()
{
//Serial.begin(9600);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = 0.034*(duration/2);
//Serial.println(distance);
if (distance < 27)
{
digitalWrite(led,HIGH);
myservo.write(pos+160);
delay(1000);
}
else
{
digitalWrite(led,LOW);
myservo.write(pos);
}
delay(300);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment