Regarding the PulseIn() command

hi, i was just wondering if anyone can clarify for me:

when i set the option for pulse type to "HIGH" for PulseIn(), i am asking for the duration between consecutive 0 to 1 state changes, and when i set it as "LOW" i am asking for the duration between consecutive 1 to 0 state changes, right or wrong?

HIGH would return the length of the pulse, not the period of the pulse:

Pin goes HIGH, timer starts... ... pin goes LOW, timer ends.

sure ok thanks i see what you mean, but it should then be possible to write code that determines the period of the pulse via some kind of linear combination of pulseIn(val,HIGH) pulseIn(val,LOW) yes?

How fast is the pulse you are reading?

Is it repetitive and periodic? Or is it more of a one-shot deal?

If you want to figure out the frequency of a pulse, as opposed to its width, there may be other options.

Something like this?

unsigned long periodIn(pin)
  uint32_t p = pulseIn(pin, HIGH);
  p += pulseIn(pin, LOW);
  return p;

A faster way might be to rewrite periodIn() based upon PulseIn();

unsigned long periodIn(uint8_t pin, unsigned long timeout)
    uint8_t bit = digitalPinToBitMask(pin);
    uint8_t port = digitalPinToPort(pin);

    unsigned long width = 0; 
    unsigned long numloops = 0;
    unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;  // timeout must >> 2x expected period

    // wait for any previous period to end
    while ((*portInputRegister(port) & bit) == 0)
        if (numloops++ == maxloops)
            return 0;
    while ((*portInputRegister(port) & bit) != 0)
        if (numloops++ == maxloops)
            return 0;

    // count HIGH part of period
    while ((*portInputRegister(port) & bit) == 0) {
        if (numloops++ == maxloops)
            return 0;
    // count LOW part of period
    while ((*portInputRegister(port) & bit) != 0) {
        if (numloops++ == maxloops)
            return 0';

    return clockCyclesToMicroseconds(width * 21 + 16);  // adapt this if needed

give it a try

thanks will do, ive found it has some hardware dependent factors on its effectiveness to, ie just for pulseIn(), and you can try these two scenarios out yourself to see what i mean

i) i get a clear 16 microsecond measurement every time when i hook up a wifi USB dongle thing, (cut off the USB connection red wire into +5V pin, Black in GND , green and white into analog pins 0&1.

ii) i connect an analog input to a RLC, (which i verify is oscillating cleanly by looking at the serial readout of analogRead() first)

and when it comes to using PulseIn() (ie HIGH will be read by the MC when the waveform peaks over 3V, and LOW when it drops below 2 V) the sketch just sits there for ages, then gives up and returns 0,

My reasoning for thinking it should still be working, im new to programming, (ive only just discovered how helpful it is for physics, im slow in alot of respects) although its obviously some sort of composite of digitalRead() you can still measure the with of waveform by knowing what analog values. This brings me to another question Is there a way to recalibrate the voltage that my MC interprets as HIGH and LOW?

Is there a way to recalibrate the voltage that my MC interprets as HIGH and LOW?

Not that I am aware of for the digitalRead() function.

However if the freq/period you measure is < 1000 Hz/ > 1ms you could create a digitalRead function that wraps around an analogRead().

int   myDigitalRead(int pint, int thresholdHIGH, int thresholdLOW)
  int raw = analogRead(pin);
  if (raw >= thresholdHIGH) return HIGH;
  if (raw <= thresholdLOW) return LOW;
  return -1;  // sort of UNDEFINED

Furthermore you can use the ADC chip in comparator mode

thanks yes i had a look, how do you declare the interrupt exclusive to the analog comparator? thanks again.