Doubt

void loop() {
// put your main code here, to run repeatedly:

digitalWrite(trigpin,LOW);
delayMicroseconds(2);
digitalWrite(trigpin,HIGH);
long duration=pulseIn(echopin,HIGH);
long distance=(duration*0.0342)/2;
Serial.println(distance);
}
please somebody tell me step-by-step how this code executes including the looping thats going on.

void loop() {
  // put your main code here, to run repeatedly: 
  digitalWrite(trigpin,LOW);                          // make sure that the trigpin starts low 
  delayMicroseconds(2);                              // wait for 2 microseconds
  digitalWrite(trigpin,HIGH);                        // set trigpin high to trigger the rangefinder
  long duration=pulseIn(echopin,HIGH);      //  count the microseconds till the echopin goes high
  long distance=(duration*0.0342)/2;          // calculate the distance from the duration
  Serial.println(distance);                           // print the distance
  }                                                           //  go back to start

Ranging with no time between ranges will be error prone because of false echos. Best to wait a bit between ranges (20 - 30ms or more) to let echos die down.

What do you “doubt”?

Usually, the trigger for an ultrasound ranger is a HIGH pulse of around 10us.

It looks like you may have copied the code incorrectly.

void loop() 
{
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin,LOW);
  unsigned long duration=pulseIn(echopin,HIGH);
  long distance=(duration*0.0342)/2;
  Serial.println(distance);
  delay (50); // We don't want to ping too frequently - strange things may happen
  }

Does the 'void loop()' end its iteration after encountering the 'delay()' function and moves to next iteration?
I was executing the following sketch:
void loop()
{
forward();
delay(3000);
halt();
delay(3000);
back();
}
In the above code 'forward()' function was executed for 3 seconds and 'halt()' for another 3 seconds but it skipped the 'back()' function. Please clear the doubt.

mmd1997:
Does the ‘void loop()’ end its iteration after encountering the ‘delay()’ function and moves to next iteration?
I was executing the following sketch:
void loop()
{
forward();
delay(3000);
halt();
delay(3000);
back();
}
In the above code ‘forward()’ function was executed for 3 seconds and ‘halt()’ for another 3 seconds but it skipped the ‘back()’ function. Please clear the doubt.

Try :

void loop()
{
    forward();
delay(3000);
halt();
delay(3000);
back();
delay(3000);   // <<<<<<<<<<<<<<<<<<<
}

(deleted)

I doubt that there was need for two Threads on the same subject so I am suggesting to the Moderator to merge them.

Double posting just wastes people’s time - including your own.

…R

Apart from the pointless identical title, why have these been merged?