Ultrasonic Sensor

hi, im a beginner programmer

Im trying to make a project using a ultrasonic sensor.

But i cant figure out one thing that it needs to do.

I want it to begin logging data when it sees like 20cm of distance instead of sending back data every 5s without a limitation on begin distance.

can someone help me out..

#define echoPin 7
#define trigPin 8

long duration;
long distance;

void setup()

pinMode (echoPin, INPUT);
pinMode (trigPin, OUTPUT);

void loop()
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);

duration = pulseIn (echoPin, HIGH);
distance = duration/58.2;

Serial.println (distance);

duplicate post in Nederlands

Just use an 'if' statement around the serial print.

Have a google for “Arduino IF” and look at the examples of how to use it.

You can use something along the lines of “if” the distance is less than some value .... start logging .

I’d suggest you start by Turning on a LED if the distance is below what you set, then separately look at how to log data , then combine.