0% found this document useful (0 votes)
5 views

codigo lab 2

CODIGOS ROBOTICA
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)
5 views

codigo lab 2

CODIGOS ROBOTICA
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/ 1

include <Servo.

h>
Servo servo1;
Servo servo2;
const int PINSERVO1 = 3;
const int PINSERVO2 = 2;
const int PULSOMIN = 1000;
const int PULSOMAX = 2000;
const int TRIG = 9;
const int ECHO = 10;
const int LEDVERDE1 = 11;
const int LEDVERDE2 = 12;
long duration;
int distance;
void setup() {
servo1.attach(PINSERVO1, PULSOMIN, PULSOMAX);
servo2.attach(PINSERVO2, PULSOMIN, PULSOMAX);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(LEDVERDE1, OUTPUT);
pinMode(LEDVERDE2, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH);
distance = duration * 0.034 / 2;
Serial.println(distance);
if (distance >= 0 && distance <= 49) {
servo1.write(0);
servo2.write(0);
digitalWrite(LEDVERDE1, LOW);
digitalWrite(LEDVERDE2, LOW);
} else if (distance >= 50 && distance <= 51) {
servo1.write(180);
servo2.write(180);
digitalWrite(LEDVERDE1, HIGH);
digitalWrite(LEDVERDE2, HIGH);
delay(3000);
digitalWrite(LEDVERDE1, LOW);
digitalWrite(LEDVERDE2, LOW);
} else {
servo1.write(0);
servo2.write(0);
digitalWrite(LEDVERDE1, LOW);
digitalWrite(LEDVERDE2, HIGH);
}
delay(200);
}

You might also like