Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Se definen los pines
- #define trigPin 13
- #define echoPin 12
- #define rojo 7
- #define verde 6
- void setup() {
- Serial.begin (9600);
- //Se configuran los pines
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(rojo, OUTPUT);
- pinMode(verde, OUTPUT);
- }
- void loop() {
- long duracion, distancia;
- //Se deshabilita el sensor
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- //Se habilita el sensor
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- //Se deshabilita el sensor
- digitalWrite(trigPin, LOW);
- //Se lee la duracin del pulso en microsegundos
- duracion = pulseIn(echoPin, HIGH); //Esta instruccion lee el tiempo que un pin estรก en
- estado determinado
- Serial.print("Duracion: ");
- Serial.println(duracion);
- distancia = (duracion/29.2) / 2;
- if (distancia < 15)
- {
- digitalWrite(rojo,HIGH);
- digitalWrite(verde,LOW);
- }
- else
- {
- digitalWrite(rojo,LOW);
- digitalWrite(verde,HIGH);
- }
- if (distancia >= 330 || distancia <= 3)
- {
- Serial.println("Fuera de Rango");
- }
- else
- {
- Serial.print(distancia);
- Serial.println("cm");
- }
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement