Advertisement
Guest User

Untitled

a guest
May 20th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. const byte TRIGGER_PIN = 2;
  2. const byte ECHO_PIN = 3;
  3. const float SOUND_SPEED = 340.0 / 1000;
  4. int a = 0;
  5. void setup() {
  6. // put your setup code here, to run once:
  7. pinMode(13, OUTPUT);
  8. pinMode(7, OUTPUT);
  9. pinMode(TRIGGER_PIN, OUTPUT);
  10. pinMode(ECHO_PIN, INPUT);
  11. digitalWrite(TRIGGER_PIN, LOW);
  12.  
  13. Serial.begin(9600);
  14.  
  15.  
  16. }
  17.  
  18. void loop() {
  19. digitalWrite(TRIGGER_PIN,HIGH);
  20. delay(10);
  21. digitalWrite(TRIGGER_PIN,LOW);
  22. long measure = pulseIn(ECHO_PIN, HIGH, 25000UL);
  23. float distance_mm = measure / 2.0 * SOUND_SPEED;
  24. Serial.println(distance_mm);
  25.  
  26.  
  27. if(distance_mm<300){
  28. tone(7, 400, 5000);
  29. delay(500);
  30. }
  31. else{
  32. noTone(7);
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. //tone(7, 440, 2000);
  49.  
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement