const int trigpin = 25;
const int echopin = 33;
int duration;
int distance;
int red = 15;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
pinMode(15, OUTPUT);
}
void loop() {
distance = getdistance();
Serial.println(("distance"+String(distance)));
if(distance<200){
digitalWrite(15, HIGH);
}
else{
digitalWrite(15, LOW);
}
delay(1000);//this speeds up the simulation
}
long getdistance(){
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin,HIGH);
return(duration*0.034/2)+1;
}