Problem using Arduino pulsein function

I am using a nano.
The input signal is connected to A5. It is an MSF time data signal - high pulses 100ms, 200ms, etc.
This is the code I am using with pulsein.

#define pulse_ip A5
uint16_t pulsetime;

void setup() {
  Serial.begin(9600);
  pinMode(pulse_ip, INPUT);
}

void loop() {
  pulsetime = pulseIn(pulse_ip, HIGH, 1500000ul);

  if (pulsetime > 1000000) {
    Serial.print(pulsetime / 1000000);
    Serial.println("s");
  } else if (pulsetime > 1000) {
    Serial.print(pulsetime / 1000);
    Serial.println("ms");
  } else {
    Serial.print(pulsetime);
    Serial.println("us");
  }
  //delay(1);
}

This is the result in the serial monitor - 40ms to 60ms:
12:40:31.165 -> 43ms
12:40:32.182 -> 42ms
12:40:33.174 -> 44ms
12:40:34.224 -> 56ms
12:40:35.226 -> 58ms
12:40:36.231 -> 58ms
12:40:37.151 -> 44ms
12:40:38.174 -> 44ms

Then I tried this program reading the pin and printing the value.

Code:

int pushButton = A5;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(20);  // delay in between reads for stability
}

That gives me, for instance, if I read it correctly, a 184ms high pulse:
12:46:45.188 -> 0
12:46:45.224 -> 0
12:46:45.224 -> 1
12:46:45.256 -> 1
12:46:45.256 -> 1
12:46:45.290 -> 1
12:46:45.337 -> 1
12:46:45.337 -> 1
12:46:45.337 -> 1
12:46:45.372 -> 1
12:46:45.408 -> 1
12:46:45.408 -> 1
12:46:45.447 -> 0
12:46:45.447 -> 0

What is going wrong with the pulsein program?

I don't know if this is the cause of your problem. pulseTime() returns unsigned long according to the reference. So uint16_t should be uint32_t = unsigned long on e.g. Arduino Uno

Yes, this solved the problem, many thanks.

Hey Albert, see what this does. It should report change only.
With a pressed contact button you will read bounce too.

Note that printing to the output buffer takes some little time.
Turning binary values into decimal text is most of that.

byte pushButton = A5;
unsigned long lastMicros;
byte buttonState, prevState;
// why micros? because millis() is +/- 1
// note that micros counts by 4, still closer timing
// if you must have millis, divide micros by 1000

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(115200); // Serial Monitor baud rate must match!
  Serial.println( "micros between polled state changes.\n" );
  
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);

  // set states
  buttonState = prevState = digitalRead(pushButton);

  // set the counter 
  lastMicros = micros();
}

// the loop routine runs over and over again forever:
void loop() {

  // read the input pin:
  int buttonState = digitalRead(pushButton);

  if ( buttonState != prevState ) {
    Serial.print(buttonState);
    Serial.print( "  " );
    Serial.println( micros() - lastMicros );

    prevState = buttonState;
    lastMicros = micros();
  }
}


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