# PulseIn - PWM monitoring

**URL:** <https://forum.arduino.cc/t/pulsein-pwm-monitoring/945526>\
**Category:** General Discussion\
**Created:** [January 10, 2022, 4:28pm UTC](https://forum.arduino.cc/t/pulsein-pwm-monitoring/945526 "2022-01-10T16:28:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![the-dude](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@the-dude](https://forum.arduino.cc/u/the-dude)\
**Post date:** [January 10, 2022, 4:28pm UTC](https://forum.arduino.cc/t/pulsein-pwm-monitoring/945526/1 "2022-01-10T16:28:50Z")

</div>

Hello,  
I'm currently using the pulseIn function to monitor when my laser engraver controller starts and stops firing the laser using pwm. PulseIn reads a pulse (either `HIGH` or `LOW` ) on a pin. For example, if `value` is `HIGH` , `pulseIn()` waits for the pin to go from `LOW` to `HIGH` , starts timing, then waits for the pin to go `LOW` and stops timing. Returns the length of the pulse in microseconds or gives up and returns 0 if no complete pulse was received within the timeout. The timeout is 5 seconds.

I recently purchased a new laser that uses mirrors (galvo) in addition to the x and y movements you would typically find on a flat bed laser. The code I used for my previous machine is below and @johnwasser was a tremendous help getting that one developed. Now my dilemma, the previous code still works for this new laser, but the laser fires so quickly that I get a bunch of on and off's in succession.

What I would ideally like is for the code to wait for 1 second (at the current codes "laser off" stage) and if a "laser on" is triggered within that 1 second, consider the laser still on. If no "laser on" is received after the 1 second, then consider the laser truly off and send a "laser off".

```cpp
const byte PWM_PIN = 3;

boolean WasOn = false;

void setup()
{
  pinMode(PWM_PIN, INPUT);
  Serial.begin(115200);
}

void loop()
{
  // If there is any pulse in the next three seconds, the laser is ON
  delay(50);
  boolean isOn = pulseIn(PWM_PIN, HIGH, 3000000UL) > 0;
  
  if (isOn != WasOn)
  {
    // State change detected
    WasOn = isOn;

    if (isOn)
      Serial.println("LASER ON");
    else
      Serial.println("LASER OFF");
  }
}

```

My previous post that is now closed is here: [pulseIn for detecting CO2 laser status - #9 by the-dude](https://forum.arduino.cc/t/pulsein-for-detecting-co2-laser-status/890111/9)

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [January 10, 2022, 4:49pm UTC](https://forum.arduino.cc/t/pulsein-pwm-monitoring/945526/2 "2022-01-10T16:49:28Z")

</div>

You want a retriggerable monoflop?

pulseIn() is not required. Assume laser is ON. Take the starting time and update it whenever a HIGH is detected before a 1 s interval. If the interval elapses the laser is OFF and goes ON with the next HIGH input.

---

<div class="post-metadata">

**Author:** ![the-dude](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@the-dude](https://forum.arduino.cc/u/the-dude)\
**Post date:** [January 10, 2022, 5:25pm UTC](https://forum.arduino.cc/t/pulsein-pwm-monitoring/945526/3 "2022-01-10T17:25:40Z")

</div>

Hey Doc, yeah the code would essentially be a mimicking a retriggerable monoflop, in a way. I have to use pulseIn() unfortunately due to some electrical noise and the fact I can only monitor the pwm signals from the controller.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 9, 2022, 5:25pm UTC](https://forum.arduino.cc/t/pulsein-pwm-monitoring/945526/4 "2022-07-09T17:25:59Z")

</div>

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