Advertisement
CaptainSpaceCat

Super Sanic

Mar 30th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #define BUZZ 13
  2. #define TRIG 10
  3. #define ECHO 11
  4.  
  5. void setup() {
  6.   pinMode(BUZZ, OUTPUT);
  7.   pinMode(TRIG, OUTPUT);
  8.   pinMode(ECHO, INPUT);
  9.   Serial.begin(9600);
  10. }
  11.  
  12. void loop() {
  13.   clearSensor();
  14.   Serial.print("Distance: ");
  15.   int distance = pulse();
  16.   Serial.println(distance);
  17.   if (distance <= 12) {
  18.     tone(BUZZ, distance*100);
  19.   } else {
  20.     noTone(BUZZ);
  21.   }
  22.   delay(50);
  23. }
  24.  
  25. void clearSensor() {
  26.   digitalWrite(TRIG, 0);
  27.   delayMicroseconds(2);
  28. }
  29.  
  30. double pulse() {
  31.   digitalWrite(TRIG, 1);
  32.   delayMicroseconds(10);
  33.   digitalWrite(TRIG, 0);
  34.  
  35.   int d = pulseIn(ECHO, 1);
  36.  
  37.   return d*0.017;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement