#define trigPin1 3 #define echoPin1 2 #define trigPin2 4 #define echoPin2 5 #define buzzPin1 11 long duration, distance, RightSensor, LeftSensor; int toneToPlayL = 0; int toneToPlayR = 0; void setup() { Serial.begin (115200); pinMode(trigPin1, OUTPUT); pinMode(echoPin1, INPUT); pinMode(trigPin2, OUTPUT); pinMode(echoPin2, INPUT); pinMode(buzzPin1, OUTPUT); } void loop() { SonarSensor(trigPin1, echoPin1); RightSensor = distance; SonarSensor(trigPin2, echoPin2); LeftSensor = distance; Serial.print(LeftSensor); Serial.print(" - "); Serial.println(RightSensor); //----------------------------------------------------------------------------- if (LeftSensor > 175) { noTone; } else if (LeftSensor <= 175 && LeftSensor > 150) { toneToPlayL = 493; } else if (LeftSensor <= 150 && LeftSensor > 125) { toneToPlayL = 440; } else if (LeftSensor <= 125 && LeftSensor > 100) { toneToPlayL = 391; } else if (LeftSensor <= 100 && LeftSensor > 75) { toneToPlayL = 349; } else if (LeftSensor <= 75 && LeftSensor > 50) { toneToPlayL = 329; } else if (LeftSensor <= 50 && LeftSensor >= 25) { toneToPlayL = 293; } else if (LeftSensor < 25) { toneToPlayL = 261; } //----------------------------------------------------------------------------- if (RightSensor > 156) { noTone; } else if (RightSensor <= 156 && RightSensor > 144 ) { toneToPlayR = 523; } else if (RightSensor <= 131 && RightSensor > 119) { toneToPlayR = 523; } else if (RightSensor <= 105 && RightSensor > 94) { toneToPlayR = 440; } else if (RightSensor <= 56 && RightSensor > 44) { toneToPlayR = 391; } else if (RightSensor <= 31 && RightSensor > 19) { toneToPlayR = 349; } else if (RightSensor < 19) { noTone; } tone(buzzPin1, toneToPlayR, 750); delay(1250); tone(buzzPin1, toneToPlayL, 750); delay(500); toneToPlayR = 0; toneToPlayL = 0; } void SonarSensor(int trigPin, int echoPin) { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration / 2) / 29.1; }