Advertisement
maeroso

foo-bar

Aug 27th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1.  
  2.  
  3. #include "Ultrasonic.h"
  4. #define echoPin 13
  5. #define trigPin 12
  6. #define ledPin 11
  7. #define MAX_DISTANCE 20
  8. #define RUIDO 2
  9.  
  10. Ultrasonic ultrasonic(12,13);
  11.  
  12. int contador, distanciaAnterior;
  13.  
  14. void setup() {
  15.   Serial.begin(9600);
  16.   pinMode(echoPin, INPUT);
  17.   pinMode(trigPin, OUTPUT);
  18.   pinMode(ledPin, OUTPUT);
  19.   distanciaAnterior = 0;
  20. }
  21.  
  22. void loop() {
  23.   digitalWrite(trigPin, LOW);
  24.   delayMicroseconds(5);
  25.   digitalWrite(trigPin, HIGH);
  26.   delayMicroseconds(10);
  27.   digitalWrite(trigPin, LOW);
  28.  
  29.   int distancia = ultrasonic.Ranging(CM);
  30.  
  31.   digitalWrite(ledPin, HIGH);
  32.  
  33.   if (distancia <= MAX_DISTANCE) {
  34.     if(abs(distancia - distanciaAnterior) > RUIDO) {
  35.       contador = contador+1;
  36.       Serial.print("Contador: ");
  37.       Serial.println(contador);
  38.       digitalWrite(ledPin, LOW);
  39.       delay(1000);
  40.     }
  41.     distanciaAnterior = distancia;
  42.   } else {
  43.     distanciaAnterior = 0;
  44.   }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement