Simple code understanding

Hi All,

Just starting out with programming ive managed to get a servo to all sorts of funky movements and am now looking at distance measuring. In respects to the below part of code i just want to check my understanding:-

void loop() {
long duration, distance; //Is this part of the code ???
digitalWrite(trigPin, LOW); // i get this bit start of with the HC-SR04 input low
delayMicroseconds(2); // wait for 2milliseconds so far so good
digitalWrite(trigPin, HIGH); // fire up the HC-SR04
delayMicroseconds(10); // let it run for 10 milliseconds
digitalWrite(trigPin, LOW); // shut it down
duration = pulseIn(echoPin, HIGH); // is this part saying read in the Hc-SR04 low/high pulse duration ?
distance = (duration/2) / 29.1; // get this bit

my main query is the relationship between the setting of "trigpin high" and "pulsein" and also is the program running sequentially or in parrellel i.e. "pulsein" is running at the same time as "trigpinHigh" is "trigpinHigh" kicking off the sonar clicks and within the duration of 10m/s the "pulsein" command is analysing the low/high cycles on which ever pin i set the arduino to.

void loop()
{
  long duration, distance;         //Is this part of the code ???  it declares two variables of type long, so yes.
  digitalWrite(trigPin, LOW);     // i get this bit start of with the HC-SR04 input low.  Trigger pin, not the output
  delayMicroseconds(2);           //  wait for 2milliseconds so far so good.  Not so good.  It delays 2 microseconds
  digitalWrite(trigPin, HIGH);     // fire up the HC-SR04
  delayMicroseconds(10);          // let it run for 10 milliseconds.  microseconds again !  Trigger pin HIGH for 10 microseconds
  digitalWrite(trigPin, LOW);      // shut it down.  
  duration = pulseIn(echoPin, HIGH);  // is this part saying read in the Hc-SR04 low/high pulse duration ?
                                      //pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. 
                                      //Returns the length of the pulse in microseconds.
  distance = (duration / 2) / 29.1; // get this bit

is the program running sequentially or in parrellel

Everything happens sequentially. A deley stops the program for the specified length of time.

PulseIn() blocks till the echo is sensed or the pulseIn function times out. Default time out is 1 second, but can be set via an added parameter when pulseIn is called. PulseIn returns unsigned long.

Thanks both,

If the code is running sequentially, the "trigpin" has been set LOW before calling "pulsein" ? am i missing something

am i missing something

No. The trigger pin should be turned off, so the device stops sending pulses.

Ok

This part of the code runs first

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

The “trigpin” has been set LOW above so no more pulses from the sonar
Now the below runs but what is “pulsein” reading if the above has stopped the sonar, or is it on the next pass i.e. we now go back to the top and after calling “pulsein” on the first pass the subsequent “trigpin HIGH” on the second/third etc pass is read.

duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

Would the below achieve the same ??

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
duration = pulseIn(echoPin, HIGH); // new position
distance = (duration/2) / 29.1;

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

nb1914:
Would the below achieve the same ??

Give it a try.

Would the below achieve the same ??

No. You must stop sending pulses.

Ok i see the code tags will use in future, still quite getting so when i get the LCD and sonar i will do some good old fashioned experimenting.