PulseIn Function

Hello, recently I have been playing around with this function however much to my disappointment, this cannot yet be implemented using Firmata/ Processing etc etc..... But I am currently trying to mimic PulseIn for an Arduino-Processing Ultrasonic Range Finder using only the Firmata libraries.

Does anyone know how the PulseIn function exactly works?

I am currently using a while loop to spot the first change in the input of a digital pin("ECHO") and from this count the duration between this change of input and the original pulse ("TRIGGER"), therefore working out the length of a distance. Sadly what seemed to be a good idea did not work......

So basically I am asking what the PulseIn function does 'behind the scenes', most notably if there are any timings involved in the pulsein function possibly causing the code below not to work.....?

while (arduinoMega.digitalRead(PIN) == Arduino.LOW) { // This is the processing code, however the syntax
duration ++; // is mostly the same
delay(1);
}

Does anyone know how the PulseIn function exactly works?

Yes. Have YOU looked at the reference page? Or, the source code?

I am currently using a while loop to spot the first change in the input of a digital pin("ECHO") and from this count the duration between this change of input and the original pulse ("TRIGGER"), therefore working out the length of a distance. Sadly what seemed to be a good idea did not work......

No kidding? Why do you feel the need to turn the Arduino into a moron? That's what Firmata does.

most notably if there are any timings involved in the pulsein function possibly causing the code below not to work.....?

pulseIn() is all about timing.

  1. Not helpful but thank you, yes I have looked at the reference page, I did not find this useful, however I thought maybe someone could give me a clearer outline without having to look at the source code to speed things up.

  2. Because Arduino is a moron in comparison to the processing sketch using OpenCV and many server databases, if I did not have to use it I would not however Processing compatible Rangefinding software/hardware is not currently available.

  3. I have gathered that much, I was asking for the specific timings and/or series of processing the Arduino makes to return a PulseIn value.... Which I would assume is few.

But never mind this post of me asking for assistance quickly rather than having to scan through the source code myself, it has taken me longer to reply to your string of unhelpful sarcastic responses than it would have to scour through the numerous functions of Arduino and maybe even build my own function that would miraculously solve my issue.

There are contradicting definitions of what pulseIn() does on this website. For example, in the Arduino reference of constants, functions etc. it states that pulseIn() with a value of HIGH, waits for the pin to go HIGH, start timing and subsequently stops timing when the pin goes LOW.

Now a pin in INPUT mode as opposed to a pin in INPUT_PULLUP mode is not HIGH by default. That means that the ‘echo’ Pin (INPUT) on ultrasound sensors such as HR-SR04 goes HIGH when it detects a pulse.

A crude implementation would be (busy waiting/ while loops may have significant overhead):

while (digitalRead(echoPin) == LOW){
  // Cycle this empty loop till echoPin goes High.
}
timestampPulseGenerated = micros();

while (digitalRead(echoPin) == HIGH){
  // Cycle this empty loop till echoPin goes LOW.
}
timestampPulseReceived = micros();
  
interval = timestampPulseReceived - timestampPulseGenerated;

Serial.print("\t\t"); Serial.print("tTS: "); Serial.print(tTS); Serial.print("\t\t"); Serial.print("fTS: "); Serial.println(fTS);
distanceToObject = (interval/ 29.136678)/2;
Serial.print("interval: "); Serial.print(interval); Serial.print("uS"); Serial.print("\t\t"); Serial.print("distanceToObject: "); Serial.print(distanceToObject, 1); Serial.println("cm");

Sorry but I can’t be bothered to declare and initialize variables in this editor. It kind of pisses me off. So heed my warning, check the code and make corrections where necessary.

I think a much better implementation is to use a timer interrupt or another ISR to measure the interval between generating ultrasonic bursts and receiving their echoes.

The NewPing library (better implementation of the ping example written on this example) has a good example of this:
https://code.google.com/p/arduino-new-ping/wiki/Ping_Event_Timer_Sketch

CantiTin23:
I think a much better implementation is to start use a timerinterrupt or another ISR to measure the interval between generating ultrasonic bursts and receiving their echoes.

IMHO the biggest problem of PulseIn() is that it is blocking to some level (depends on timeout param), an interrupt based version would not.

Oh, sorry. My explanation is wrong. There is no contradiction in the references on this website, however some people write confusing datasheets. I thought pulseIn() did something different because some people use it to measure pulses of external hardware but they do not bother to explain how that hardware functions. In my case, it was an ultrasound range finder (HR-SR04). I have found a thread that clarifies what the functionality of the HR-SR04 is.

http://forum.arduino.cc/index.php?PHPSESSID=mv6ng02dekcsmgv3bvl1e7sjc7&topic=202563.0

Let me quote a global moderator (named AWOL):

The echo pulse goes high pretty much as soon as the device is triggered, and goes low as soon as the echo is received.
There may be a deliberate "deaf period" during and just after the outgoing pulse train to prevent direct coupling causing spurious returns, analogous to a pulsed radar's main bang suppression. (Bats also have main bang suppression by having a mechanism that clamps their eardrum to prevent them deafening themselves!)

So yes, the reference about pulseIn() is correct, however like I said previously, there are better implementations.

I apologise for my late reply to this post; became a tad frustrated after my last attempt...... But thanks for your help guys I shall have another shot at this now!