pulseIn funct equivalent

Hi, i had written pulseIn function equivalet for read sr04 sensor, but pulseIn function and my function, gives different result.
Im trying to fix my function for migrate my project on stm32.

const int trigger = 2;
const int echo = 3;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);

  digitalWrite(trigger, LOW);
}

bool b = 0;

void loop() {
  // put your main code here, to run repeatedly:
  static long duration;
  static int distance;
  static int distancepulse;

  digitalWrite(trigger, LOW);
  delayMicroseconds(10);

  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);

  digitalWrite(trigger, LOW);

  if (b)
  {
    while(digitalRead(echo) == LOW);

    duration = 0;
    while(digitalRead(echo) == HIGH)
    {
      delayMicroseconds(1);
      duration++;
    }
  }
  else
  {
    duration = pulseIn(echo, HIGH);
  }

  distance = duration * 0.034 / 2;

  if (b)
    Serial.print("Distanza : ");
  else
    Serial.print("PULSE-IN Distanza : ");
  Serial.print(duration / 100.0F);
  Serial.println("cm");

  delay(500);

  b = !b;
}

Your duplicate topic posted yesterday has been deleted

Why did you post the same question today ?

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Timestamp the start of the echo pulse.
Timestamp the end of the echo pulse.
Subtract.
Rinse, repeat.

Maybe even disable interrupts.

    duration = 0;
    while(digitalRead(echo) == HIGH)
    {
      delayMicroseconds(1);
      duration++;
    }

All you are doing is counting the number of times you call "delayMicroseconds(1)". Even if the "delayMicroseconds(1)" takes 1 microsecond you are not counting the time it takes to add 1 to 'duration' or to do the looping and reading. I think the digitalRead() alone takes about 6 microseconds.

If you have the micros() function available, use that:

    unsigned long startTime = micros();
    while(digitalRead(echo) == HIGH) {}
    duration = micros() - startTime;

If you don't want to rely on micros() you will probably have to experiment to find out how fast the loop runs and then translate loop count to microseconds. Let's say you determine it takes 4.3785 microseconds per loop:

    duration = 0;
    while(digitalRead(echo) == HIGH)
    {
      duration++;
    }
    duration = duration * 4.3785;  // Convert loops to microseconds.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.