const int buttonPin = A2;
const int trigPin = A4;
const int echoPin = A3;
const int greenLed = A0;
const int yellowLed = A5;
const int redLed = A1;
const int buzzerPin = A6;
long duration, distance;
int buttonState=0;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);.
pinMode(buttonPin, INPUT);
pinMode(redLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == LOW ) {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
int duration = pulseIn(echoPin, HIGH);
float distance = duration / 58.0;
Serial.print("Distance in CM: ");
Serial.println(distance);
if(distance<40){
digitalWrite(redLed, HIGH);
digitalWrite(yellowLed, LOW);
digitalWrite(greenLed, LOW);
tone(buzzerPin, 1000);}
else if(distance>=40 && distance<=120){
digitalWrite(redLed, LOW);
digitalWrite(yellowLed, HIGH);
digitalWrite(greenLed, LOW);
tone(buzzerPin, 300);}
else if(distance>120){
digitalWrite(redLed, LOW);
digitalWrite(yellowLed, LOW);
digitalWrite(greenLed, HIGH);
noTone(buzzerPin);
}
}
delay(1000);
}