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

0% found this document useful (0 votes)
28 views4 pages

Papelera Automatica

proyecto arduino

Uploaded by

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

Papelera Automatica

proyecto arduino

Uploaded by

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

#include<Servo.

h>

const int trigPin = 5;

const int echoPin = 6;

const int servoPin = 7;

// defines variables

double SetDelay, Input, Output, ServoOutput;

Servo myServo; //Initialize Servo.

void setup() {

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(9600); // Starts the serial communication

myServo.attach(servoPin); //Attach Servo

Input = readPosition();

void loop() {
Input = readPosition();

ServoOutput=Input;

myServo.write(ServoOutput);

if(ServoOutput ==140)

SetDelay=3000;

else{

SetDelay=100;

delay(SetDelay);

float readPosition() {

delay(40);

long duration, var;

int distance;

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echoPin

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance = duration/(29*2);

// Prints the distance on the Serial Monitor

Serial.print("Distance: ");

Serial.println(distance);

if(distance <68) {

var=140;

else{

var=50;
}

//Returns distance value.

return var;

You might also like