int trigger_pin=14;
int echo_pin=13;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(echo_pin, INPUT);
pinMode(trigger_pin, OUTPUT);
pinMode(4, OUTPUT);
}
long wdistance (){
digitalWrite(trigger_pin,LOW);
delay(10);
digitalWrite(trigger_pin,HIGH);
delay(100);
digitalWrite(trigger_pin,LOW);
float d=pulseIn(echo_pin,HIGH);
return(d*0.034/2)+1;
delay(1000);
}
void loop() {
int c=wdistance ();
Serial.println(String(c));
if(c>=200){
digitalWrite(4, HIGH);
Serial.println("detected");
}
else{
digitalWrite(4, LOW);
Serial.println("not detected");// put your main code here, to run repeatedly:
}
delay(1000); // this speeds up the simulation
}