Advertisement
RuiViana

Vibracall_e_Buzzer

Nov 14th, 2016
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. const int trigPin = 12;
  2. const int echoPin = 13;
  3. const int motorPin = 5;
  4. const int motor2Pin = 6;
  5. const int limitCm = 30;
  6. #define Buzzer1 10
  7. #define Buzzer2 11
  8. int distancia;
  9.  
  10. void setup()
  11. {
  12. Serial.begin(9600);
  13. pinMode(trigPin, OUTPUT);
  14. pinMode(echoPin, INPUT);
  15. pinMode(motorPin, OUTPUT);
  16. pinMode(motor2Pin, OUTPUT);
  17. pinMode(Buzzer1, OUTPUT); // configura pino como saída
  18. pinMode(Buzzer2, OUTPUT); // configura pino como saída
  19. }
  20.  
  21.  
  22. void loop()
  23. {
  24. digitalWrite(trigPin, LOW);
  25. delayMicroseconds(2);
  26. digitalWrite(trigPin, HIGH);
  27. delayMicroseconds(10);
  28. digitalWrite(trigPin, LOW);
  29. unsigned long duracao = pulseIn(echoPin, HIGH);
  30. distancia = duracao / 58;
  31. Serial.print("Distancia em CM: ");
  32. Serial.print(distancia);
  33. Serial.println("cm");
  34. checkLimit();
  35. delay( 1000 );
  36. }
  37. void checkLimit()
  38. {
  39. if ( distancia < limitCm ) {
  40. digitalWrite( motorPin, HIGH);
  41. analogWrite(Buzzer1, 127);
  42. digitalWrite( motor2Pin, HIGH);
  43. analogWrite(Buzzer2, 127);
  44. Serial.println( "ALERTA!...");
  45. } else {
  46. digitalWrite( motorPin, LOW);
  47. analogWrite(Buzzer1, 0);
  48. analogWrite(Buzzer2, 0);
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement