int LEDred1 = 7;
int LEDred2 = 6;
int LEDred3 = 5;
int Buzzer = 3;
int trigPin = 10;
int echoPin = 9;
long duration, distance;
void setup() {
// put your setup code here, to run once:
pinMode(LEDred1, OUTPUT);
pinMode(LEDred2, OUTPUT);
pinMode(LEDred3, OUTPUT);
pinMode(Buzzer, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
Serial.print("Distance:");
Serial.print(distance);
Serial.print("cm");
if (distance>100){
digitalWrite(LEDred1, HIGH);
digitalWrite(LEDred2, HIGH);
digitalWrite(LEDred3, HIGH);
noTone(Buzzer);
}
else{
digitalWrite(LEDred1, LOW);
digitalWrite(LEDred2, LOW);
digitalWrite(LEDred3, LOW);
tone(Buzzer,1000);
}
}