#define trigpin 3
#define echopin 6
long ult_duration;
long ult_distance;
// Set a measuring stick kind of
int expc = 39;
void setup() {
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
Serial.begin(115200);
}
void loop() {
digitalWrite(trigpin,LOW);
delayMicroseconds(1);
digitalWrite(trigpin, HIGH);
// Activate sensing for 10 microseconds
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
//Read echo pin
ult_duration = pulseIn(echopin,HIGH);
ult_distance = ult_duration * 0.034 /2;
Serial.print("Distance: ");
Serial.print(ult_distance);
Serial.println(" cm");
if(ult_distance < expc){
Serial.println("Motion Detected");
}
delay(10); // this speeds up the simulation
}