Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Ultrasonic.h"
- #define echoPin 13
- #define trigPin 12
- #define ledPin 11
- #define MAX_DISTANCE 20
- #define RUIDO 2
- Ultrasonic ultrasonic(12,13);
- int contador, distanciaAnterior;
- void setup() {
- Serial.begin(9600);
- pinMode(echoPin, INPUT);
- pinMode(trigPin, OUTPUT);
- pinMode(ledPin, OUTPUT);
- distanciaAnterior = 0;
- }
- void loop() {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(5);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- int distancia = ultrasonic.Ranging(CM);
- digitalWrite(ledPin, HIGH);
- if (distancia <= MAX_DISTANCE) {
- if(abs(distancia - distanciaAnterior) > RUIDO) {
- contador = contador+1;
- Serial.print("Contador: ");
- Serial.println(contador);
- digitalWrite(ledPin, LOW);
- delay(1000);
- }
- distanciaAnterior = distancia;
- } else {
- distanciaAnterior = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement