Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BUZZ 13
- #define TRIG 10
- #define ECHO 11
- void setup() {
- pinMode(BUZZ, OUTPUT);
- pinMode(TRIG, OUTPUT);
- pinMode(ECHO, INPUT);
- Serial.begin(9600);
- }
- void loop() {
- clearSensor();
- Serial.print("Distance: ");
- int distance = pulse();
- Serial.println(distance);
- if (distance <= 12) {
- tone(BUZZ, distance*100);
- } else {
- noTone(BUZZ);
- }
- delay(50);
- }
- void clearSensor() {
- digitalWrite(TRIG, 0);
- delayMicroseconds(2);
- }
- double pulse() {
- digitalWrite(TRIG, 1);
- delayMicroseconds(10);
- digitalWrite(TRIG, 0);
- int d = pulseIn(ECHO, 1);
- return d*0.017;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement