Created
October 3, 2018 08:17
-
-
Save elktros/e9902ce8e2306b89e8fc9a64dec557c8 to your computer and use it in GitHub Desktop.
Code for the project How to Make a Smart Dustbin using Arduino?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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