int trigger=4;
int echo=5;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
pinMode(23, OUTPUT);//green
pinMode(22, OUTPUT);//yellow
pinMode(21, OUTPUT);//red
}
int finding_distance(){
digitalWrite(trigger, HIGH);
delay(100);
digitalWrite(trigger, LOW);
float a=pulseIn(echo, HIGH);
return(a*0.034/2);
}
void loop() {
int distance=finding_distance();
Serial.println(distance);
if(distance>300){
Serial.println("safe");
digitalWrite(23, HIGH);
digitalWrite(21, LOW);
digitalWrite(22, LOW);}
else if(200<=distance && distance<300){
Serial.println("unsafe");
digitalWrite(22, HIGH);
digitalWrite(23, LOW);
digitalWrite(21, LOW);
}
else if(distance<200){
Serial.println("danger");
digitalWrite(21, HIGH);
digitalWrite(22, LOW);
digitalWrite(23, LOW);
}
// put your main code here, to run repeatedly:
// this speeds up the simulation
}