#define ECHO_PIN 2
#define TRIG_PIN 3
void setup() {
Serial.begin(115200);
pinMode(13, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// put your setup code here, to run once:
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH );
return duration * 0.034 / 2;
}
void loop() {
float distance = readDistanceCM();
bool isNearby = distance < 100;
digitalWrite(13, isNearby );
Serial. print("Measured distance : ");
Serial. println(readDistanceCM());
delay(100);
}
// put your main code here, to run repeatedly: