Skip to content

Instantly share code, notes, and snippets.

@vanessa242 vanessa242/Visions
Created May 26, 2018

Embed
What would you like to do?
const int buzzerPin = 3;
const int trigPinLeftBottom = 11;
const int trigPinRightBottom = 10;
const int trigPinLeftTop = 9;
const int trigPinRightTop = 6;
const int echoPinLeftBottom = 13;
const int echoPinRightBottom = 12;
const int echoPinLeftTop = 8;
const int echoPinRightTop = 7;
long durationRightBottom, cmRightBottom;
long durationLeftBottom, cmLeftBottom;
long durationRightTop, cmRightTop;
long durationLeftTop, cmLeftTop;
long avgDist;
void setup() {
Serial.begin (9600);
pinMode(trigPinLeftBottom, OUTPUT);
pinMode(trigPinRightBottom, OUTPUT);
pinMode(trigPinLeftTop, OUTPUT);
pinMode(trigPinRightTop, OUTPUT);
pinMode(echoPinLeftBottom, INPUT);
pinMode(echoPinRightBottom, INPUT);
pinMode(echoPinLeftTop, INPUT);
pinMode(echoPinRightTop, INPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
digitalWrite(trigPinLeftBottom, LOW);
delayMicroseconds(2);
digitalWrite(trigPinLeftBottom, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinLeftBottom, LOW);
pinMode(echoPinLeftBottom, INPUT);
durationLeftBottom = pulseIn(echoPinLeftBottom, HIGH);
cmLeftBottom = (durationLeftBottom/2) / 29.1;
digitalWrite(trigPinRightBottom, LOW);
delayMicroseconds(2);
digitalWrite(trigPinRightBottom, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinRightBottom, LOW);
pinMode(echoPinRightBottom, INPUT);
durationRightBottom = pulseIn(echoPinRightBottom, HIGH);
cmRightBottom = (durationRightBottom/2) / 29.1;
digitalWrite(trigPinLeftTop, LOW);
delayMicroseconds(2);
digitalWrite(trigPinLeftTop, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinLeftTop, LOW);
pinMode(echoPinLeftTop, INPUT);
durationLeftTop = pulseIn(echoPinLeftTop, HIGH);
cmLeftTop = (durationLeftTop/2) / 29.1;
digitalWrite(trigPinRightTop, LOW);
delayMicroseconds(2);
digitalWrite(trigPinRightTop, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinRightTop, LOW);
pinMode(echoPinRightTop, INPUT);
durationRightTop = pulseIn(echoPinRightTop, HIGH);
cmRightTop = (durationRightTop/2) / 29.1;
Serial.print(cmLeftBottom);
Serial.print(" ");
Serial.print(cmRightBottom);
Serial.print(" ");
Serial.print(cmLeftTop);
Serial.print(" ");
Serial.print(cmRightTop);
Serial.print(" ");
Serial.println();
if (cmLeftBottom < 20 || cmRightBottom < 20 || cmLeftTop < 20 || cmRightTop < 20) {
tone(buzzerPin, 600);
} else if (cmLeftBottom < 40 || cmRightBottom < 40 || cmLeftTop < 40 || cmRightTop < 40) {
tone(buzzerPin, 400);
} else if (cmLeftBottom < 60 || cmRightBottom < 60 || cmLeftTop < 60 || cmRightTop < 60){
tone(buzzerPin, 200);
} else if (cmLeftBottom < 75 || cmRightBottom < 75 || cmLeftTop < 75 || cmRightTop < 75) {
tone(buzzerPin, 50);
} else {
tone(buzzerPin, 0);
}
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.